def record_create(obj): t = env.look(_tenv, obj.children[0]) addr = tree.Temp(env.new_temp()) if not t: error(obj.pos, 'no such type : %s' % (obj.children[0])) return None if t[0] != 'TY_RECORD': error(obj.pos, 'type is not record : %s' % (obj.children[0])) return None size = len(t[1]) if len(obj.children[1]) != size: error( obj.pos, 'need %d items, but give %d items in record: %s' % (len(obj.children[1]), size, obj.children[0])) i = 0 record = [] while i < size: index = find_record_index(t[1], obj.children[1][i].children[0]) if index is None: error( obj.pos, "record create error, type %s has no such item : \'%s\'" % (obj.children[0], obj.children[1][i].children[0])) value = build_IR_tree_exp(obj.children[1][i]) location = tree.Binop('+', addr, tree.Const(index * frame.WORD_SIZE)) record.append(tree.Move(tree.Mem(location), value)) i += 1 alloc = tree.Call(tree.Name("__recordCreate__"), [size]) r = [tree.Move(addr, alloc)] + record return tree.Eseq(ir_seq_list(r), addr, t)
def if_then_else_exp(obj): t = env.new_label() f = env.new_label() done = env.new_label() result = env.new_temp() e1 = build_IR_tree_exp(obj.children[0]) if e1.ty != 'TY_INT': error(obj.pos, "if condition must be int not %s" % (e1.ty)) e2 = build_IR_tree_exp(obj.children[1]) e3 = build_IR_tree_exp(obj.children[2]) if (e2.ty != e3.ty) \ and not(((type(e2.ty) is list) and (e2.ty[0] == 'TY_RECORD') and (e3.ty == 'TY_NIL')) \ or ((type(e3.ty) is list) and (e3.ty[0] == 'TY_RECORD') and (e2.ty == 'TY_NIL'))): error(obj.pos, "then type : %s doesn't match else type : %s" % (e2.ty, e3.ty)) return tree.Eseq( ir_seq_list([ un_cx(e1, t, f), tree.Label(t), tree.Move(tree.Mem(result), e2), tree.Jump(tree.Name(done), [done]), tree.Label(f), tree.Move(tree.Mem(result), e3), tree.Label(done) ]), tree.Temp(result), e2.ty)
def alloc_local(frame, escape): if escape: access = ['InFrame', (-1 - frame['localNum']) * WORD_SIZE] frame['localNum'] += 1 else: access = ['InReg', env.new_temp()] frame['locals'].append(access) return access
def fv(): global _rv if _rv == None: _rv = env.new_temp() return _rv
def fp(): global _fp if _fp == None: _fp = env.new_temp() return _fp
def string_exp(obj): # return name(obj.children[0]) lab = env.new_temp() frame.add_string(lab, obj.children[0]) return tree.Name(lab, 'TY_STRING')