Exemple #1
0
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)),
                    ),
                ),
            ),
        )
Exemple #3
0
def field_variable(variable: TranslatedExpression,
                   field_index: int) -> TranslatedExpression:
    return Expression(
        Memory(
            BinaryOperation(
                BinaryOperator.plus,
                convert_to_expression(variable),
                BinaryOperation(
                    BinaryOperator.mul,
                    Constant(field_index),
                    Constant(frame.word_size),
                ),
            )))
Exemple #4
0
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)
Exemple #5
0
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 convert_to_condition(exp: TranslatedExpression) -> tree.Condition:
    if isinstance(exp, Expression):
        jump = ConditionalJump(RelationalOperator.ne, exp.expression,
                               Constant(0))
        return tree.Condition(jump, [jump], [jump])

    if isinstance(exp, NoResult):
        raise Exception(
            "Trying to remove_conditional from a NoResult expression.")

    if isinstance(exp, Conditional):
        return exp.condition
Exemple #7
0
def subscript_variable(
        variable: TranslatedExpression,
        subscript: TranslatedExpression) -> TranslatedExpression:
    return Expression(
        Memory(
            BinaryOperation(
                BinaryOperator.plus,
                convert_to_expression(variable),
                BinaryOperation(
                    BinaryOperator.mul,
                    convert_to_expression(subscript),
                    Constant(frame.word_size),
                ),
            )))
Exemple #8
0
def string_conditional_operation_expression(
        operator: ast.Oper, left: TranslatedExpression,
        right: TranslatedExpression) -> TranslatedExpression:
    jump_expression = ConditionalJump(
        convert_conditional_operator(operator),
        frame.external_call(
            "string_compare",
            [convert_to_expression(left),
             convert_to_expression(right)],
        ),
        Constant(0),
    )
    return Conditional(
        Condition(jump_expression, [jump_expression], [jump_expression]))
Exemple #9
0
def while_expression(test: TranslatedExpression, body: TranslatedExpression,
                     break_label: TempLabel) -> TranslatedExpression:
    test_label = TempManager.new_label()
    body_label = TempManager.new_label()

    sequence = Sequence([
        Label(test_label),
        ConditionalJump(
            RelationalOperator.ne,
            convert_to_expression(test),
            Constant(0),
            body_label,
            break_label,
        ),
        Label(body_label),
        convert_to_statement(body),
        Jump(Name(test_label), [test_label]),
        Label(break_label),
    ])

    return NoResult(sequence)
Exemple #10
0
def integer_expression(integer: int) -> TranslatedExpression:
    return Expression(Constant(integer))
Exemple #11
0
def nil_expression() -> TranslatedExpression:
    return Expression(Constant(0))
Exemple #12
0
def empty_expression() -> TranslatedExpression:
    return NoResult(StatementExpression(Constant(0)))
Exemple #13
0
def noop_statement() -> Statement:
    return StatementExpression(Constant(0))