From c9aeb4284e6456b8ca846f2bdfcd8f68d6ecd7b8 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Thu, 18 Jul 2019 08:42:00 -0500 Subject: lua move constants out of binary image Rockbox constants are auto generated by a perl script like the majority of the plugin functions. Constants are contained in rb_defines.lua, if the file exists it is auto loaded by the lua state for backwards compatibility Frees ~1k Change-Id: I237700576c748f468249e501c839d89effca3f39 --- apps/plugins/lua/lua.make | 7 +- apps/plugins/lua/rbdefines_helper.pl | 199 +++++++++++++++++++++++++++++++++++ apps/plugins/lua/rocklib.c | 8 +- apps/plugins/lua/rocklua.c | 8 +- 4 files changed, 215 insertions(+), 7 deletions(-) create mode 100755 apps/plugins/lua/rbdefines_helper.pl (limited to 'apps/plugins') diff --git a/apps/plugins/lua/lua.make b/apps/plugins/lua/lua.make index b5a0c74633..f54d35bc44 100644 --- a/apps/plugins/lua/lua.make +++ b/apps/plugins/lua/lua.make @@ -35,11 +35,16 @@ else ROCKS += $(LUA_BUILDDIR)/lua.rock endif -$(LUA_BUILDDIR)/lua.rock: $(LUA_OBJ) $(TLSFLIB) $(LUA_BUILDDIR)/actions.lua $(LUA_BUILDDIR)/buttons.lua $(LUA_BUILDDIR)/settings.lua $(LUA_BUILDDIR)/rocklib_aux.o $(LUA_INCLUDELIST) +$(LUA_BUILDDIR)/lua.rock: $(LUA_OBJ) $(TLSFLIB) $(LUA_BUILDDIR)/actions.lua $(LUA_BUILDDIR)/buttons.lua $(LUA_BUILDDIR)/settings.lua $(LUA_BUILDDIR)/rocklib_aux.o $(LUA_BUILDDIR)/rb_defines.lua $(LUA_INCLUDELIST) $(LUA_BUILDDIR)/actions.lua: $(LUA_OBJ) $(LUA_SRCDIR)/action_helper.pl $(call PRINTS,GEN $(@F))$(CC) $(PLUGINFLAGS) $(INCLUDES) -E -P $(APPSDIR)/plugins/lib/pluginlib_actions.h | $(LUA_SRCDIR)/action_helper.pl > $(LUA_BUILDDIR)/actions.lua +$(LUA_BUILDDIR)/rb_defines.lua: $(LUA_OBJ) $(LUA_SRCDIR)/rbdefines_helper.pl + $(SILENT)$(CC) $(INCLUDES) -dD -E -P $(TARGET) $(CFLAGS) -include plugin.h - < /dev/null | $(LUA_SRCDIR)/rbdefines_helper.pl | \ + $(HOSTCC) -fno-builtin $(HOST_INCLUDES) -x c -o $(LUA_BUILDDIR)/rbdefines_helper - + $(call PRINTS,GEN $(@F))$(LUA_BUILDDIR)/rbdefines_helper > $(LUA_BUILDDIR)/rb_defines.lua + $(LUA_BUILDDIR)/settings.lua: $(LUA_OBJ) $(LUA_SRCDIR)/settings_helper.pl $(SILENT)$(CC) $(INCLUDES) -E -P $(TARGET) $(CFLAGS) -include plugin.h -include cuesheet.h - < /dev/null | $(LUA_SRCDIR)/settings_helper.pl | \ $(CC) $(INCLUDES) $(TARGET) $(CFLAGS) -S -x c -include config.h -include plugin.h -o $(LUA_BUILDDIR)/settings_helper.s - diff --git a/apps/plugins/lua/rbdefines_helper.pl b/apps/plugins/lua/rbdefines_helper.pl new file mode 100755 index 0000000000..671936962e --- /dev/null +++ b/apps/plugins/lua/rbdefines_helper.pl @@ -0,0 +1,199 @@ +#!/usr/bin/env perl +############################################################################ +# __________ __ ___. +# Open \______ \ ____ ____ | | _\_ |__ _______ ___ +# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +# \/ \/ \/ \/ \/ +# $Id$ +# +# Copyright (C) 2019 by William Wilgus +# +# All files in this archive are subject to the GNU General Public License. +# See the file COPYING in the source tree root for full license agreement. +# +# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY +# KIND, either express or implied. +# +############################################################################ + +#rockbox to lua define generator, add names of constants to the array to include + +my @rockbox_defines = ( + '^HZ$', + '^LCD_(DEPTH|HEIGHT|WIDTH)$', + '^MODEL_NAME$', + '^SCREEN_MAIN$', + '^LCD_DEFAULT_(FG|BG)$', + '^LCD_REMOTE_(DEPTH|HEIGHT|WIDTH)$', + '^LCD_.+(BRIGHT|DARK)COLOR', + '^SCREEN_REMOTE$', + '^FONT_SYSFIXED$', + '^FONT_UI$', + '^PLAYLIST_(INSERT|PREPEND|REPLACE)', + '^TOUCHSCREEN_(POINT|BUTTON)$', + '^HOME_DIR$', + '^PLUGIN_DIR$', + '^PLUGIN(_APPS_|_GAMES_|_)DATA_DIR$', + '^ROCKBOX_DIR$', + '^VIEWERS_DATA_DIR$'); + +my @captured_defines; +my @names_seen; +my @all_defines; + +############# precompile regex for speed ############# +my $def_regex = qr/#define\s+([^\s\r\n]+)\s+([^\r\n]+)/; +my $quot_regex = qr/.*([\"\']).*/; +my $num_regex = qr/.*([\+\-\*\\|&\d]).*/; + +print < +#include + +#define stringify(s) #s + +/* auto generated defines */ + + +EOF +; + +while(my $line = ) +{ + + if($line =~ $def_regex) #does it begin with #define? + { + push(@all_defines, $line); #save all defines for possible ambiguous macros + $name = $1; + next if $name =~ /^(ATTRIBUTE_|print|__).*/; #don't add reserved + $value = $2; + + if(grep($name =~ $_, @rockbox_defines)) + { + push(@names_seen, $name); + push(@captured_defines, {'name' => $name, 'value' => $value}); + print $line + } + else + { + $name =~ s{(\(.*\))}{}gx; #remove (...) on function type macros + #ifndef guard so we don't clash with the host + printf "#ifndef %s\n%s#endif\n\n", $name, $line; + } + } + elsif($line =~ /^(?!.*;)enum.*{/) #enum { + { + print $line; + next if($line =~ /.*};.*/); + do_enum($line) + } + elsif($line =~ /^(?!.*[;\(\)])enum.*/) #enum + { + #need to be careful might be a function returning enum + my $lastline = $line; + next if($line =~ /.*};.*/); + ($line = ); + if($line =~ /.*{.*/) + { + print $lastline; + print $line; + } + else { next; } + do_enum($line) + } + +} + +#Sort the functions +my @sorted_defines = sort { @$a{'name'} cmp @$b{'name'} } @captured_defines; + +printf "int main(void)\n{\n"; +printf "\tprintf(\"--[[Autogenerated rockbox constants]]\\n\\n\");"; +# Print the C array +foreach my $define (@sorted_defines) +{ + if(@$define{'value'} =~ /^0[xX][0-9a-fA-F]+$/) #hex number + { + printf "\tprintf(\"rb[\\\"%%s\\\"] = 0x%%x\\n\", stringify(%s), %s);\n", @$define{'name'}, @$define{'name'}; + } + elsif(@$define{'value'} =~ /^[0-9]+$/) #number + { + printf "\tprintf(\"rb[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", @$define{'name'}, @$define{'name'}; + } + else #might be a string but we don't know since the macro isn't expanded far enough + { + my $max_depth = 10; + my $var = @$define{'value'}; + while($max_depth > 0) #follow the chain of #defines until we can see what type + { + $max_depth--; + $var = "\\\s*#define\\s+$var\\s+.*"; + #warn $var; + if(my ($x) = grep($_ =~ /($var)/, @all_defines)) #check all the defines + { + #warn $x; + if($x =~ $def_regex) + { + $var = $2; + #warn $var; + last if ($var =~ $quot_regex); + last if ($var =~ $num_regex); + next + } + #warn "end ".$var; + last + } + else + { + $var = @$define{'value'}; + last + } + } + if ($var =~$quot_regex) #has a quote it is a string + { + #guard with empty literals "" so gcc throws an error if it isn't a string + printf "\tprintf(\"rb[\\\"%%s\\\"] = \\\"%%s\\\"\\n\", stringify(%s), \"\" %s \"\");\n", @$define{'name'}, @$define{'name'}; + } + elsif ($var =~$num_regex) #it must be a number + { + printf "\tprintf(\"rb[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", @$define{'name'}, @$define{'name'}; + } + else { warn "Skipping ".@$define{'name'}." indeterminate macro type\n"; } + } +} + +print <) + { + + if($line =~ /.*};.*/) + { + print $line; + last + } + elsif($line =~ /([^\s,\t]+)\s*=?.*,?/) + { + $name = $1; + #printf "%s WHATTT?", $name; + $value = "0"; #enums are always integers + } + print $line; + if(grep($name =~ $_, @rockbox_defines)) + { + push(@names_seen, $name); + push(@captured_defines, {'name' => $name, 'value' => $value}); + } + + } +} diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index 426dd079af..1d20989009 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c @@ -793,7 +793,11 @@ LUALIB_API int luaopen_rock(lua_State *L) { luaL_register(L, LUA_ROCKLIBNAME, rocklib); luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux); - + lua_getglobal(L, "require"); + lua_pushstring(L, "rb_defines"); + if (lua_pcall (L, 1, 0, 0)) + lua_pop(L, 1); +#if 0 static const struct lua_int_reg rlib_const_int[] = { /* useful integer constants */ @@ -860,7 +864,7 @@ LUALIB_API int luaopen_rock(lua_State *L) luaS_newlloc(L, rlcs->name, TSTR_INBIN); lua_setfield(L, -2, rlcs->name); } - +#endif return 1; } diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c index af87a0bfd7..eec6ee54f9 100644 --- a/apps/plugins/lua/rocklua.c +++ b/apps/plugins/lua/rocklua.c @@ -31,15 +31,15 @@ static const luaL_Reg lualibs[] = { {"", luaopen_base}, + {LUA_LOADLIBNAME, luaopen_package}, {LUA_TABLIBNAME, luaopen_table}, {LUA_STRLIBNAME, luaopen_string}, - {LUA_OSLIBNAME, luaopen_os}, - {LUA_ROCKLIBNAME, luaopen_rock}, - {LUA_ROCKLIBNAME, luaopen_rock_img}, {LUA_BITLIBNAME, luaopen_bit}, {LUA_IOLIBNAME, luaopen_io}, - {LUA_LOADLIBNAME, luaopen_package}, {LUA_MATHLIBNAME, luaopen_math}, + {LUA_OSLIBNAME, luaopen_os}, + {LUA_ROCKLIBNAME, luaopen_rock}, + {LUA_ROCKLIBNAME, luaopen_rock_img}, {LUA_DIRLIBNAME, luaopen_luadir}, {NULL, NULL} }; -- cgit v1.2.3