4 * AST to Bytecode Conversion
12 #include "bytecode_gen.h"
13 #include "bytecode_ops.h"
15 #define TRACE_VAR_LOOKUPS 0
16 #define TRACE_NODE_RETURNS 0
19 extern tSpiderFunction *gpExports_First;
22 typedef struct sAST_BlockInfo
24 struct sAST_BlockInfo *Parent;
34 int AST_ConvertNode(tAST_BlockInfo *Block, tAST_Node *Node);
36 int BC_Variable_Define(tAST_BlockInfo *Block, int Type, const char *Name);
37 int BC_Variable_SetValue(tAST_BlockInfo *Block, tAST_Node *VarNode);
38 int BC_Variable_GetValue(tAST_BlockInfo *Block, tAST_Node *VarNode);
40 void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...);
41 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
44 // int giNextBlockIdent = 1;
48 * \brief Convert a function into bytecode
50 tBC_Function *Bytecode_ConvertFunction(tScript_Function *Fcn)
53 tAST_BlockInfo bi = {0};
55 // TODO: Return BCFcn instead?
56 if(Fcn->BCFcn) return NULL;
58 ret = Bytecode_CreateFunction(Fcn);
62 if( AST_ConvertNode(&bi, Fcn->ASTFcn) )
64 Bytecode_DeleteFunction(ret);
74 * \brief Convert a node into bytecode
75 * \param Block Execution context
76 * \param Node Node to execute
78 int AST_ConvertNode(tAST_BlockInfo *Block, tAST_Node *Node)
92 Bytecode_AppendEnterContext(Block->Handle); // Create a new block
94 tAST_BlockInfo blockInfo;
95 blockInfo.Parent = Block;
96 blockInfo.Handle = Block->Handle;
97 blockInfo.ContinueTarget = 0;
98 blockInfo.BreakTarget = 0;
99 // Loop over all nodes, or until the return value is set
100 for(node = Node->Block.FirstChild;
102 node = node->NextSibling )
104 AST_ConvertNode(Block, node);
107 Bytecode_AppendLeaveContext(Block->Handle); // Leave this context
111 case NODETYPE_ASSIGN:
112 // TODO: Support assigning to object attributes
113 if( Node->Assign.Dest->Type != NODETYPE_VARIABLE ) {
114 AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
117 ret = AST_ConvertNode(Block, Node->Assign.Value);
120 // Perform assignment operation
121 if( Node->Assign.Operation != NODETYPE_NOP )
124 ret = BC_Variable_GetValue(Block, Node->Assign.Dest);
126 Bytecode_AppendBinOp(Block->Handle, Node->Assign.Operation);
129 // Set the variable value
130 ret = BC_Variable_SetValue( Block, Node->Assign.Dest );
133 // Post increment/decrement
134 case NODETYPE_POSTINC:
135 case NODETYPE_POSTDEC:
136 Bytecode_AppendConstInt(Block->Handle, 1);
138 // TODO: Support assigning to object attributes
139 if( Node->UniOp.Value->Type != NODETYPE_VARIABLE ) {
140 AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
144 ret = BC_Variable_GetValue(Block, Node->UniOp.Value);
147 if( Node->Type == NODETYPE_POSTDEC )
148 Bytecode_AppendBinOp(Block->Handle, NODETYPE_SUBTRACT);
150 Bytecode_AppendBinOp(Block->Handle, NODETYPE_ADD);
153 ret = BC_Variable_SetValue(Block, Node->UniOp.Value);
158 case NODETYPE_METHODCALL:
159 case NODETYPE_FUNCTIONCALL:
160 case NODETYPE_CREATEOBJECT:
162 for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling)
164 ret = AST_ConvertNode(Block, node);
170 if( Node->Type == NODETYPE_CREATEOBJECT )
172 // TODO: Sanity check stack top
173 Bytecode_AppendCreateObj(Block->Handle, Node->FunctionCall.Name, i);
175 else if( Node->Type == NODETYPE_METHODCALL )
177 // TODO: Sanity check stack top
178 ret = AST_ConvertNode(Block, Node->FunctionCall.Object);
180 Bytecode_AppendMethodCall(Block->Handle, Node->FunctionCall.Name, i);
184 Bytecode_AppendFunctionCall(Block->Handle, Node->FunctionCall.Name, i);
191 ret = AST_ConvertNode(Block, Node->If.Condition);
194 if_true = Bytecode_AllocateLabel(Block->Handle);
195 if_end = Bytecode_AllocateLabel(Block->Handle);
197 Bytecode_AppendCondJump(Block->Handle, if_true);
200 ret = AST_ConvertNode(Block, Node->If.False);
202 Bytecode_AppendJump(Block->Handle, if_end);
205 Bytecode_SetLabel(Block->Handle, if_true);
206 ret = AST_ConvertNode(Block, Node->If.True);
210 Bytecode_SetLabel(Block->Handle, if_end);
214 case NODETYPE_LOOP: {
215 int loop_start, loop_end;
216 int saved_break, saved_continue;
217 const char *saved_tag;
220 ret = AST_ConvertNode(Block, Node->For.Init);
223 loop_start = Bytecode_AllocateLabel(Block->Handle);
224 loop_end = Bytecode_AllocateLabel(Block->Handle);
226 saved_break = Block->BreakTarget;
227 saved_continue = Block->ContinueTarget;
228 saved_tag = Block->Tag;
229 Block->BreakTarget = loop_end;
230 Block->ContinueTarget = loop_end;
231 Block->Tag = Node->For.Tag;
233 Bytecode_SetLabel(Block->Handle, loop_start);
235 // Check initial condition
236 if( !Node->For.bCheckAfter )
238 ret = AST_ConvertNode(Block, Node->For.Condition);
240 Bytecode_AppendUniOp(Block->Handle, NODETYPE_LOGICALNOT);
241 Bytecode_AppendCondJump(Block->Handle, loop_end);
245 ret = AST_ConvertNode(Block, Node->For.Code);
249 ret = AST_ConvertNode(Block, Node->For.Increment);
253 if( Node->For.bCheckAfter )
255 ret = AST_ConvertNode(Block, Node->For.Condition);
257 Bytecode_AppendCondJump(Block->Handle, loop_start);
260 Bytecode_SetLabel(Block->Handle, loop_end);
262 Block->BreakTarget = saved_break;
263 Block->ContinueTarget = saved_continue;
264 Block->Tag = saved_tag;
268 case NODETYPE_RETURN:
269 ret = AST_ConvertNode(Block, Node->UniOp.Value);
271 Bytecode_AppendReturn(Block->Handle);
275 case NODETYPE_CONTINUE: {
276 tAST_BlockInfo *bi = Block;
277 if( Node->Variable.Name[0] ) {
278 while(bi && strcmp(bi->Tag, Node->Variable.Name) == 0) bi = bi->Parent;
281 // TODO: Check if BreakTarget/ContinueTarget are valid
282 if( Node->Type == NODETYPE_BREAK )
283 Bytecode_AppendJump(Block->Handle, bi->BreakTarget);
285 Bytecode_AppendJump(Block->Handle, bi->ContinueTarget);
289 case NODETYPE_DEFVAR:
290 ret = BC_Variable_Define(Block, Node->DefVar.DataType, Node->DefVar.Name);
293 if( Node->DefVar.InitialValue )
295 ret = AST_ConvertNode(Block, Node->DefVar.InitialValue);
297 Bytecode_AppendSaveVar(Block->Handle, Node->DefVar.Name);
303 Bytecode_AppendSubNamespace(Block->Handle, Node->Scope.Name);
304 ret = AST_ConvertNode(Block, Node->Scope.Element);
308 case NODETYPE_VARIABLE:
309 ret = BC_Variable_GetValue( Block, Node );
312 // Element of an Object
313 case NODETYPE_ELEMENT:
314 ret = AST_ConvertNode( Block, Node->Scope.Element );
317 Bytecode_AppendElement(Block->Handle, Node->Scope.Name);
320 // Cast a value to another
322 ret = AST_ConvertNode(Block, Node->Cast.Value);
324 Bytecode_AppendCast(Block->Handle, Node->Cast.DataType);
327 // Index into an array
329 ret = AST_ConvertNode(Block, Node->BinOp.Left); // Array
331 ret = AST_ConvertNode(Block, Node->BinOp.Right); // Offset
334 Bytecode_AppendIndex(Block->Handle);
337 // TODO: Implement runtime constants
338 case NODETYPE_CONSTANT:
339 // TODO: Scan namespace for constant name
340 AST_RuntimeError(Node, "TODO - Runtime Constants");
345 case NODETYPE_STRING:
346 Bytecode_AppendConstString(Block->Handle, Node->Constant.String.Data, Node->Constant.String.Length);
348 case NODETYPE_INTEGER:
349 Bytecode_AppendConstInt(Block->Handle, Node->Constant.Integer);
352 Bytecode_AppendConstInt(Block->Handle, Node->Constant.Real);
355 // --- Operations ---
356 // Boolean Operations
357 case NODETYPE_LOGICALNOT: // Logical NOT (!)
358 if(!op) op = BC_OP_LOGICNOT;
359 case NODETYPE_BWNOT: // Bitwise NOT (~)
360 if(!op) op = BC_OP_BITNOT;
361 case NODETYPE_NEGATE: // Negation (-)
362 if(!op) op = BC_OP_NEG;
363 ret = AST_ConvertNode(Block, Node->UniOp.Value);
365 Bytecode_AppendUniOp(Block->Handle, op);
368 case NODETYPE_LOGICALAND: // Logical AND (&&)
369 if(!op) op = BC_OP_LOGICAND;
370 case NODETYPE_LOGICALOR: // Logical OR (||)
371 if(!op) op = BC_OP_LOGICOR;
372 case NODETYPE_LOGICALXOR: // Logical XOR (^^)
373 if(!op) op = BC_OP_LOGICXOR;
375 case NODETYPE_EQUALS: if(!op) op = BC_OP_EQUALS;
376 case NODETYPE_LESSTHAN: if(!op) op = BC_OP_LESSTHAN;
377 case NODETYPE_GREATERTHAN: if(!op) op = BC_OP_GREATERTHAN;
378 case NODETYPE_LESSTHANEQUAL: if(!op) op = BC_OP_LESSTHANOREQUAL;
379 case NODETYPE_GREATERTHANEQUAL: if(!op) op = BC_OP_GREATERTHANOREQUAL;
380 // General Binary Operations
381 case NODETYPE_ADD: if(!op) op = BC_OP_ADD;
382 case NODETYPE_SUBTRACT: if(!op) op = BC_OP_SUBTRACT;
383 case NODETYPE_MULTIPLY: if(!op) op = BC_OP_MULTIPLY;
384 case NODETYPE_DIVIDE: if(!op) op = BC_OP_DIVIDE;
385 case NODETYPE_MODULO: if(!op) op = BC_OP_MODULO;
386 case NODETYPE_BWAND: if(!op) op = BC_OP_BITAND;
387 case NODETYPE_BWOR: if(!op) op = BC_OP_BITOR;
388 case NODETYPE_BWXOR: if(!op) op = BC_OP_BITXOR;
389 case NODETYPE_BITSHIFTLEFT: if(!op) op = BC_OP_BITSHIFTLEFT;
390 case NODETYPE_BITSHIFTRIGHT: if(!op) op = BC_OP_BITSHIFTRIGHT;
391 case NODETYPE_BITROTATELEFT: if(!op) op = BC_OP_BITROTATELEFT;
392 ret = AST_ConvertNode(Block, Node->BinOp.Left);
394 ret = AST_ConvertNode(Block, Node->BinOp.Right);
397 Bytecode_AppendBinOp(Block->Handle, op);
402 // AST_RuntimeError(Node, "BUG - SpiderScript AST_ConvertNode Unimplemented %i", Node->Type);
406 #if TRACE_NODE_RETURNS
407 if(ret && ret != ERRPTR) {
408 AST_RuntimeError(Node, "Ret type of %p %i is %i", Node, Node->Type, ret->Type);
411 AST_RuntimeError(Node, "Ret type of %p %i is %p", Node, Node->Type, ret);
419 * \brief Define a variable
420 * \param Block Current block state
421 * \param Type Type of the variable
422 * \param Name Name of the variable
423 * \return Boolean Failure
425 int BC_Variable_Define(tAST_BlockInfo *Block, int Type, const char *Name)
428 tAST_Variable *var, *prev = NULL;
430 for( var = Block->FirstVar; var; prev = var, var = var->Next )
432 if( strcmp(var->Name, Name) == 0 ) {
433 AST_RuntimeError(NULL, "Redefinition of variable '%s'", Name);
438 var = malloc( sizeof(tAST_Variable) + strlen(Name) + 1 );
441 strcpy(var->Name, Name);
443 if(prev) prev->Next = var;
444 else Block->FirstVar = var;
448 Bytecode_AppendDefineVar(Block->Handle, Name, Type);
453 tAST_Variable *BC_Variable_Lookup(tAST_BlockInfo *Block, tAST_Node *VarNode, int CreateType)
456 tAST_Variable *var = NULL;
459 if( VarNode->BlockState == Block && VarNode->BlockIdent == Block->Ident ) {
460 var = VarNode->ValueCache;
461 #if TRACE_VAR_LOOKUPS
462 AST_RuntimeMessage(VarNode, "debug", "Fast var fetch on '%s' %p (%p:%i)",
463 VarNode->Variable.Name, var,
464 VarNode->BlockState, VarNode->BlockIdent
471 for( bs = Block; bs; bs = bs->Parent )
473 for( var = bs->FirstVar; var; var = var->Next )
475 if( strcmp(var->Name, VarNode->Variable.Name) == 0 )
483 if( Block->Script->Variant->bDyamicTyped && CreateType != SS_DATATYPE_UNDEF ) {
485 var = BC_Variable_Define(Block, CreateType, VarNode->Variable.Name, NULL);
489 AST_RuntimeError(VarNode, "Variable '%s' is undefined", VarNode->Variable.Name);
494 #if TRACE_VAR_LOOKUPS
495 AST_RuntimeMessage(VarNode, "debug", "Saved variable lookup of '%s' %p (%p:%i)",
496 VarNode->Variable.Name, var,
497 Block, Block->Ident);
500 VarNode->ValueCache = var;
501 VarNode->BlockState = Block;
502 VarNode->BlockIdent = Block->Ident;
512 * \brief Set the value of a variable
513 * \return Boolean Failure
515 int BC_Variable_SetValue(tAST_BlockInfo *Block, tAST_Node *VarNode)
519 var = BC_Variable_Lookup(Block, VarNode, SS_DATATYPE_UNDEF);
524 Bytecode_AppendSaveVar(Block->Handle, VarNode->Variable.Name);
529 * \brief Get the value of a variable
531 int BC_Variable_GetValue(tAST_BlockInfo *Block, tAST_Node *VarNode)
535 var = BC_Variable_Lookup(Block, VarNode, 0);
538 Bytecode_AppendLoadVar(Block->Handle, VarNode->Variable.Name);
543 void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...)
548 fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
550 fprintf(stderr, "%s: ", Type);
551 va_start(args, Format);
552 vfprintf(stderr, Format, args);
554 fprintf(stderr, "\n");
556 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...)
561 fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
563 fprintf(stderr, "error: ");
564 va_start(args, Format);
565 vfprintf(stderr, Format, args);
567 fprintf(stderr, "\n");