Esempio n. 1
0
    def visit(self, node, context):
        self.visit(node.left_expression, context)
        code = node.left_expression.code
        localvars = node.left_expression.locals

        self.visit(node.right_expression, context)
        code += node.right_expression.code
        localvars += node.right_expression.locals

        local_int_left = context.define_local()
        localvars.append(local_int_left)
        cil_getattr1 = ast_cil.CILGetAttr(node.left_expression.value, 0)
        cil_assign1 = ast_cil.CILAssignment(local_int_left, cil_getattr1)
        code.append(cil_assign1)

        local_int_right = context.define_local()
        localvars.append(local_int_right)
        cil_getattr2 = ast_cil.CILGetAttr(node.right_expression.value, 0)
        cil_assign2 = ast_cil.CILAssignment(local_int_right, cil_getattr2)
        code.append(cil_assign2)

        self.num_labels += 1
        cil_notnone_label = ast_cil.CILLabel('NOT_NONE' + str(self.num_labels))
        self.num_labels += 1
        cil_divisionby0_label = ast_cil.CILLabel('DIVISION_BY_0' +
                                                 str(self.num_labels))

        cil_condition = ast_cil.CILCondition(local_int_right,
                                             cil_notnone_label.label)
        code.append(cil_condition)

        code.append(cil_divisionby0_label)  # DIVISION_BY_0
        cil_goto = ast_cil.CILGoTo('_divide_by_0')
        code.append(cil_goto)

        code.append(cil_notnone_label)  # NOT_NONE

        local_int_content = context.define_local()
        localvars.append(local_int_content)

        cil_div = ast_cil.CILDiv(local_int_left, local_int_right)
        cil_assign3 = ast_cil.CILAssignment(local_int_content, cil_div)
        code.append(cil_assign3)

        local_value = context.define_local()
        localvars.append(local_value)
        local_int_tag = context.define_local(self.types_dict['Int'])
        localvars.append(local_int_tag)

        cil_allocate = ast_cil.CILAllocate(local_int_tag)
        cil_assign4 = ast_cil.CILAssignment(local_value, cil_allocate)
        cil_setattr = ast_cil.CILSetAttr(local_value, 0, local_int_content)
        code += [cil_assign4, cil_setattr]

        node.value = local_value
        node.locals = localvars
        node.code = code
Esempio n. 2
0
    def visit(self, node, context):
        self.visit(node.expression, context)
        code = node.expression.code
        localvars = node.expression.locals

        local_one = context.define_local(1)
        localvars.append(local_one)

        local_bool_content = context.define_local()
        localvars.append(local_bool_content)

        cil_getattr = ast_cil.CILGetAttr(node.expression.value, 0)
        cil_assign1 = ast_cil.CILAssignment(local_bool_content, cil_getattr)
        code.append(cil_assign1)

        local_bool_value = context.define_local()
        localvars.append(local_bool_value)

        cil_neg = ast_cil.CILMinus(local_one, local_bool_content)
        cil_assign2 = ast_cil.CILAssignment(local_bool_value, cil_neg)
        code.append(cil_assign2)

        local_value = context.define_local()
        localvars.append(local_value)
        local_bool_tag = context.define_local(self.types_dict['Bool'])
        localvars.append(local_bool_tag)

        cil_allocate = ast_cil.CILAllocate(local_bool_tag)
        cil_assign3 = ast_cil.CILAssignment(local_value, cil_allocate)
        cil_setattr = ast_cil.CILSetAttr(local_value, 0, local_bool_value)
        code += [cil_assign3, cil_setattr]

        node.value = local_value
        node.locals = localvars
        node.code = code
Esempio n. 3
0
    def visit(self, node, context):
        localvars = []
        code = []

        vinfo = context.find_variable(node.name)

        if vinfo is None:
            # check if this variable is an attr
            attr = self.cilProgram.dotTYPES.types[
                self.current_type.name].attributes.get(node.name)
            if attr is None:
                # throw an error because var is not defined(this shouldn't happend)
                pass

            local_value = context.define_local()
            localvars.append(local_value)

            cil_getattr = ast_cil.CILGetAttr(context.self, attr.offset)
            cil_assign = ast_cil.CILAssignment(local_value, cil_getattr)
            code.append(cil_assign)
        else:
            local_value = vinfo.cil_name

        node.value = local_value
        node.locals = localvars
        node.code = code
Esempio n. 4
0
    def visit(self, node, context):
        self.visit(node.left_expression, context)
        code = node.left_expression.code
        localvars = node.left_expression.locals

        self.visit(node.right_expression, context)
        code += node.right_expression.code
        localvars += node.right_expression.locals

        local_int_left = context.define_local()
        localvars.append(local_int_left)
        cil_getattr1 = ast_cil.CILGetAttr(node.left_expression.value, 0)
        cil_assign1 = ast_cil.CILAssignment(local_int_left, cil_getattr1)
        code.append(cil_assign1)

        local_int_right = context.define_local()
        localvars.append(local_int_right)
        cil_getattr2 = ast_cil.CILGetAttr(node.right_expression.value, 0)
        cil_assign2 = ast_cil.CILAssignment(local_int_right, cil_getattr2)
        code.append(cil_assign2)

        local_int_content = context.define_local()
        localvars.append(local_int_content)

        cil_mult = ast_cil.CILMult(local_int_left, local_int_right)
        cil_assign3 = ast_cil.CILAssignment(local_int_content, cil_mult)
        code.append(cil_assign3)

        local_value = context.define_local()
        localvars.append(local_value)
        local_int_tag = context.define_local(self.types_dict['Int'])
        localvars.append(local_int_tag)

        cil_allocate = ast_cil.CILAllocate(local_int_tag)
        cil_assign4 = ast_cil.CILAssignment(local_value, cil_allocate)
        cil_setattr = ast_cil.CILSetAttr(local_value, 0, local_int_content)
        code += [cil_assign4, cil_setattr]

        node.value = local_value
        node.locals = localvars
        node.code = code
Esempio n. 5
0
    def visit(self, node, context):
        localvars = []
        code = []

        self.num_labels += 1
        cil_while_label = ast_cil.CILLabel('WHILE' + str(self.num_labels))
        self.num_labels += 1
        cil_loop_label = ast_cil.CILLabel('LOOP' + str(self.num_labels))
        self.num_labels += 1
        cil_pool_label = ast_cil.CILLabel('POOL' + str(self.num_labels))

        local_while_value = context.define_local()
        localvars.append(local_while_value)

        code.append(cil_while_label)  # WHILE

        self.visit(node.while_expression, context)
        localvars += node.while_expression.locals
        code += node.while_expression.code

        cil_getattr = ast_cil.CILGetAttr(node.while_expression.value, 0)
        cil_assign = ast_cil.CILAssignment(local_while_value, cil_getattr)
        code.append(cil_assign)

        cil_condition = ast_cil.CILCondition(local_while_value,
                                             cil_loop_label.label)
        code.append(cil_condition)

        cil_goto1 = ast_cil.CILGoTo(cil_pool_label.label)
        code.append(cil_goto1)

        code.append(cil_loop_label)  # LOOP
        self.visit(node.loop_expression, context)
        localvars += node.loop_expression.locals
        code += node.loop_expression.code

        cil_goto2 = ast_cil.CILGoTo(cil_while_label.label)
        code.append(cil_goto2)

        code.append(cil_pool_label)  # POOL
        # return void
        local_value = context.define_local()
        localvars.append(local_value)

        node.value = local_value
        node.locals = localvars
        node.code = code
Esempio n. 6
0
    def visit(self, node, context):
        self.visit(node.if_expression, context)
        localvars = node.if_expression.locals
        code = node.if_expression.code

        local_value = context.define_local()
        localvars.append(local_value)

        self.num_labels += 1
        cil_then_label = ast_cil.CILLabel('THEN' + str(self.num_labels))
        self.num_labels += 1
        cil_end_label = ast_cil.CILLabel('END' + str(self.num_labels))

        local_if_value = context.define_local()
        localvars.append(local_if_value)

        cil_getattr = ast_cil.CILGetAttr(node.if_expression.value, 0)
        cil_assign1 = ast_cil.CILAssignment(local_if_value, cil_getattr)
        code.append(cil_assign1)

        cil_condition = ast_cil.CILCondition(local_if_value,
                                             cil_then_label.label)
        code.append(cil_condition)

        self.visit(node.else_expression, context)
        localvars += node.else_expression.locals
        code += node.else_expression.code
        cil_assign2 = ast_cil.CILAssignment(
            local_value, ast_cil.CILVar(node.else_expression.value))
        code.append(cil_assign2)

        cil_goto = ast_cil.CILGoTo(cil_end_label.label)
        code.append(cil_goto)

        code.append(cil_then_label)  # THEN
        self.visit(node.then_expression, context)
        localvars += node.then_expression.locals
        code += node.then_expression.code
        cil_assign3 = ast_cil.CILAssignment(
            local_value, ast_cil.CILVar(node.then_expression.value))
        code.append(cil_assign3)

        code.append(cil_end_label)  # END

        node.value = local_value
        node.locals = localvars
        node.code = code