def for_expression( variable: TranslatedExpression, lo: TranslatedExpression, hi: TranslatedExpression, body: TranslatedExpression, break_label: TempLabel, ) -> TranslatedExpression: test_label = TempManager.new_label() body_label = TempManager.new_label() limit = TempManager.new_temp() variable_expression = convert_to_expression(variable) sequence = Sequence([ Move(variable_expression, convert_to_expression(lo)), Move(Temporary(limit), convert_to_expression(hi)), Label(test_label), ConditionalJump( RelationalOperator.le, variable_expression, Temporary(limit), body_label, break_label, ), Label(body_label), convert_to_statement(body), Move( variable_expression, BinaryOperation(BinaryOperator.plus, variable_expression, Constant(1)), ), Jump(Name(test_label), [test_label]), Label(break_label), ]) return NoResult(sequence)
def if_expression( test: TranslatedExpression, then: TranslatedExpression, else_do: Optional[TranslatedExpression], ) -> TranslatedExpression: test_condition = convert_to_condition(test) then_expression = convert_to_expression(then) else_expression = (convert_to_expression(else_do) if else_do is not None else Constant(0)) true_label = TempManager.new_label() false_label = TempManager.new_label() join_label = TempManager.new_label() result = TempManager.new_temp() patch_true_labels(test_condition.trues, true_label) patch_false_labels(test_condition.trues, false_label) sequence = Sequence([ test_condition.statement, Label(true_label), Move(Temporary(result), then_expression), Jump(Name(join_label), [join_label]), Label(false_label), Move(Temporary(result), else_expression), Label(join_label), ]) return Expression(EvaluateSequence(sequence, Temporary(result)))
def record_expression( field_list: List[TranslatedExpression]) -> TranslatedExpression: result = TempManager.new_temp() creation_sequence = [ Move( Temporary(result), frame.external_call("init_record", [Constant(len(field_list) * frame.word_size)]), ) ] for index, field_expression in enumerate(field_list): field_allocation = Move( Memory( BinaryOperation( BinaryOperator.plus, Temporary(result), Constant(index * frame.word_size), )), convert_to_expression(field_expression), ) creation_sequence.append(field_allocation) return Expression( EvaluateSequence(Sequence(creation_sequence), Temporary(result)))
def convert_to_expression(exp: TranslatedExpression) -> tree.Expression: if isinstance(exp, Expression): return exp.expression if isinstance(exp, NoResult): return EvaluateSequence(exp.statement, Constant(0)) if isinstance(exp, Conditional): temporary_value = TempManager.new_temp() true = TempManager.new_label() false = TempManager.new_label() patch_true_labels(exp.condition.trues, true) patch_false_labels(exp.condition.falses, false) return EvaluateSequence( Move(Temporary(temporary_value), Constant(1)), EvaluateSequence( exp.condition.statement, EvaluateSequence( Label(false), EvaluateSequence( Move(Temporary(temporary_value), Constant(0)), EvaluateSequence(Label(true), Temporary(temporary_value)), ), ), ), )
def reorder( expression_list: List[Expression] ) -> Tuple[Statement, List[Expression]]: if not expression_list: return noop_statement(), [] if isinstance(expression_list[0], Call): temporary = TempManager.new_temp() expression_list[0] = EvaluateSequence( Move(Temporary(temporary), expression_list[0]), Temporary(temporary)) return reorder(expression_list) head_statement, head_expression = do_expression(expression_list[0]) tail_statement, tail_expressions = reorder(expression_list[1:]) if commute(tail_statement, head_expression): return ( simplified_sequence(head_statement, tail_statement), [head_expression] + tail_expressions, ) temporary = TempManager.new_temp() return ( simplified_sequence( simplified_sequence(head_statement, Move(Temporary(temporary), head_expression)), tail_statement, ), [Temporary(temporary)] + tail_expressions, )
def simple_variable(access: Access, level: Level) -> TranslatedExpression: result = Temporary(frame.frame_pointer()) current_level = level while current_level is not access.level: static_link_access = current_level.formals()[0] result = frame.access_to_exp(static_link_access.access, result) current_level = current_level.parent return Expression(frame.access_to_exp(access.access, result))
def proc_entry_exit(function_level: RealLevel, body: TranslatedExpression): body_statement = Move(Temporary(frame.return_value()), convert_to_expression(body)) proc_statement = frame.preserve_callee_registers( function_level.frame, frame.shift_view(function_level.frame, body_statement)) FragmentManager.add_fragment( ProcessFragment(proc_statement, function_level.frame))
def call_expression( function_label: TempLabel, function_level: RealLevel, caller_level: RealLevel, argument_list: List[TranslatedExpression], ) -> TranslatedExpression: argument_expressions = [ convert_to_expression(argument) for argument in argument_list ] if function_label in BaseEnvironmentManager.standard_library_functions: return Expression( frame.external_call(function_label, argument_expressions)) static_link_expression = Temporary(frame.frame_pointer()) current_level = caller_level while current_level is not function_level.parent: current_static_link = current_level.formals()[0] static_link_expression = frame.access_to_exp( current_static_link.access, static_link_expression) current_level = current_level.parent return Expression( Call(Name(function_label), [static_link_expression] + argument_expressions))