class CompilationEngine(object): # the destination file for writing destination_file = None # the tokenizer for the input file tokenizer = None # symbol table symbol_table = None # vm writer vm_writer = None # the class name class_name = "" # indicies for if and while loops # start at -1 because we increment before use while_index = -1 if_index = -1 # the constructor for compiling a single class # the next method to be called after construction must be compile_class # source_filename must be a single file, not a directory def __init__(self, source_filename): # destination filename # if the original extension was .jack, then make the extension .vm # if the original extension was not .jack, then append .vm if source_filename.lower().endswith(".jack"): destination_filename = source_filename[:-5] + ".vm" else: destination_filename = source_filename + ".vm" # open the destination filename for writing self.destination_file = open(destination_filename, 'w') # create a tokenizer for the input file self.tokenizer = JackTokenizer(source_filename) # create the symbol table self.symbol_table = SymbolTable() # create the vm writer self.vm_writer = VMWriter(self.destination_file) # compiles a complete class and closes the output file def compile_class(self): # class keyword tt, t = self._token_next(True, "KEYWORD", "class") # name of class tt, t = self._token_next(True, "IDENTIFIER") self.class_name = t # open brace tt, t = self._token_next(True, "SYMBOL", "{") # one or more variable declarations self.tokenizer.advance() while True: tt, t = self._token_next(False) if tt == "KEYWORD" and t in ["field", "static"]: self.compile_class_var_dec() else: # stop trying to process variable declarations break # one or more subroutine declarations while True: tt, t = self._token_next(False) if tt == "KEYWORD" and t in ["constructor", "function", "method"]: self.compile_subroutine() else: # stop trying to process functions break # close brace # do not advance because we already advanced upon exiting the last loop tt, t = self._token_next(False, "SYMBOL", "}") # done with compilation; close the output file self.destination_file.close() # compiles a static declaration or field declaration def compile_class_var_dec(self): # compile the variable declaration # False means this is a class (not a subroutine) self.compile_var_dec(False) # compiles a complete method, function, or constructor def compile_subroutine(self): # start of subroutine self.symbol_table.start_subroutine() # constructor, function, or method keyword tt, type = self._token_next(False, "KEYWORD") # type of the return value # can be either keyword (void) or an identifier (any type) tt, t = self._token_next(True) # name of the method/function/constructor tt, name = self._token_next(True) name = self.class_name + "." + name # if the type is a method, "define" this as an argument, so the other # argument indexes work correctly if type == "method": self.symbol_table.define("this", self.class_name, SymbolTable.ARG) # opening parenthesis tt, t = self._token_next(True, "SYMBOL", "(") # arguments self.tokenizer.advance() self.compile_parameter_list() # closing parenthesis tt, t = self._token_next(False, "SYMBOL", ")") # opening brace tt, t = self._token_next(True, "SYMBOL", "{") # variable declarations self.tokenizer.advance() while True: tt, t = self._token_next(False) if tt == "KEYWORD" and t == "var": self.compile_var_dec() else: # stop trying to process variable declarations break # write the function num_locals = self.symbol_table.var_count(self.symbol_table.VAR) self.vm_writer.write_function(name, num_locals) # write any special code at the top of the function if type == "constructor": # code to allocate memory and set "this" size = self.symbol_table.var_count(self.symbol_table.FIELD) self.vm_writer.write_push(self.vm_writer.CONST, size) self.vm_writer.write_call("Memory.alloc", 1) self.vm_writer.write_pop(self.vm_writer.POINTER, 0) elif type == "function": # nothing special pass elif type == "method": # put argument 0 into pointer 0 (this) self.vm_writer.write_push(self.vm_writer.ARG, 0) self.vm_writer.write_pop(self.vm_writer.POINTER, 0) else: print "WARNING: Expected constructor, function, or name; got", type # statements self.compile_statements() # closing brace tt, t = self._token_next(False, "SYMBOL", "}") self.tokenizer.advance() # compiles a (possibly empty) parameter list, not including the enclosing # parentheses def compile_parameter_list(self): # check for empty list tt, t = self._token_next(False) if tt == "SYMBOL" and t == ")": # the parameter list was empty; do not process any more pass else: # there are things in the parameter list while True: # keyword (variable type) tt, type = self._token_next(False) # identifier (variable name) tt, name = self._token_next(True) # the kind is always an arg, since these are all parameters to the # function kind = SymbolTable.ARG # define the variable in the symbol table self.symbol_table.define(name, type, kind) # possible comma tt, t = self._token_next(True) if tt != "SYMBOL" or t != ",": # not a comma; stop processing parameters break self.tokenizer.advance() # compiles a var declaration # if subroutine is true, only the var keyword can be used # if subroutine is false, only the static and field keywords can be used def compile_var_dec(self, subroutine=True): # the keyword to start the declaration tt, kind = self._token_next(False, "KEYWORD") # check for required types if subroutine: if kind == "var": kind = SymbolTable.VAR else: print "WARNING: expecting var, but received %s" % (str(kind)) else: if kind == "static": kind = SymbolTable.STATIC elif kind == "field": kind = SymbolTable.FIELD else: print "WARNING: expecting static or field, but received %s" % (str(kind)) # type of the declaration # could be an identifier or a keyword (int, etc) tt, type = self._token_next(True) # name of the declaration tt, name = self._token_next(True, "IDENTIFIER") # define the variable in the symbol table self.symbol_table.define(name, type, kind) # can support more than one identifier name, to declare more than one # variable, separated by commas; process the 2nd-infinite variables self.tokenizer.advance() while True: tt, t = self._token_next(False) if tt == "SYMBOL" and t == ",": # another variable name follows tt, name = self._token_next(True, "IDENTIFIER") # define the variable in the symbol table self.symbol_table.define(name, type, kind) self.tokenizer.advance() else: # no more variable names break # should be on the semicolon at the end of the line tt, t = self._token_next(False, "SYMBOL", ";") self.tokenizer.advance() # compiles a sequence of statements, not including the enclosing {} def compile_statements(self): while True: tt, t = self._token_next(False) if tt == "KEYWORD" and t in ["do", "let", "while", "return", "if"]: # call compile_t, where t is the type of compilation we want token = getattr(self, "compile_" + t)() else: # not a statement; stop processing statements break # compiles a do statement def compile_do(self): # do keyword tt, t = self._token_next(False, "KEYWORD", "do") # subroutine call self.tokenizer.advance() self.compile_subroutine_call() # do statements do not have a return value, so eliminate the return # off of the stack self.vm_writer.write_pop(self.vm_writer.TEMP, 0) # semicolon tt, t = self._token_next(False, "SYMBOL", ";") self.tokenizer.advance() # compiles a let statement def compile_let(self): # let keyword tt, t = self._token_next(False, "KEYWORD", "let") # variable name tt, name = self._token_next(True, "IDENTIFIER") # possible brackets for array tt, t = self._token_next(True) if tt == "SYMBOL" and t == "[": # array - write operation array = True # compile the offset expression self.tokenizer.advance() self.compile_expression() # write the base address onto the stack segment, index = self._resolve_symbol(name) self.vm_writer.write_push(segment, index) # add base and offset self.vm_writer.write_arithmetic("add") # we cannot yet put the result into pointer 1, since the read # operation (which hasn't been parsed/computed yet) may use pointer 1 # to read from an arrya value # closing bracket tt, t = self._token_next(False, "SYMBOL", "]") # advance to the next token, since we are expected to be on the = for # the next line self.tokenizer.advance() else: array = False # equals sign tt, t = self._token_next(False, "SYMBOL", "=") # expression self.tokenizer.advance() self.compile_expression() if array: # our stack now looks like this: # TOP OF STACK # computed result to store # address in which value should be stored # ... previous stuff ... # pop the computed value to temp 0 self.vm_writer.write_pop(self.vm_writer.TEMP, 0) # pop the array address to pointer 1 (that) self.vm_writer.write_pop(self.vm_writer.POINTER, 1) # put the computed value back onto the stack self.vm_writer.write_push(self.vm_writer.TEMP, 0) # pop to the variable name or the array reference self.vm_writer.write_pop(self.vm_writer.THAT, 0) else: # not an array - pop the expression to the variable segment, index = self._resolve_symbol(name) self.vm_writer.write_pop(segment, index) # semicolon tt, t = self._token_next(False, "SYMBOL", ";") self.tokenizer.advance() # compiles a while statement def compile_while(self): # labels for this while loop self.while_index += 1 while_start = "WHILE_START_%d" % (self.while_index) while_end = "WHILE_END_%d" % (self.while_index) # while keyword tt, t = self._token_next(False, "KEYWORD", "while") # opening parenthesis tt, t = self._token_next(True, "SYMBOL", "(") # label for the start of the while statement self.vm_writer.write_label(while_start) # the expression that is the condition of the while statement self.tokenizer.advance() self.compile_expression() # the closing parenthesis tt, t = self._token_next(False, "SYMBOL", ")") # the result of the evaluation is now on the stack # if false, then goto to the end of the loop # to do this, negate and then call if-goto self.vm_writer.write_arithmetic("not") self.vm_writer.write_if(while_end) # the opening brace tt, t = self._token_next(True, "SYMBOL", "{") # the statments that is the body of the while loop self.tokenizer.advance() self.compile_statements() # the closing brace tt, t = self._token_next(False, "SYMBOL", "}") # after the last statement of the while loop # need to jump back up to the top of the loop to evaluate again self.vm_writer.write_goto(while_start) # label at the end of the loop self.vm_writer.write_label(while_end) self.tokenizer.advance() # compiles a return statement def compile_return(self): # return keyword tt, t = self._token_next(False, "KEYWORD", "return") # possible expression to return tt, t = self._token_next(True) if tt != "SYMBOL" and t != ";": self.compile_expression() else: # no return expression; return 0 self.vm_writer.write_push(self.vm_writer.CONST, 0) # ending semicolon tt, t = self._token_next(False, "SYMBOL", ";") self.vm_writer.write_return() self.tokenizer.advance() # compiles a if statement, including a possible trailing else clause def compile_if(self): # it is more efficient in an if-else case to have the else portion first # in the code when testing, but we use the less-efficient but # easier-to-write true-false pattern here # labels for this if statement self.if_index += 1 if_false = "IF_FALSE_%d" % (self.if_index) if_end = "IF_END_%d" % (self.if_index) # if keyword tt, t = self._token_next(False, "KEYWORD", "if") # opening parenthesis tt, t = self._token_next(True, "SYMBOL", "(") # expression of if statement self.tokenizer.advance() self.compile_expression() # closing parenthesis tt, t = self._token_next(False, "SYMBOL", ")") # the result of the evaluation is now on the stack # if false, then goto the false label # if true, fall through to executing code # if there is no else, then false and end are the same, but having two # labels does not increase code size self.vm_writer.write_arithmetic("not") self.vm_writer.write_if(if_false) # opening brace tt, t = self._token_next(True, "SYMBOL", "{") # statements for true portion self.tokenizer.advance() self.compile_statements() # closing brace tt, t = self._token_next(False, "SYMBOL", "}") tt, t = self._token_next(True) if tt == "KEYWORD" and t == "else": # else statement exists # goto the end of the if statement at the end of the true portion self.vm_writer.write_goto(if_end) # label for the start of the false portion self.vm_writer.write_label(if_false) # opening brace tt, t = self._token_next(True, "SYMBOL", "{") # statements self.tokenizer.advance() self.compile_statements() # closing brace tt, t = self._token_next(False, "SYMBOL", "}") # end label self.vm_writer.write_label(if_end) # advance tokenizer only if we are in the else, since otherwise the # token was advanced by the else check self.tokenizer.advance() else: # no else portion; only put in a label for false, since end is not # used self.vm_writer.write_label(if_false) # compiles an expression (one or more terms connected by operators) def compile_expression(self): # the first term self.compile_term() # finish any number of operators followed by terms while True: tt, t = self._token_next(False) if tt == "SYMBOL" and t in "+-*/&|<>=": # found an operator # postfix order - add the next term and then do the operator # the next term self.tokenizer.advance() self.compile_term() # the operator if t == "+": self.vm_writer.write_arithmetic("add") if t == "-": self.vm_writer.write_arithmetic("sub") if t == "=": self.vm_writer.write_arithmetic("eq") if t == ">": self.vm_writer.write_arithmetic("gt") if t == "<": self.vm_writer.write_arithmetic("lt") if t == "&": self.vm_writer.write_arithmetic("and") if t == "|": self.vm_writer.write_arithmetic("or") if t == "*": self.vm_writer.write_call("Math.multiply", 2) if t == "/": self.vm_writer.write_call("Math.divide", 2) else: # no term found; done parsing the expression break # compiles a term # this routine is faced with a slight difficulty when trying to decide # between some of the alternative parsing rules. specifically, if the # current token is an identifier, the routine must distinguish between a # variable, an array entry, and a subroutine call. a single lookahead token, # which may be one of [, (, or ., suffices to distinguish between the three # possibilities. any other token is not part of this term and should not # be advanced over. def compile_term(self): # a term: integer_constant | string_constant | keyword_constant | # varname | varname[expression] | subroutine_call | (expression) | # unary_op term tt, t = self._token_next(False) if tt == "INT_CONST": self.vm_writer.write_push(self.vm_writer.CONST, t) # advance for the next statement self.tokenizer.advance() elif tt == "STRING_CONST": # after this portion is run, a pointer to a string should be on the # stack # we create a new string of a certain size and then append characters # one by one; each append operation returns the pointer to the same # string # create the string # string is a len, data tuple; not null-terminated size = len(t) self.vm_writer.write_push(self.vm_writer.CONST, size) self.vm_writer.write_call("String.new", 1) # append each character for char in t: self.vm_writer.write_push(self.vm_writer.CONST, ord(char)) self.vm_writer.write_call("String.appendChar", 2) # advance for the next statement self.tokenizer.advance() elif tt == "KEYWORD": if t == "true": # true is -1, which is 0 negated self.vm_writer.write_push(self.vm_writer.CONST, 0) self.vm_writer.write_arithmetic("not") elif t == "false" or t == "null": self.vm_writer.write_push(self.vm_writer.CONST, 0) elif t == "this": self.vm_writer.write_push(self.vm_writer.POINTER, 0) # advance for the next statement self.tokenizer.advance() elif tt == "SYMBOL" and t == "(": # ( expression ) # parse the expression self.tokenizer.advance() self.compile_expression() # closing parenthesis tt, t = self._token_next(False, "SYMBOL", ")") # advance for the next statement self.tokenizer.advance() elif tt == "SYMBOL" and t in "-~": # unary_op term # postfix order - add the next term and then do the operator # parse the rest of the term self.tokenizer.advance() self.compile_term() # write the unary operation if t == "-": self.vm_writer.write_arithmetic("neg") elif t == "~": self.vm_writer.write_arithmetic("not") elif tt == "IDENTIFIER": # varname, varname[expression], subroutine_call # do not write the identifer yet # get the next bit of the expression # if it is a [, then array; if it is a ( or ., then subroutine call # if none of above, then pass over tt2, t2 = self._token_next(True) if tt2 == "SYMBOL" and t2 in "(.": # subroutine call # back up and then compile the subroutine call self.tokenizer.retreat() self.compile_subroutine_call() elif tt2 == "SYMBOL" and t2 == "[": # array - read operation # write the base address onto the stack segment, index = self._resolve_symbol(t) self.vm_writer.write_push(segment, index) # compile the offset expression self.tokenizer.advance() self.compile_expression() # add base and offset self.vm_writer.write_arithmetic("add") # put the resulting address into pointer 1 (that) self.vm_writer.write_pop(self.vm_writer.POINTER, 1) # read from that 0 onto the stack self.vm_writer.write_push(self.vm_writer.THAT, 0) # closing bracket tt, t = self._token_next(False, "SYMBOL", "]") # advance for the next statement self.tokenizer.advance() else: # none of above - just a single identifier segment, index = self._resolve_symbol(t) self.vm_writer.write_push(segment, index) else: # unknown print "WARNING: Unknown term expression object:", tt, t # compiles a (possible empty) comma-separated list of expressions def compile_expression_list(self): num_args = 0 # check for empty list tt, t = self._token_next(False) if tt == "SYMBOL" and t == ")": # the parameter list was empty; do not process any more pass else: # there are things in the parameter list while True: # expression to pass self.compile_expression() num_args += 1 # possible comma tt, t = self._token_next(False) if tt == "SYMBOL" and t == ",": self.tokenizer.advance() else: # not a comma; stop processing parameters break return num_args # compiles a subroutine call # two cases: # - subroutineName(expressionList) # - (class|var).subroutineName(expressionList) def compile_subroutine_call(self): # first part of name tt, name1 = self._token_next(False, "IDENTIFIER") # a dot and another name may exist, or it could be a parenthesis name2 = None tt, t = self._token_next(True) if tt == "SYMBOL" and t == ".": # the name after the dot tt, name2 = self._token_next(True, "IDENTIFIER") # advance so that we are on the parenthesis self.tokenizer.advance() # determine if this is a method call # three possibilities # - class.func() - function call # - var.func() - method call # - func() - method call on current object if self.symbol_table.contains(name1): method_call = True local_call = False elif name2 == None: method_call = True local_call = True else: method_call = False # if a method call, push variable name1 # this a method call if the symbol table contains name1 and name2 exists # OR name1 is a method in the current object if method_call and local_call: # push the current object onto the stack as a hidden argument self.vm_writer.write_push(self.vm_writer.POINTER, 0) elif method_call and not local_call: # push the variable onto the stack as a hidden argument segment, index = self._resolve_symbol(name1) self.vm_writer.write_push(segment, index) # opening parenthesis tt, t = self._token_next(False, "SYMBOL", "(") # expression list self.tokenizer.advance() num_args = self.compile_expression_list() # closing parenthesis tt, t = self._token_next(False, "SYMBOL", ")") # write the call if method_call and local_call: # methd + <blank> # get the name of the vm function to call classname = self.class_name vm_function_name = classname + "." + name1 # increase arguments by 1, since there is the hidden "this" num_args += 1 # make the call self.vm_writer.write_call(vm_function_name, num_args) elif method_call and not local_call: # variable name + method # get the name of the vm function to call classname = self.symbol_table.get(name1)[1] vm_function_name = classname + "." + name2 # increase arguments by 1, since there is the hidden "this" num_args += 1 # make the call self.vm_writer.write_call(vm_function_name, num_args) else: # get the name of the vm function to call vm_function_name = name1 + "." + name2 # make the call self.vm_writer.write_call(vm_function_name, num_args) self.tokenizer.advance() # returns the token_type and token of the next token after advancing the # tokenizer before reading if advance is True def _token_next(self, advance=False, expected_type=None, expected_value=None): # advance the tokenizer, if requested if advance: self.tokenizer.advance() # get the token type and the token itself token_type = self.tokenizer.token_type() token = str(getattr(self.tokenizer, token_type.lower())()) if expected_type and token_type != expected_type: print "WARNING: Type", token_type, "found; expected", expected_type import traceback, sys traceback.print_stack() sys.exit(1) if expected_value and token != expected_value: print "WARNING: Value", token, "found; expected", expected_value import traceback, sys traceback.print_stack() sys.exit(1) return token_type, token # convets a symbol table type into a segment type def _type_to_segment(self, type): if type == self.symbol_table.STATIC: return self.vm_writer.STATIC elif type == self.symbol_table.FIELD: return self.vm_writer.THIS elif type == self.symbol_table.ARG: return self.vm_writer.ARG elif type == self.symbol_table.VAR: return self.vm_writer.LOCAL else: print "ERROR: Bad type %s" % (str(type)) # resolves the symbol from the symbol table # the segment and index is returned as a 2-tuple def _resolve_symbol(self, name): kind, type, index = self.symbol_table.get(name) return self._type_to_segment(kind), index
class IlGenerator(object): @staticmethod def generate(modules, module, ast): self = IlGenerator(modules, module) self.stmts(self.function().new_block(), ast) return self.module def __init__(self, modules, module): self.modules = modules self.module = module self.functions = [ self.module.new_function('main', types.Function([], types.UNIT)) ] self.symbols = SymbolTable() def function(self): return self.functions[-1] def push_function(self, name, type, params, freevars): # Modifying new function to take in function reference. fn = self.module.new_function(name, type, params=params, parent=self.function().ref(), freevars=freevars) self.functions.append(fn) return fn def pop_function(self): return self.functions.pop() def new_register(self, regtype): return self.function().new_register(regtype) def stmts(self, blk, n): self.symbols = self.symbols.push() for kid in n.children: blk = self.stmt(blk, kid) self.symbols = self.symbols.pop() return blk def stmt(self, blk, n): return self.dispatch_stmt( blk, n, { "import": self.import_action, "decl": self.decl_action, "assign": self.assign_action, "expr-stmt": self.expr_stmt_action, "print": self.print_action, "if": self.if_action, "while": self.while_action, "label": self.label_action, "break": self.break_action, "continue": self.continue_action, "function": self.function_action, "return": self.return_action, "stmts": self.stmts, }) def import_action(self, blk, n): # Create a new module. print("IMport") modules = generate(n.children[1]) print("Done") for mod in modules: self.modules.add(mod) return blk def decl_action(self, blk, n): name = n.children[0].value dest = self.new_register(n.children[1].type) a, blk = self.expr(blk, dest, n.children[1]) self.symbols[name] = a return blk def assign_action(self, blk, n): name = n.children[0].value dest = self.symbols[name] a, blk = self.expr(blk, dest, n.children[1]) return blk def print_action(self, blk, n): a, blk = self.expr(blk, None, n.children[0]) if isinstance(a, il.FunctionRef): closure = self.module.lookup(a).closure(self.module, self.symbols) if len(closure.captured) > 0: a, blk = self.create_closures(blk, closure) blk.append(il.Instruction(il.OPS['PRINT'], a, None, None)) return blk def return_action(self, blk, n): a, blk = self.expr(blk, None, n.children[0]) if isinstance(a, il.FunctionRef): closure = self.module.lookup(a).closure(self.module, self.symbols) if len(closure.captured) > 0: a, blk = self.create_closures(blk, closure) blk.append(il.Instruction(il.OPS['RTRN'], a, None, None)) return blk def create_closures(self, blk, closure): ## Implements function closure creation registers = list() rewrite = dict() for name, operand in closure.captured.iteritems(): if isinstance(operand, il.Closure): fn_ref = operand.fn operand, blk = self.create_closures(blk, operand) rewrite[fn_ref] = il.ClosureRegister(len(registers), fn_ref.type()) registers.append(operand) result = self.new_register(closure.fn.type()) rewrite[closure.fn] = il.ClosureRegister(len(registers), closure.fn.type()) for idx, reg in enumerate(registers): rewrite[reg] = il.ClosureRegister(idx, reg.type()) closure_code = self.rewrite(closure.fn, rewrite) blk.append( il.Instruction(il.OPS['CLOSURE'], closure_code.ref(), registers, result)) return result, blk def rewrite(self, fn_ref, rewrites): ## Implements function rewriting for closure creation old = self.module.lookup(fn_ref) new = self.push_function(old.name + '-closure', old.type(), old.params, list()) new.locals = old.locals def replace(operand): if isinstance(operand, il.FunctionRef) and operand in rewrites: return rewrites[operand] elif isinstance(operand, il.Register) and operand in rewrites: return rewrites[operand] elif isinstance(operand, il.Register) and operand.fn == old.ref(): return il.Register(operand.id, new.ref(), operand.type()) elif isinstance(operand, list): return [replace(inner) for inner in operand] return operand for old_blk in old.blocks: new_blk = new.new_block() for inst in old_blk.code: new_blk.append( il.Instruction(inst.op, replace(inst.a), replace(inst.b), replace(inst.result))) for idx, old_blk in enumerate(old.blocks): new_blk = new.blocks[idx] for link in old_blk.next: new_blk.link_to(new.blocks[link.target], link.link_type) self.pop_function() return new def if_action(self, entry, n): body = self.function().new_block() afterwards = self.function().new_block() # Evaluate the condition expr. cond, cond_out = self.expr(entry, None, n.children[0]) # Evaluate the body of the conditional. body_out = self.stmts(body, n.children[1]) # Always goes to the afterwards block after if loop. body_out.goto_link(afterwards) # If the cond is true, go to body else to afterwards. cond_out.if_link(cond, body, afterwards) return afterwards def while_action(self, entry, n): header = self.function().new_block() body = self.function().new_block() afterwards = self.function().new_block() return self._while_action(entry, n, header, body, afterwards) def _while_action(self, entry, n, header, body, afterwards): self.function().push_loop(header, afterwards) entry.goto_link(header) cond, cond_out = self.expr(header, None, n.children[0]) cond_out.if_link(cond, body, afterwards) body_out = self.stmts(body, n.children[1]) body_out.goto_link(header) self.function().pop_loop() return afterwards def label_action(self, entry, n): # Blocks for continue, body and exit. continue_blk = self.function().new_block() body_blk = self.function().new_block() exit_blk = self.function().new_block() # Add label to the symbol table mapping to a LabeledLoop. label_name = n.children[0].value self.symbols[label_name] = il.LabeledLoop(continue_blk, exit_blk) # Use the internal while action to pass your own continue, body and exit_blks. afterwards = self._while_action(entry, n.children[1], continue_blk, body_blk, exit_blk) return afterwards def break_action(self, blk, n): # If no children, exit out of the nearest loop. if len(n.children) == 0: exit_blk = self.function().loop_exit() else: # Find the labelled loop record in the symbol_table. # Retrieve the exit blk of the loop. label = n.children[0].value exit_blk = self.symbols[label].exit_blk # Add a goto link to the exit block of the corresponding loop. blk.goto_link(exit_blk) # Create a new empty block and return for other instructions to be added. dead = self.function().new_block() return dead def continue_action(self, blk, n): if len(n.children) == 0: header = self.function().loop_cont() else: label = n.children[0].value header = self.symbols[label].continue_blk blk.goto_link(header) dead = self.function().new_block() return dead def expr_stmt_action(self, blk, n): _, blk = self.expr(blk, None, n.children[0]) return blk def function_action(self, blk, n): function_type = n.children[2].value name = n.children[0].value body = n.children[3] params = [ il.Param(id=idx, name=param.children[0].value, type=param.type) for idx, param in enumerate(n.children[1].children) ] free = freevars(n) # Push the function into the function list. # self.function() returns this function now. fn = self.push_function(name, function_type, params, free) # Add function reference to the symbol table of the calling function. self.symbols[fn.name] = fn.ref() # Push a new symbol table for the function. self.symbols = self.symbols.push() # Create a new block for the function. function_block = fn.new_block() for i, param in enumerate(params): # Create a local register for each parameter. param_register = fn.new_register(param.type) # Add the parameter to symbol_table. self.symbols[param.name] = param_register # Append PRM instructions to the function_block. # This instruction gives the id of each parameter which can be # referenced by the called function to lookup the parameter value # in the params of the function frame. function_block.append( il.Instruction(il.OPS['PRM'], il.Constant(param.id, param.type), None, param_register)) # Fill out instructions in the function_block. self.stmts(function_block, body) # Remove the function from the function list. # After this call, self.function() should not return this function. self.pop_function() return blk def expr(self, blk, result, n): return self.dispatch_expr( blk, result, n, { "negate": self.negate, "+": self.binop(il.OPS['ADD']), "-": self.binop(il.OPS['SUB']), "*": self.binop(il.OPS['MUL']), "/": self.binop(il.OPS['DIV']), "%": self.binop(il.OPS['MOD']), "==": self.binop(il.OPS['EQ']), "!=": self.binop(il.OPS['NE']), "<": self.binop(il.OPS['LT']), ">": self.binop(il.OPS['GT']), "<=": self.binop(il.OPS['LE']), ">=": self.binop(il.OPS['GE']), "not": self.not_op, "&&": self.and_op, "||": self.or_op, "call": self.call, "NAME": self.name, "INTEGER": self.number, "FLOAT": self.number, }) def negate(self, blk, result, n): if result is None: result = self.new_register(n.type) a, blk = self.expr(blk, None, n.children[0]) blk.append( il.Instruction(il.OPS['SUB'], il.Constant(0, n.type), a, result)) return result, blk def binop(self, op): def binop(blk, result, n): if result is None: result = self.new_register(n.type) a, blk = self.expr(blk, None, n.children[0]) b, blk = self.expr(blk, None, n.children[1]) blk.append(il.Instruction(op, a, b, result)) return result, blk return binop def not_op(self, blk, result, n): if not result: result = self.new_register(n.type) a, blk = self.expr(blk, result, n.children[0]) blk.append(il.Instruction(il.OPS['NOT'], a, None, result)) return result, blk def and_op(self, a_in_blk, result, n): if result is None: result = self.new_register(n.type) a, a_out_blk = self.expr(a_in_blk, result, n.children[0]) b_in_blk = self.function().new_block() exit_blk = self.function().new_block() # If 1st condition true, go to second block, else exit block. a_out_blk.if_link(a, b_in_blk, exit_blk) # on-true on-false b, b_out_blk = self.expr(b_in_blk, result, n.children[1]) b_out_blk.goto_link(exit_blk) return result, exit_blk def or_op(self, a_in_blk, result, n): if result is None: result = self.new_register(n.type) a, a_out_blk = self.expr(a_in_blk, result, n.children[0]) b_in_blk = self.function().new_block() exit_blk = self.function().new_block() a_out_blk.if_link(a, exit_blk, b_in_blk) # on-true on-false b, b_out_blk = self.expr(b_in_blk, result, n.children[1]) b_out_blk.goto_link(exit_blk) return result, exit_blk def call(self, blk, result, n): function_name = n.children[0].value exprs = n.children[1] parameters = [] # Resolve each parameter for expr in exprs.children: r, _ = self.expr(blk, None, expr) parameters.append(r) if result is None: result = self.function().new_register(n.type) # SymbolTable returns the function reference. blk.append( il.Instruction(il.OPS['CALL'], self.symbols.get(function_name), parameters, result)) return result, blk def name(self, blk, result, n): if result is None: return self.symbols[n.value], blk blk.append( il.Instruction(il.OPS['MV'], self.symbols[n.value], None, result)) return result, blk def number(self, blk, result, n): const = il.Constant(n.value, n.type) if result is None: return const, blk blk.append(il.Instruction(il.OPS['IMM'], const, None, result)) return result, blk def dispatch_stmt(self, blk, n, labels_to_actions): for name, func in labels_to_actions.iteritems(): if n.label == name: return func(blk, n) raise IlGenException("got '{}', want one of: {}".format( n.label, labels_to_actions.keys())) def dispatch_expr(self, blk, result, n, labels_to_actions): for name, func in labels_to_actions.iteritems(): if n.label == name: return func(blk, result, n) raise IlGenException("got '{}', want one of: {}".format( n.label, labels_to_actions.keys()))