瀏覽代碼

Refactored some enums to remove code duplication.

mingodad 13 年之前
父節點
當前提交
d04f15c243
共有 5 個文件被更改,包括 98 次插入203 次删除
  1. 11 1
      squirrel/sqcompiler.h
  2. 6 65
      squirrel/sqfuncstate.cpp
  3. 1 1
      squirrel/sqlexer.cpp
  4. 3 63
      squirrel/sqobject.cpp
  5. 77 73
      squirrel/sqopcodes.h

+ 11 - 1
squirrel/sqcompiler.h

@@ -5,7 +5,12 @@
 struct SQVM;
 
 enum SQKeywordsEnum {
-    TK_IDENTIFIER = 258,
+    TK_FIRST_ENUM_TOKEN = 258,
+    /*
+    the above token is only for internal purposes
+    like calculate total enum_tokens = TK_LAST_ENUM_TOKEN - TK_FIRST_ENUM_TOKEN -1
+    */
+    TK_IDENTIFIER,
     TK_STRING_LITERAL,
     TK_INTEGER,
     TK_FLOAT,
@@ -104,6 +109,11 @@ enum SQKeywordsEnum {
     TK_BIT_XOR_EQ,
     TK_BIT_SHIFT_LEFT_EQ,
     TK_BIT_SHIFT_RIGHT_EQ,
+    /*
+    the next token is only for internal purposes
+    like calculate total enum_tokens = TK_LAST_ENUM_TOKEN - TK_FIRST_ENUM_TOKEN -1
+    */
+    TK_LAST_ENUM_TOKEN
 };
 
 typedef void(*CompilerErrorFunc)(void *ud, const SQChar *s);

+ 6 - 65
squirrel/sqfuncstate.cpp

@@ -10,71 +10,12 @@
 #include "sqopcodes.h"
 #include "sqfuncstate.h"
 
-#ifdef _DEBUG_DUMP
-SQInstructionDesc g_InstrDesc[]={
-	{_SC("_OP_LINE")},
-	{_SC("_OP_LOAD")},
-	{_SC("_OP_LOADINT")},
-	{_SC("_OP_LOADFLOAT")},
-	{_SC("_OP_DLOAD")},
-	{_SC("_OP_TAILCALL")},
-	{_SC("_OP_CALL")},
-	{_SC("_OP_PREPCALL")},
-	{_SC("_OP_PREPCALLK")},
-	{_SC("_OP_GETK")},
-	{_SC("_OP_MOVE")},
-	{_SC("_OP_NEWSLOT")},
-	{_SC("_OP_DELETE")},
-	{_SC("_OP_SET")},
-	{_SC("_OP_GET")},
-	{_SC("_OP_EQ")},
-	{_SC("_OP_NE")},
-	{_SC("_OP_ADD")},
-	{_SC("_OP_SUB")},
-	{_SC("_OP_MUL")},
-	{_SC("_OP_DIV")},
-	{_SC("_OP_MOD")},
-	{_SC("_OP_BITW")},
-	{_SC("_OP_RETURN")},
-	{_SC("_OP_LOADNULLS")},
-	{_SC("_OP_LOADROOT")},
-	{_SC("_OP_LOADBOOL")},
-	{_SC("_OP_DMOVE")},
-	{_SC("_OP_JMP")},
-	{_SC("_OP_JCMP")},
-	{_SC("_OP_JZ")},
-	{_SC("_OP_SETOUTER")},
-	{_SC("_OP_GETOUTER")},
-	{_SC("_OP_NEWOBJ")},
-	{_SC("_OP_APPENDARRAY")},
-	{_SC("_OP_COMPARITH")},
-	{_SC("_OP_INC")},
-	{_SC("_OP_INCL")},
-	{_SC("_OP_PINC")},
-	{_SC("_OP_PINCL")},
-	{_SC("_OP_CMP")},
-	{_SC("_OP_EXISTS")},
-	{_SC("_OP_INSTANCEOF")},
-	{_SC("_OP_AND")},
-	{_SC("_OP_OR")},
-	{_SC("_OP_NEG")},
-	{_SC("_OP_NOT")},
-	{_SC("_OP_BWNOT")},
-	{_SC("_OP_CLOSURE")},
-	{_SC("_OP_YIELD")},
-	{_SC("_OP_RESUME")},
-	{_SC("_OP_FOREACH")},
-	{_SC("_OP_POSTFOREACH")},
-	{_SC("_OP_CLONE")},
-	{_SC("_OP_TYPEOF")},
-	{_SC("_OP_PUSHTRAP")},
-	{_SC("_OP_POPTRAP")},
-	{_SC("_OP_THROW")},
-	{_SC("_OP_NEWSLOTA")},
-	{_SC("_OP_GETBASE")},
-	{_SC("_OP_CLOSE")},
-	{_SC("_OP_JCMP")}
-};
+#ifdef _DEBUG_DUMP
+#define ENUM_OP(a,b) {_SC(#a)},
+SQInstructionDesc g_InstrDesc[]={
+    SQ_OP_CODE_LIST()
+};
+#undef ENUM_OP
 #endif
 void DumpLiteral(SQObjectPtr &o)
 {

+ 1 - 1
squirrel/sqlexer.cpp

@@ -43,7 +43,7 @@ void SQLexer::Init(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up,Compile
 
 SQTable * SQLexer::GetKeywords()
 {
-	SQTable *tbl = SQTable::Create(_sharedstate, 58 /*26*/);
+	SQTable *tbl = SQTable::Create(_sharedstate, (TK_LAST_ENUM_TOKEN - TK_FIRST_ENUM_TOKEN - 1) /*26*/);
 	ADD_KEYWORD(while, TK_WHILE);
 	ADD_KEYWORD(do, TK_DO);
 	ADD_KEYWORD(if, TK_IF);

+ 3 - 63
squirrel/sqobject.cpp

@@ -561,72 +561,12 @@ bool SQFunctionProto::SaveAsSource(SQVM *v,SQUserPointer up,SQWRITEFUNC write)
 	SafeWriteFmt(v,write,up,"\tinstructions = [\n");
     SafeWriteFmt(v,write,up,"\t\t//[op_str, op, arg0, arg1, arg2, arg3],\n");
 	const SQChar *str_op;
-#define CASE_OP(v) case v: str_op = _SC(#v); break;
 	for(i=0;i<ninstructions;i++){
 	    SQInstruction &inst = _instructions[i];
 	    switch(inst.op){
-            CASE_OP(_OP_LINE)
-            CASE_OP(_OP_LOAD)
-            CASE_OP(_OP_LOADINT)
-            CASE_OP(_OP_LOADFLOAT)
-            CASE_OP(_OP_DLOAD)
-            CASE_OP(_OP_TAILCALL)
-            CASE_OP(_OP_CALL)
-            CASE_OP(_OP_PREPCALL)
-            CASE_OP(_OP_PREPCALLK)
-            CASE_OP(_OP_GETK)
-            CASE_OP(_OP_MOVE)
-            CASE_OP(_OP_NEWSLOT)
-            CASE_OP(_OP_DELETE)
-            CASE_OP(_OP_SET)
-            CASE_OP(_OP_GET)
-            CASE_OP(_OP_EQ)
-            CASE_OP(_OP_NE)
-            CASE_OP(_OP_ADD)
-            CASE_OP(_OP_SUB)
-            CASE_OP(_OP_MUL)
-            CASE_OP(_OP_DIV)
-            CASE_OP(_OP_MOD)
-            CASE_OP(_OP_BITW)
-            CASE_OP(_OP_RETURN)
-            CASE_OP(_OP_LOADNULLS)
-            CASE_OP(_OP_LOADROOT)
-            CASE_OP(_OP_LOADBOOL)
-            CASE_OP(_OP_DMOVE)
-            CASE_OP(_OP_JMP)
-            //CASE_OP(_OP_JNZ)
-            CASE_OP(_OP_JCMP)
-            CASE_OP(_OP_JZ)
-            CASE_OP(_OP_SETOUTER)
-            CASE_OP(_OP_GETOUTER)
-            CASE_OP(_OP_NEWOBJ)
-            CASE_OP(_OP_APPENDARRAY)
-            CASE_OP(_OP_COMPARITH)
-            CASE_OP(_OP_INC)
-            CASE_OP(_OP_INCL)
-            CASE_OP(_OP_PINC)
-            CASE_OP(_OP_PINCL)
-            CASE_OP(_OP_CMP)
-            CASE_OP(_OP_EXISTS)
-            CASE_OP(_OP_INSTANCEOF)
-            CASE_OP(_OP_AND)
-            CASE_OP(_OP_OR)
-            CASE_OP(_OP_NEG)
-            CASE_OP(_OP_NOT)
-            CASE_OP(_OP_BWNOT)
-            CASE_OP(_OP_CLOSURE)
-            CASE_OP(_OP_YIELD)
-            CASE_OP(_OP_RESUME)
-            CASE_OP(_OP_FOREACH)
-            CASE_OP(_OP_POSTFOREACH)
-            CASE_OP(_OP_CLONE)
-            CASE_OP(_OP_TYPEOF)
-            CASE_OP(_OP_PUSHTRAP)
-            CASE_OP(_OP_POPTRAP)
-            CASE_OP(_OP_THROW)
-            CASE_OP(_OP_NEWSLOTA)
-            CASE_OP(_OP_GETBASE)
-            CASE_OP(_OP_CLOSE)
+#define ENUM_OP(a,b) case a: str_op = _SC(#a); break;
+	        SQ_OP_CODE_LIST()
+#undef ENUM_OP
             default:
                 str_op = _SC("???");
 	    }

+ 77 - 73
squirrel/sqopcodes.h

@@ -7,7 +7,7 @@
 
 enum BitWiseOP {
 	BW_AND = 0,
-	BW_OR = 2,	
+	BW_OR = 2,
 	BW_XOR = 3,
 	BW_SHIFTL = 4,
 	BW_SHIFTR = 5,
@@ -16,7 +16,7 @@ enum BitWiseOP {
 
 enum CmpOP {
 	CMP_G = 0,
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x20)\
+	ENUM_OP(_OP_NEWOBJ, 0x21)\
+	ENUM_OP(_OP_APPENDARRAY, 0x22)\
+	ENUM_OP(_OP_COMPARITH, 0x23)\
+	ENUM_OP(_OP_INC, 0x24)\
+	ENUM_OP(_OP_INCL, 0x25)\
+	ENUM_OP(_OP_PINC, 0x26)\
+	ENUM_OP(_OP_PINCL, 0x27)\
+	ENUM_OP(_OP_CMP, 0x28)\
+	ENUM_OP(_OP_EXISTS, 0x29)\
+	ENUM_OP(_OP_INSTANCEOF, 0x2A)\
+	ENUM_OP(_OP_AND, 0x2B)\
+	ENUM_OP(_OP_OR, 0x2C)\
+	ENUM_OP(_OP_NEG, 0x2D)\
+	ENUM_OP(_OP_NOT, 0x2E)\
+	ENUM_OP(_OP_BWNOT, 0x2F)\
+	ENUM_OP(_OP_CLOSURE, 0x30)\
+	ENUM_OP(_OP_YIELD, 0x31)\
+	ENUM_OP(_OP_RESUME, 0x32)\
+	ENUM_OP(_OP_FOREACH, 0x33)\
+	ENUM_OP(_OP_POSTFOREACH, 0x34)\
+	ENUM_OP(_OP_CLONE, 0x35)\
+	ENUM_OP(_OP_TYPEOF, 0x36)\
+	ENUM_OP(_OP_PUSHTRAP, 0x37)\
+	ENUM_OP(_OP_POPTRAP, 0x38)\
+	ENUM_OP(_OP_THROW, 0x39)\
+	ENUM_OP(_OP_NEWSLOTA, 0x3A)\
+	ENUM_OP(_OP_GETBASE, 0x3B)\
+	ENUM_OP(_OP_CLOSE, 0x3C)
+
+#define ENUM_OP(a,b) a = b,
 enum SQOpcode
-{
-	_OP_LINE=				0x00,	
-	_OP_LOAD=				0x01,
-	_OP_LOADINT=			0x02,
-	_OP_LOADFLOAT=			0x03,
-	_OP_DLOAD=				0x04,
-	_OP_TAILCALL=			0x05,	
-	_OP_CALL=				0x06,	
-	_OP_PREPCALL=			0x07,	
-	_OP_PREPCALLK=			0x08,	
-	_OP_GETK=				0x09,	
-	_OP_MOVE=				0x0A,	
-	_OP_NEWSLOT=			0x0B,	
-	_OP_DELETE=				0x0C,	
-	_OP_SET=				0x0D,	
-	_OP_GET=				0x0E,
-	_OP_EQ=					0x0F,
-	_OP_NE=					0x10,
-	_OP_ADD=				0x11,
-	_OP_SUB=				0x12,
-	_OP_MUL=				0x13,
-	_OP_DIV=				0x14,
-	_OP_MOD=				0x15,
-	_OP_BITW=				0x16,
-	_OP_RETURN=				0x17,	
-	_OP_LOADNULLS=			0x18,	
-	_OP_LOADROOT=			0x19,
-	_OP_LOADBOOL=			0x1A,
-	_OP_DMOVE=				0x1B,	
-	_OP_JMP=				0x1C,	
-	//_OP_JNZ=				0x1D,
-	_OP_JCMP=				0x1D,
-	_OP_JZ=					0x1E,	
-	_OP_SETOUTER=			0x1F,	
-	_OP_GETOUTER=			0x20,	
-	_OP_NEWOBJ=				0x21,
-	_OP_APPENDARRAY=		0x22,	
-	_OP_COMPARITH=			0x23,	
-	_OP_INC=				0x24,	
-	_OP_INCL=				0x25,	
-	_OP_PINC=				0x26,	
-	_OP_PINCL=				0x27,	
-	_OP_CMP=				0x28,
-	_OP_EXISTS=				0x29,	
-	_OP_INSTANCEOF=			0x2A,
-	_OP_AND=				0x2B,
-	_OP_OR=					0x2C,
-	_OP_NEG=				0x2D,
-	_OP_NOT=				0x2E,
-	_OP_BWNOT=				0x2F,	
-	_OP_CLOSURE=			0x30,	
-	_OP_YIELD=				0x31,	
-	_OP_RESUME=				0x32,
-	_OP_FOREACH=			0x33,
-	_OP_POSTFOREACH=		0x34,
-	_OP_CLONE=				0x35,
-	_OP_TYPEOF=				0x36,
-	_OP_PUSHTRAP=			0x37,
-	_OP_POPTRAP=			0x38,
-	_OP_THROW=				0x39,
-	_OP_NEWSLOTA=			0x3A,
-	_OP_GETBASE=			0x3B,
-	_OP_CLOSE=				0x3C,
-};							  
+{
+    SQ_OP_CODE_LIST()
+};
+#undef ENUM_OP
 
-struct SQInstructionDesc {	  
-	const SQChar *name;		  
-};							  
+struct SQInstructionDesc {
+	const SQChar *name;
+};
 
-struct SQInstruction 
+struct SQInstruction
 {
 	SQInstruction(){};
 	SQInstruction(SQOpcode _op,SQInteger a0=0,SQInteger a1=0,SQInteger a2=0,SQInteger a3=0)
@@ -114,8 +118,8 @@ struct SQInstruction
 		_arg0 = (unsigned char)a0;_arg1 = (SQInt32)a1;
 		_arg2 = (unsigned char)a2;_arg3 = (unsigned char)a3;
 	}
-    
-	
+
+
 	SQInt32 _arg1;
 	unsigned char op;
 	unsigned char _arg0;