• R/O
  • SSH
  • HTTPS

eirlua: コミット


コミットメタ情報

リビジョン10 (tree)
日時2018-02-08 06:00:53
作者quiret

ログメッセージ

- removed windows header from global includes (it is not required anymore)
- added NativeExecutive to build solution
- GCC compatibility adjustments
- readded support for precompiled header

変更サマリ

差分

--- src/print.cpp (revision 9)
+++ src/print.cpp (nonexistent)
@@ -1,311 +0,0 @@
1-/*
2-** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
3-** print bytecodes
4-** See Copyright Notice in lua.h
5-*/
6-
7-#include "luacore.h"
8-
9-#include "ldebug.h"
10-#include "lobject.h"
11-#include "lopcodes.h"
12-#include "lundump.h"
13-
14-#include <iostream>
15-
16-#define Sizeof(x) ((int)sizeof(x))
17-#define CVOID(p) ((const void*)(p))
18-
19-static void PrintString(const TString* ts)
20-{
21- const char* s=getstr(ts);
22- size_t i,n=ts->len;
23- putchar('"');
24- for (i=0; i<n; i++)
25- {
26- int c=s[i];
27- switch (c)
28- {
29- case '"': printf("\\\""); break;
30- case '\\': printf("\\\\"); break;
31- case '\a': printf("\\a"); break;
32- case '\b': printf("\\b"); break;
33- case '\f': printf("\\f"); break;
34- case '\n': printf("\\n"); break;
35- case '\r': printf("\\r"); break;
36- case '\t': printf("\\t"); break;
37- case '\v': printf("\\v"); break;
38- default: if (isprint((unsigned char)c))
39- putchar(c);
40- else
41- printf("\\%03u",(unsigned char)c);
42- }
43- }
44- putchar('"');
45-}
46-
47-static void PrintConstant( const Proto* f, size_t i )
48-{
49- const TValue *o = &f->k[i];
50-
51- switch( ttype(o) )
52- {
53- case LUA_TNIL:
54- printf( "nil" );
55- break;
56- case LUA_TBOOLEAN:
57- printf( bvalue(o) ? "true" : "false" );
58- break;
59- case LUA_TNUMBER:
60- printf( LUA_NUMBER_FMT, nvalue(o) );
61- break;
62- case LUA_TSTRING:
63- PrintString( tsvalue(o) );
64- break;
65- default: /* cannot happen */
66- printf( "? type=%d", ttype(o) );
67- break;
68- }
69-}
70-
71-static void PrintCode(const Proto* f)
72-{
73- const Instruction *code = f->code;
74-
75- size_t n = f->sizecode;
76-
77- for ( size_t pc = 0; pc < n; pc++ )
78- {
79- Instruction i = code[pc];
80-
81- OpCode o = GET_OPCODE(i);
82-
83- int a = GETARG_A(i);
84- int b = GETARG_B(i);
85- int c = GETARG_C(i);
86- int bx = GETARG_Bx(i);
87- int sbx = GETARG_sBx(i);
88- int line = getline( f, pc );
89-
90- std::cout << "\t" << ( pc + 1 ) << "\t";
91-
92- if ( line > 0 )
93- {
94- printf( "[%d]\t", line );
95- }
96- else
97- {
98- printf("[-]\t");
99- }
100-
101- printf( "%-9s\t", luaP_getopname(o) );
102-
103- switch (getOpMode(o))
104- {
105- case iABC:
106- printf("%d",a);
107-
108- if ( getBMode(o) != OpArgN )
109- {
110- printf( " %d",ISK(b) ? (-1-INDEXK(b)) : b );
111- }
112- if ( getCMode(o) != OpArgN )
113- {
114- printf( " %d",ISK(c) ? (-1-INDEXK(c)) : c );
115- }
116- break;
117- case iABx:
118- if ( getBMode(o) == OpArgK )
119- {
120- printf( "%d %d", a, -1-bx );
121- }
122- else
123- {
124- printf( "%d %d", a, bx );
125- }
126- break;
127- case iAsBx:
128- if ( o == OP_JMP )
129- {
130- printf( "%d", sbx );
131- }
132- else
133- {
134- printf( "%d %d", a, sbx );
135- }
136- break;
137- }
138-
139- switch (o)
140- {
141- case OP_LOADK:
142- printf( "\t; " );
143- PrintConstant( f, bx );
144- break;
145- case OP_GETUPVAL:
146- case OP_SETUPVAL:
147- printf( "\t; %s", ( f->sizeupvalues > 0) ? getstr( f->upvalues[b] ) : "-" );
148- break;
149- case OP_GETGLOBAL:
150- case OP_SETGLOBAL:
151- printf( "\t; %s", svalue(&f->k[bx]) );
152- break;
153- case OP_GETTABLE:
154- case OP_SELF:
155- if (ISK(c))
156- {
157- printf( "\t; " );
158- PrintConstant( f, INDEXK(c) );
159- }
160- break;
161- case OP_SETTABLE:
162- case OP_ADD:
163- case OP_SUB:
164- case OP_MUL:
165- case OP_DIV:
166- case OP_POW:
167- case OP_EQ:
168- case OP_LT:
169- case OP_LE:
170- if ( ISK(b) || ISK(c) )
171- {
172- printf("\t; ");
173-
174- if ( ISK(b) )
175- {
176- PrintConstant( f, INDEXK(b) );
177- }
178- else
179- {
180- printf("-");
181- }
182-
183- printf(" ");
184-
185- if ( ISK(c) )
186- {
187- PrintConstant( f, INDEXK(c) );
188- }
189- else
190- {
191- printf("-");
192- }
193- }
194- break;
195- case OP_JMP:
196- case OP_FORLOOP:
197- case OP_FORPREP:
198- std::cout << "\t; to " << ( sbx + pc + 2 );
199- break;
200- case OP_CLOSURE:
201- printf( "\t; %p", CVOID( f->p[bx] ) );
202- break;
203- case OP_SETLIST:
204- if ( c == 0 )
205- {
206- printf( "\t; %d", (int)code[ ++pc ] );
207- }
208- else
209- {
210- printf( "\t; %d", c );
211- }
212- break;
213- default:
214- break;
215- }
216-
217- printf("\n");
218- }
219-}
220-
221-#define SS(x) (x==1)?"":"s"
222-#define S(x) x,SS(x)
223-
224-static void PrintHeader(const Proto* f)
225-{
226- const char* s=getstr(f->source);
227- if (*s=='@' || *s=='=')
228- s++;
229- else if (*s==LUA_SIGNATURE[0])
230- s="(bstring)";
231- else
232- s="(string)";
233- printf("\n%s <%s:%d,%d> (%zu instruction%s, %zu bytes at %p)\n",
234- (f->linedefined==0)?"main":"function",s,
235- f->linedefined,f->lastlinedefined,
236- S(f->sizecode),f->sizecode*Sizeof(Instruction),CVOID(f));
237- printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
238- f->numparams,f->is_vararg?"+":"",SS(f->numparams),
239- S(f->maxstacksize),S(f->nups));
240- printf("%zu local%s, %zu constant%s, %zu function%s\n",
241- S(f->sizelocvars),S(f->sizek),S(f->sizep));
242-}
243-
244-static void PrintConstants( const Proto* f )
245-{
246- size_t n = f->sizek;
247-
248- printf( "constants (%zu) for %p:\n", n, CVOID(f) );
249-
250- for ( size_t i = 0; i < n; i++ )
251- {
252- printf( "\t%zu\t", i+1 );
253-
254- PrintConstant( f, i );
255-
256- printf( "\n" );
257- }
258-}
259-
260-static void PrintLocals( const Proto* f )
261-{
262- size_t n = f->sizelocvars;
263-
264- printf( "locals (%zu) for %p:\n", n, CVOID(f) );
265-
266- for ( size_t i = 0; i < n; i++ )
267- {
268- printf(
269- "\t%zu\t%s\t%zu\t%zu\n",
270- i,
271- getstr( f->locvars[i].varname ),
272- f->locvars[i].startpc + 1,
273- f->locvars[i].endpc + 1
274- );
275- }
276-}
277-
278-static void PrintUpvalues( const Proto* f )
279-{
280- size_t n = f->sizeupvalues;
281-
282- printf( "upvalues (%zu) for %p:\n", n, CVOID(f) );
283-
284- if ( f->upvalues == NULL )
285- return;
286-
287- for ( size_t i = 0; i < n; i++ )
288- {
289- printf( "\t%zu\t%s\n", i, getstr(f->upvalues[i]) );
290- }
291-}
292-
293-void luaU_print( const Proto* f, bool full )
294-{
295- PrintHeader(f);
296- PrintCode(f);
297-
298- if ( full )
299- {
300- PrintConstants(f);
301- PrintLocals(f);
302- PrintUpvalues(f);
303- }
304-
305- size_t n = f->sizep;
306-
307- for ( size_t i = 0; i < n; i++ )
308- {
309- luaU_print( f->p[i], full );
310- }
311-}
--- src/lauxlib.cpp (revision 9)
+++ src/lauxlib.cpp (revision 10)
@@ -218,7 +218,7 @@
218218 return luaL_checklstring(L, narg, len);
219219 }
220220
221-LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg)
221+LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg)
222222 {
223223 lua_Number d = lua_tonumber(L, narg);
224224
@@ -621,44 +621,80 @@
621621 }
622622
623623
624-LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
625- LoadF lf;
626- int status, readstatus;
627- int c;
628- int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
629- lf.extraline = 0;
630- if (filename == NULL) {
631- lua_pushliteral(L, "=stdin");
632- lf.f = stdin;
633- }
634- else {
635- lua_pushfstring(L, "@%s", filename);
636- lf.f = fopen(filename, "r");
637- if (lf.f == NULL) return errfile(L, "open", fnameindex);
638- }
639- c = getc(lf.f);
640- if (c == '#') { /* Unix exec. file? */
641- lf.extraline = 1;
642- while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
643- if (c == '\n') c = getc(lf.f);
644- }
645- if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
646- lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
647- if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
648- /* skip eventual `#!...' */
649- while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
624+LUALIB_API int luaL_loadfile (lua_State *L, const char *filename)
625+{
626+ LoadF lf;
627+ int status, readstatus;
628+ int c;
629+ int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
630+
650631 lf.extraline = 0;
651- }
652- ungetc(c, lf.f);
653- status = lua_load(L, getF, &lf, lua_tostring(L, -1));
654- readstatus = ferror(lf.f);
655- if (filename) fclose(lf.f); /* close file (even in case of errors) */
656- if (readstatus) {
657- lua_settop(L, fnameindex); /* ignore results from `lua_load' */
658- return errfile(L, "read", fnameindex);
659- }
660- lua_remove(L, fnameindex);
661- return status;
632+
633+ if (filename == NULL)
634+ {
635+ lua_pushliteral(L, "=stdin");
636+ lf.f = stdin;
637+ }
638+ else
639+ {
640+ lua_pushfstring(L, "@%s", filename);
641+
642+ lf.f = fopen(filename, "r");
643+
644+ if (lf.f == NULL)
645+ {
646+ return errfile(L, "open", fnameindex);
647+ }
648+ }
649+
650+ c = getc(lf.f);
651+
652+ if (c == '#')
653+ { /* Unix exec. file? */
654+ lf.extraline = 1;
655+ while ((c = getc(lf.f)) != EOF && c != '\n')
656+ {} /* skip first line */
657+ if (c == '\n')
658+ {
659+ c = getc(lf.f);
660+ }
661+ }
662+
663+ if (c == LUA_SIGNATURE[0] && filename)
664+ { /* binary file? */
665+ lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
666+
667+ if (lf.f == NULL)
668+ {
669+ return errfile(L, "reopen", fnameindex);
670+ }
671+
672+ /* skip eventual `#!...' */
673+ while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0])
674+ { }
675+
676+ lf.extraline = 0;
677+ }
678+
679+ ungetc(c, lf.f);
680+
681+ status = lua_load(L, getF, &lf, lua_tostring(L, -1));
682+
683+ readstatus = ferror(lf.f);
684+
685+ if (filename)
686+ {
687+ fclose(lf.f); /* close file (even in case of errors) */
688+ }
689+
690+ if (readstatus)
691+ {
692+ lua_settop(L, fnameindex); /* ignore results from `lua_load' */
693+ return errfile(L, "read", fnameindex);
694+ }
695+
696+ lua_remove(L, fnameindex);
697+ return status;
662698 }
663699
664700
@@ -707,13 +743,15 @@
707743 return realloc(ptr, nsize);
708744 }
709745
710-
711-static int panic (lua_State *L) {
746+#if 0
747+static int panic (lua_State *L)
748+{
712749 (void)L; /* to avoid warnings */
713750 fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
714751 lua_tostring(L, -1));
715752 return 0;
716753 }
754+#endif //0
717755
718756 LUALIB_API lua_State* luaL_newstate()
719757 {
@@ -723,4 +761,4 @@
723761 LUALIB_API lua_config* luaL_newconfig()
724762 {
725763 return lua_newconfig(l_alloc, NULL);
726-}
\ No newline at end of file
764+}
--- src/llimits.h (revision 9)
+++ src/llimits.h (revision 10)
@@ -61,7 +61,11 @@
6161
6262 #if defined(_DEBUG) || defined(_LUA_RUNTIME_FEEDBACK)
6363 #include "assert.h"
64+#ifdef _MSC_VER
6465 #define lua_assert(x) { if ( !(x) ) __debugbreak(); }
66+#else
67+#define lua_assert(x) assert( x )
68+#endif //COMPILER DEPENDANT CODE
6569 #define check_exp(c,e) (e)
6670 #define api_check(L,x) lua_assert(x)
6771 #else
@@ -114,7 +118,7 @@
114118
115119
116120 #ifndef lua_lock
117-#define lua_lock(L) ((void) 0)
121+#define lua_lock(L) ((void) 0)
118122 #define lua_unlock(L) ((void) 0)
119123 #endif
120124
@@ -125,7 +129,7 @@
125129
126130 /*
127131 ** macro to control inclusion of some hard tests on stack reallocation
128-*/
132+*/
129133 #ifndef HARDSTACKTESTS
130134 #define condhardstacktests(x) ((void)0)
131135 #else
--- src/loadlib.cpp (revision 9)
+++ src/loadlib.cpp (revision 10)
@@ -82,6 +82,8 @@
8282 ** =======================================================================
8383 */
8484
85+#define WIN32_LEAN_AND_MEAN
86+#define NOMINMAX
8587 #include <windows.h>
8688
8789
--- src/lobject.h (revision 9)
+++ src/lobject.h (revision 10)
@@ -1623,7 +1623,7 @@
16231623 TValue theValue;
16241624 TValue *theValuePtr;
16251625
1626- inline LocalValueAddress( void ) : theValuePtr( &theValue ), ValueAddress( &theValuePtr )
1626+ inline LocalValueAddress( void ) : ValueAddress( &theValuePtr ), theValuePtr( &theValue )
16271627 {
16281628 return;
16291629 }
--- src/lopcodes.cpp (revision 9)
+++ src/lopcodes.cpp (revision 10)
@@ -3,6 +3,7 @@
33 ** See Copyright Notice in lua.h
44 */
55
6+#include "luacore.h"
67
78 #define lopcodes_c
89 #define LUA_CORE
--- src/loslib.cpp (revision 9)
+++ src/loslib.cpp (revision 10)
@@ -4,8 +4,6 @@
44 ** See Copyright Notice in lua.h
55 */
66
7-#define loslib_c
8-
97 #include "luacore.h"
108
119 #include <errno.h>
--- src/lstack.h (revision 9)
+++ src/lstack.h (revision 10)
@@ -237,14 +237,16 @@
237237 lua_assert( this->base == NULL );
238238 }
239239
240- inline void Initialize( lua_State *L, stackOffset_t stackSize )
240+ inline void Initialize( global_State *g, lua_State *L, stackOffset_t stackSize )
241241 {
242+ UNUSED(g);
243+
242244 SetSize( L, stackSize );
243245
244246 // TODO: add more initializers.
245247 }
246248
247- inline void Shutdown( lua_State *L )
249+ inline void Shutdown( global_State *g, lua_State *L )
248250 {
249251 // Invalidate all context items that still belong to this thread.
250252 LIST_FOREACH_BEGIN( CtxItem, m_contextItems.root, node )
@@ -262,8 +264,6 @@
262264 SetSize( L, 0 );
263265
264266 // Call shutdown on acceleration structures.
265- global_State *g = L->gstate;
266-
267267 _cachedVirtualStackItem.Shutdown( g );
268268 _cachedVirtualStackItemConst.Shutdown( g );
269269 }
--- src/lstate.cpp (revision 9)
+++ src/lstate.cpp (revision 10)
@@ -144,11 +144,13 @@
144144 // Link the stacks to the runtime thread.
145145 L1->rtStack.runtimeThread = L1;
146146
147+ global_State *g = G(L);
148+
147149 /* initialize CallInfo array */
148- ciStack->Initialize( L, BASIC_CI_SIZE );
150+ ciStack->Initialize( g, L, BASIC_CI_SIZE );
149151 CallInfo *mainFrame = ciStack->ObtainItem( L ); // there must be an item on the ci stack.
150152 /* initialize stack array */
151- rtStack->Initialize( L, BASIC_STACK_SIZE );
153+ rtStack->Initialize( g, L, BASIC_STACK_SIZE );
152154 StkId firstStackValue = rtStack->ObtainItem( L ); // there must be an item on the rt stack.
153155 /* initialize first ci */
154156 mainFrame->func = firstStackValue;
@@ -159,8 +161,8 @@
159161
160162 static void freestack (lua_State *L, lua_State *L1)
161163 {
162- L1->ciStack.Shutdown( L );
163- L1->rtStack.Shutdown( L );
164+ L1->ciStack.Shutdown( G(L), L );
165+ L1->rtStack.Shutdown( G(L), L );
164166 }
165167
166168
--- src/lstate.h (revision 9)
+++ src/lstate.h (revision 10)
@@ -7,12 +7,6 @@
77 #ifndef lstate_h
88 #define lstate_h
99
10-#ifdef _WIN32
11-#define WIN32_LEAN_AND_MEAN
12-#define NOMINMAX
13-#include <windows.h>
14-#endif //_WIN32
15-
1610 #include "lua.h"
1711
1812 #include "lapi.h"
--- src/ltable.cpp (revision 9)
+++ src/ltable.cpp (revision 10)
@@ -293,9 +293,9 @@
293293 }
294294 }
295295
296- int sizenode = sizenode(nativeTable);
296+ size_t sizen = sizenode(nativeTable);
297297
298- for ( idx -= sizearray; idx < sizenode; idx++ )
298+ for ( idx -= sizearray; idx < sizen; idx++ )
299299 { /* then hash part */
300300 Node *curNode = gnode(nativeTable, idx);
301301 TValue *curItem = gval(curNode);
--- src/ltable.hxx (revision 9)
+++ src/ltable.hxx (revision 10)
@@ -648,7 +648,9 @@
648648
649649 // Copy construct it.
650650 tableNativeImplementation *nativeImpl =
651- new (advObjMem) tableNativeImplementation( *(const tableNativeImplementation*)srcAdvObjMem, srcTable->gstate );
651+ new (advObjMem) tableNativeImplementation( *(const tableNativeImplementation*)srcAdvObjMem, srcTable->gstate );
652+
653+ (void)nativeImpl;
652654
653655 // Fin.
654656 }
--- src/ltablib.cpp (revision 9)
+++ src/ltablib.cpp (revision 10)
@@ -127,12 +127,16 @@
127127 }
128128
129129
130-static void addfield (lua_State *L, luaL_Buffer *b, int i) {
131- lua_rawgeti(L, 1, i);
132- if (!lua_isstring(L, -1))
133- luaL_error(L, "invalid value (%s) at index %d in table for "
134- LUA_QL("concat"), luaL_typename(L, -1), i);
135- luaL_addvalue(b);
130+static void addfield (lua_State *L, luaL_Buffer *b, int i)
131+{
132+ lua_rawgeti( L, 1, i );
133+
134+ if ( !lua_isstring(L, -1) )
135+ {
136+ luaL_error( L, "invalid value (%s) at index %d in table for " LUA_QL("concat"), luaL_typename(L, -1), i );
137+ }
138+
139+ luaL_addvalue( b );
136140 }
137141
138142
--- src/ltm.cpp (revision 9)
+++ src/ltm.cpp (revision 10)
@@ -168,7 +168,7 @@
168168 return retAddr;
169169 }
170170
171-inline Table* luaT_getmetabyobj( lua_State *L, const TValue *o )
171+Table* luaT_getmetabyobj( lua_State *L, const TValue *o )
172172 {
173173 switch( ttype(o) )
174174 {
--- src/ltypelib.cpp (revision 9)
+++ src/ltypelib.cpp (revision 10)
@@ -154,7 +154,7 @@
154154
155155 if ( const char *typeName = iter.typeName )
156156 {
157- lua_pushstring( L, iter.typeName );
157+ lua_pushstring( L, typeName );
158158 }
159159 else
160160 {
@@ -165,7 +165,7 @@
165165 {
166166 lua_pushboolean( L, false );
167167 }
168-
168+
169169 return 1;
170170 }
171171
@@ -261,7 +261,7 @@
261261 LuaTypeSystem& typeSys = g->config->typeSys;
262262
263263 GCObject *gcObj = gcvalue( givenObj );
264-
264+
265265 GCObject *newObj = NULL;
266266 {
267267 const LuaRTTI *rtSrcObj = LuaTypeSystem::GetTypeStructFromObject( gcObj );
@@ -422,4 +422,4 @@
422422
423423 // Return NULL just to make the compiler happy.
424424 return NULL;
425-}
\ No newline at end of file
425+}
--- src/luaconf.h (revision 9)
+++ src/luaconf.h (revision 10)
@@ -780,8 +780,6 @@
780780 ** insecure) or if you want the original tmpnam anyway. By default, Lua
781781 ** uses tmpnam except when POSIX is available, where it uses mkstemp.
782782 */
783-#if defined(loslib_c) || defined(luaall_c)
784-
785783 #if defined(LUA_USE_MKSTEMP)
786784 #include <unistd.h>
787785 #define LUA_TMPNAMBUFSIZE 32
@@ -796,9 +794,7 @@
796794 #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
797795 #endif
798796
799-#endif
800797
801-
802798 /*
803799 @@ lua_popen spawns a new process connected to the current one through
804800 @* the file streams.
--- src/luacore.h (revision 9)
+++ src/luacore.h (revision 10)
@@ -2,10 +2,4 @@
22 // Can be turned into a precompiled header for performance optimizations.
33
44 #define LUA_CORE
5-#include "lua.h"
6-
7-#ifdef _WIN32
8-#define WIN32_LEAN_AND_MEAN
9-#define NOMINMAX
10-#include <windows.h>
11-#endif
\ No newline at end of file
5+#include "lua.h"
\ No newline at end of file
--- src/lvm.cpp (revision 9)
+++ src/lvm.cpp (revision 10)
@@ -540,10 +540,12 @@
540540 void lua_debugLClosureStack( lua_State *L, CallInfo *currentFrame )
541541 {
542542 stackOffset_t currentStackTop = currentFrame->stack.GetUsageCount( L, L->rtStack );
543+ (void)currentStackTop;
543544
544545 LClosure *lcl = clvalue( currentFrame->func )->GetLClosure();
545546
546547 stackOffset_t requiredStackTop = lcl->p->maxstacksize;
548+ (void)requiredStackTop;
547549
548550 lua_assert( currentStackTop == requiredStackTop );
549551 }
@@ -981,6 +983,9 @@
981983 }
982984 break;
983985 }
986+ default:
987+ // GCC is retarded.
988+ break;
984989 }
985990 }
986991 else
@@ -1301,6 +1306,9 @@
13011306 Protect(luaC_checkGC(L));
13021307 break;
13031308 }
1309+ default:
1310+ // GCC is stupid.
1311+ break;
13041312 }
13051313 }
13061314
--- src/print.cxx (nonexistent)
+++ src/print.cxx (revision 10)
@@ -0,0 +1,311 @@
1+/*
2+** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
3+** print bytecodes
4+** See Copyright Notice in lua.h
5+*/
6+
7+#include "luacore.h"
8+
9+#include "ldebug.h"
10+#include "lobject.h"
11+#include "lopcodes.h"
12+#include "lundump.h"
13+
14+#include <iostream>
15+
16+#define Sizeof(x) ((int)sizeof(x))
17+#define CVOID(p) ((const void*)(p))
18+
19+static void PrintString(const TString* ts)
20+{
21+ const char* s=getstr(ts);
22+ size_t i,n=ts->len;
23+ putchar('"');
24+ for (i=0; i<n; i++)
25+ {
26+ int c=s[i];
27+ switch (c)
28+ {
29+ case '"': printf("\\\""); break;
30+ case '\\': printf("\\\\"); break;
31+ case '\a': printf("\\a"); break;
32+ case '\b': printf("\\b"); break;
33+ case '\f': printf("\\f"); break;
34+ case '\n': printf("\\n"); break;
35+ case '\r': printf("\\r"); break;
36+ case '\t': printf("\\t"); break;
37+ case '\v': printf("\\v"); break;
38+ default: if (isprint((unsigned char)c))
39+ putchar(c);
40+ else
41+ printf("\\%03u",(unsigned char)c);
42+ }
43+ }
44+ putchar('"');
45+}
46+
47+static void PrintConstant( const Proto* f, size_t i )
48+{
49+ const TValue *o = &f->k[i];
50+
51+ switch( ttype(o) )
52+ {
53+ case LUA_TNIL:
54+ printf( "nil" );
55+ break;
56+ case LUA_TBOOLEAN:
57+ printf( bvalue(o) ? "true" : "false" );
58+ break;
59+ case LUA_TNUMBER:
60+ printf( LUA_NUMBER_FMT, nvalue(o) );
61+ break;
62+ case LUA_TSTRING:
63+ PrintString( tsvalue(o) );
64+ break;
65+ default: /* cannot happen */
66+ printf( "? type=%d", ttype(o) );
67+ break;
68+ }
69+}
70+
71+static void PrintCode(const Proto* f)
72+{
73+ const Instruction *code = f->code;
74+
75+ size_t n = f->sizecode;
76+
77+ for ( size_t pc = 0; pc < n; pc++ )
78+ {
79+ Instruction i = code[pc];
80+
81+ OpCode o = GET_OPCODE(i);
82+
83+ int a = GETARG_A(i);
84+ int b = GETARG_B(i);
85+ int c = GETARG_C(i);
86+ int bx = GETARG_Bx(i);
87+ int sbx = GETARG_sBx(i);
88+ int line = getline( f, pc );
89+
90+ std::cout << "\t" << ( pc + 1 ) << "\t";
91+
92+ if ( line > 0 )
93+ {
94+ printf( "[%d]\t", line );
95+ }
96+ else
97+ {
98+ printf("[-]\t");
99+ }
100+
101+ printf( "%-9s\t", luaP_getopname(o) );
102+
103+ switch (getOpMode(o))
104+ {
105+ case iABC:
106+ printf("%d",a);
107+
108+ if ( getBMode(o) != OpArgN )
109+ {
110+ printf( " %d",ISK(b) ? (-1-INDEXK(b)) : b );
111+ }
112+ if ( getCMode(o) != OpArgN )
113+ {
114+ printf( " %d",ISK(c) ? (-1-INDEXK(c)) : c );
115+ }
116+ break;
117+ case iABx:
118+ if ( getBMode(o) == OpArgK )
119+ {
120+ printf( "%d %d", a, -1-bx );
121+ }
122+ else
123+ {
124+ printf( "%d %d", a, bx );
125+ }
126+ break;
127+ case iAsBx:
128+ if ( o == OP_JMP )
129+ {
130+ printf( "%d", sbx );
131+ }
132+ else
133+ {
134+ printf( "%d %d", a, sbx );
135+ }
136+ break;
137+ }
138+
139+ switch (o)
140+ {
141+ case OP_LOADK:
142+ printf( "\t; " );
143+ PrintConstant( f, bx );
144+ break;
145+ case OP_GETUPVAL:
146+ case OP_SETUPVAL:
147+ printf( "\t; %s", ( f->sizeupvalues > 0) ? getstr( f->upvalues[b] ) : "-" );
148+ break;
149+ case OP_GETGLOBAL:
150+ case OP_SETGLOBAL:
151+ printf( "\t; %s", svalue(&f->k[bx]) );
152+ break;
153+ case OP_GETTABLE:
154+ case OP_SELF:
155+ if (ISK(c))
156+ {
157+ printf( "\t; " );
158+ PrintConstant( f, INDEXK(c) );
159+ }
160+ break;
161+ case OP_SETTABLE:
162+ case OP_ADD:
163+ case OP_SUB:
164+ case OP_MUL:
165+ case OP_DIV:
166+ case OP_POW:
167+ case OP_EQ:
168+ case OP_LT:
169+ case OP_LE:
170+ if ( ISK(b) || ISK(c) )
171+ {
172+ printf("\t; ");
173+
174+ if ( ISK(b) )
175+ {
176+ PrintConstant( f, INDEXK(b) );
177+ }
178+ else
179+ {
180+ printf("-");
181+ }
182+
183+ printf(" ");
184+
185+ if ( ISK(c) )
186+ {
187+ PrintConstant( f, INDEXK(c) );
188+ }
189+ else
190+ {
191+ printf("-");
192+ }
193+ }
194+ break;
195+ case OP_JMP:
196+ case OP_FORLOOP:
197+ case OP_FORPREP:
198+ std::cout << "\t; to " << ( sbx + pc + 2 );
199+ break;
200+ case OP_CLOSURE:
201+ printf( "\t; %p", CVOID( f->p[bx] ) );
202+ break;
203+ case OP_SETLIST:
204+ if ( c == 0 )
205+ {
206+ printf( "\t; %d", (int)code[ ++pc ] );
207+ }
208+ else
209+ {
210+ printf( "\t; %d", c );
211+ }
212+ break;
213+ default:
214+ break;
215+ }
216+
217+ printf("\n");
218+ }
219+}
220+
221+#define SS(x) (x==1)?"":"s"
222+#define S(x) x,SS(x)
223+
224+static void PrintHeader(const Proto* f)
225+{
226+ const char* s=getstr(f->source);
227+ if (*s=='@' || *s=='=')
228+ s++;
229+ else if (*s==LUA_SIGNATURE[0])
230+ s="(bstring)";
231+ else
232+ s="(string)";
233+ printf("\n%s <%s:%d,%d> (%zu instruction%s, %zu bytes at %p)\n",
234+ (f->linedefined==0)?"main":"function",s,
235+ f->linedefined,f->lastlinedefined,
236+ S(f->sizecode),f->sizecode*Sizeof(Instruction),CVOID(f));
237+ printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
238+ f->numparams,f->is_vararg?"+":"",SS(f->numparams),
239+ S(f->maxstacksize),S(f->nups));
240+ printf("%zu local%s, %zu constant%s, %zu function%s\n",
241+ S(f->sizelocvars),S(f->sizek),S(f->sizep));
242+}
243+
244+static void PrintConstants( const Proto* f )
245+{
246+ size_t n = f->sizek;
247+
248+ printf( "constants (%zu) for %p:\n", n, CVOID(f) );
249+
250+ for ( size_t i = 0; i < n; i++ )
251+ {
252+ printf( "\t%zu\t", i+1 );
253+
254+ PrintConstant( f, i );
255+
256+ printf( "\n" );
257+ }
258+}
259+
260+static void PrintLocals( const Proto* f )
261+{
262+ size_t n = f->sizelocvars;
263+
264+ printf( "locals (%zu) for %p:\n", n, CVOID(f) );
265+
266+ for ( size_t i = 0; i < n; i++ )
267+ {
268+ printf(
269+ "\t%zu\t%s\t%zu\t%zu\n",
270+ i,
271+ getstr( f->locvars[i].varname ),
272+ f->locvars[i].startpc + 1,
273+ f->locvars[i].endpc + 1
274+ );
275+ }
276+}
277+
278+static void PrintUpvalues( const Proto* f )
279+{
280+ size_t n = f->sizeupvalues;
281+
282+ printf( "upvalues (%zu) for %p:\n", n, CVOID(f) );
283+
284+ if ( f->upvalues == NULL )
285+ return;
286+
287+ for ( size_t i = 0; i < n; i++ )
288+ {
289+ printf( "\t%zu\t%s\n", i, getstr(f->upvalues[i]) );
290+ }
291+}
292+
293+void luaU_print( const Proto* f, bool full )
294+{
295+ PrintHeader(f);
296+ PrintCode(f);
297+
298+ if ( full )
299+ {
300+ PrintConstants(f);
301+ PrintLocals(f);
302+ PrintUpvalues(f);
303+ }
304+
305+ size_t n = f->sizep;
306+
307+ for ( size_t i = 0; i < n; i++ )
308+ {
309+ luaU_print( f->p[i], full );
310+ }
311+}
旧リポジトリブラウザで表示