summaryrefslogtreecommitdiff
path: root/src/nb_opcode.h
blob: 7bd9710b2556ab405500f62a062cafab78910755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#ifndef NB_OPCODE_H
#define NB_OPCODE_H

typedef enum {
	// import a external module
	OP_IMPORT,              // IMPORT

	// load class, enum, or mask from a module
	OP_LOAD_EXTERNAL,       // LOAD_EXTERNAL       nameOfModule		|    

	OP_PUSH_CONSTANT,       // PUSH_CONSTANT       i							|   1 
	OP_PUSH_NULL,           // PUSH_NULL		      								|   1
	OP_PUSH_TRUE,           // PUSH_TRUE		      								|   1
	OP_PUSH_FALSE,          // PUSH_FALSE		      								|   1
								
	// local-scoped variables
	OP_LOAD_LOCAL,          // LOAD_LOCAL          i							|   1
	OP_STORE_LOCAL,         // STORE_LOCAL         i							|  -1
										      									    
	// load or store upvalue 
	OP_LOAD_UPVALUE,        // LOAD_UPVALUE        i 							|   1
	OP_STORE_UPVALUE,       // STORE_UPVALUE       i							|  -1 

	// file-scoped variables										      									    
	OP_LOAD_MODULE,         // LOAD_MODULE         i							|   1
	OP_STORE_MODULE,        // STORE_MODULE        i							|  -1
									    
	OP_POP,                 // POP					      							  |   1
	OP_POPN,                // POPN                numOfValue     |   n
										      									    
	OP_JUMP,                // JUMP                i							|   0 
	OP_JUMP_IF_FALSE,       // JUMP_IF_FALSE                      |
	OP_RETURN,              // RETURN              numOfReturn		|  -numOfReturn
	OP_RETURN_NULL,         // RETURN_NULL                        | 
	
	// instansiate class 
	OP_INSTANTIATE,         // INSTANTIATE         numOfArgs      | 
					
	// call methods or functions
	OP_CALL,                // CALL                numOfArgs			|  -(1+numOfArgs)
	OP_OPERATOR,            // OPERATOR            operatorId     |  -(1+numOfArgs)

	// define class and method
	OP_CLASS,               // CLASS                              |   0
	OP_METHOD,              // METHOD
	OP_METHOD_STATIC,       // METHOD_STATIC

	// load and store member of object or static member of class
	OP_LOAD_FIELD,          // LOAD_FIELD 
	OP_STORE_FIELD,         // STORE_FIELD 

	// call base class method, handle base. in methods 
	OP_CALL_BASE,           // CALL_BASE           numOfArgs      |
		
	OP_TYPEOF,              // 
	OP_INSTANCEOF,          // 

	// define enum or mask
	OP_ENUM,                // ENUM                numOfElements  |  -(numOfElements)
	OP_MASK,                // MASK                numOfElements  |  -(numOfElements)

	// create map, list or hashset 
	OP_MAP, 
	OP_LIST,
	OP_HASHSET,

} OpCode;

// 对于运算符,使用 CALL 指令,运算符的操作数个数是确定的
// operator     numOfArgs     pop
// +                2			      
// ++		            1			      
// += 	            2			      
// - 		            2			      
// --		            1			      
// -=		            2			      
// *		            2			      
// *=		            2			      
// /		            2			      
// /=		            2			      
// ^		            2			      
// ^=		            2			      
// %		            2			      
// %=		            2			      
// >>		            2			      
// >>= 	            2			      
// <<		            2			      
// <<=	            2			      
// &		            2			      
// |		            2			      
// !		            1			      
// 			            			      
// &&		            2			      
// &=		            2			      
// ||		            2			      
// |=		            2			      
// ==		            2			      
// >		            2			      
// >=		            2			      
// <		            2			      
// <=		            2			      
// !=		            2			      
// 			        


// 导入模块流程
// import Test; 
// IMPORT 
// 
// import Test for CFoo; 
// IMPORT           "Test"
// STORE_MODULE   
// POP
// 
// import Test for CFoo as CClass;
// IMPORT           "Test"
// LOAD_EXTERNAL    "CFoo"
// STORE_MODULE      0    // "CClass"
// POPN              2


// 方法调用
// var foo = new CClass(); 
// var b = 20;
// foo.Func(10, b); 
// LOAD_LOCAL      1      // foo
// LOAD_FIELD     "Func"
// LOAD_LOCAL      1      // foo
// PUSH_CONSTANT   10 
// LOAD_LOCAL      2      // b
// CALL            3
// 
// var c = a + b; 
// LOAD_LOCAL      1      // a
// LOAD_LOCAL      2      // b
// OPERATOR        1      // + 


// 函数调用
// foo = func(10);
// LOAD_MODULE     1      // func
// PUSH_CONSTANT   1      // 10
// CALL            1      
// STORE_LOCAL     2      // foo

// 对象方法和函数\类方法的不同在于对象方法会把self放在某个upvalue
// 处理self.调用
// class CFoo { 
//   function foo(a) { print(self.b);} 
//   static function foo2(a) { print(a);} 
// }
// function foo3(a) {print(a);}
// foo和另两个的差别在于后两个索引到的self upvalue是空的,而foo的
// 能够索引到这个upvalue,
// 

// 实例化
// foo = new CClass(10, 30);
// LOAD_MODULE     1      // CClass 
// PUSH_CONSTANT   4      // 10 
// PUSH_CONSTANT   5      // 30 
// INSTANTIATE     2    
// STORE_LOCAL     2      // foo


// 创建enum\mask
// enum EColor { White = 1, Blue = 2, Yellow = 3 }
// CONSTANT       "EColor"
// ENUM                     // EColor
// CONSTANT        1			  
// STORE_FIELD    "White"   // White = 1
// CONSTANT        2			  
// STORE_FIELD    "Blue"    // Blue = 1
// CONSTANT        3			  
// STORE_FIELD    "Yellow"  // Yellow = 1
// POP                      // EColor


// 创建map
// var foo = {"keyA" : 10, "keyB" : "hello"};
// foo["keyC"] = 20;
// MAP
// CONSTANT        1        // 10 
// STORE_FIELD    "keyA"    // "keyA" : 10
// CONSTANT       "hello"
// STORE_FIELD    "keyB"    // "keyB" : "hello"
// LOAD_LOCAL      1        // foo
// CONSTATN        20
// STORE_FIELD    "keyC"


// 创建list\hashset 
// var foo = [1,2,3];
// LIST
// CONSTANT        1    // 1 
// STORE_FIELD 
// CONSTANT        2
// STORE_FIELD  
// CONSTANT        3
// STORE_FIELD 


// 构建闭包,实现观察者模式的方法
// class CFoo { 
//   function OnEnable() {
//     var this = self;
//     // self 这里会作为upvalue
//	   onClick += function(...){this.OnClickCallback(...);};
//   } 
// }

// self.用来索引self upvalue,如果是方法,就会索引对象
// base.用来索引base upvalue
// type.用来索引type upvalue
// basetype.用来索引基类的upvalue

// .运算符只能运用于对象或者map\list\hashset,会设置的upvalue:
// * self

// 类不能用::或者.索引对象方法,只能索引到static成员

// 在init外部不能创建新的对象成员

#endif