Exemple #1
0
def p_selection_statement_1(p):
    '''selection_statement : IF '(' expression ')' statement'''
    if_l, l_if = cg.label("if")
    endif_l, l_endif = cg.label("endif")
    p[0] = {}
    p[0]['alloc'] = p[5].get('alloc','')
    cmp = cg.compare(p[3], if_l, endif_l)
    l3 = "br label %{}".format(endif_l)
    p[0]['code'] = p[3]['code'] + cmp + l_if + p[5]['code'] + cg.indent(l3) + l_endif
Exemple #2
0
def p_iteration_statement_1(p):
    '''iteration_statement : WHILE '(' expression ')' statement'''
    while_cmp_l, l_cmp = cg.label("while_cmp")
    while_body_l, l_body = cg.label("while_body")
    while_end_l, l_end = cg.label("while_end")
    p[0] = {}
    p[0]['alloc'] = p[5].get('alloc','')
    br_cmp = "br label %{}".format(while_cmp_l)
    cmp = cg.compare(p[3], while_body_l, while_end_l)
    p[0]['code'] = (cg.indent(br_cmp) + l_cmp + p[3]['code'] + cmp + l_body
        + p[5]['code'] + cg.indent(br_cmp) + cg.indent(l_end,0))
Exemple #3
0
def p_selection_statement_2(p):
    '''selection_statement : IF '(' expression ')' statement ELSE statement'''
    if_l, l_if = cg.label("if")
    else_l, l_else = cg.label("else")
    endif_l, l_endif = cg.label("endif")
    p[0] = {}
    p[0]['alloc'] = p[5].get('alloc','') + p[7].get('alloc','')
    cmp = cg.compare(p[3]['id'], if_l, else_l)
    br_endif = "br label %{}".format(endif_l)
    p[0]['code'] = (p[3]['code'] + cmp + cg.indent(l_if, 0) + p[5]['code']
        + cg.indent(br_endif) + l_else + p[7]['code'] + cg.indent(br_endif) + l_endif)
Exemple #4
0
def p_iteration_statement_2(p):
    '''iteration_statement : DO statement WHILE '(' expression ')' ';' '''
    while_body_l, l_body = cg.label("do_while_body")
    while_cmp_l, l_cmp = cg.label("do_while_cmp")
    while_end_l, l_end = cg.label("do_while_end")
    p[0] = {}
    p[0]['alloc'] = p[2].get('alloc','')
    br_body = "br label %{}".format(while_body_l)
    cmp = cg.compare(p[5], while_body_l, while_end_l)
    br_cmp = "br label %{}".format(while_cmp_l)
    p[0]['code'] = (cg.indent(br_body) + l_body + p[2]['code'] + cg.indent(br_cmp)
        + l_cmp + p[5]['code'] + cmp + l_end)
Exemple #5
0
def p_iteration_statement_3(p):
    '''iteration_statement : FOR new_scope '(' expression_statement expression_statement expression ')' statement
                            | FOR new_scope '(' declaration expression_statement expression ')' statement'''
    for_cmp, l_cmp = cg.label("for_cmp")
    for_incr, l_incr = cg.label("for_incr")
    for_body, l_body = cg.label("for_body")
    for_end, l_end = cg.label("for_end")
    p[0] = {}
    p[0]['alloc'] = p[4].get('alloc','')
    p[0]['alloc'] += p[8].get('alloc', '')
    br_cmp = "br label %{}".format(for_cmp)
    br_incr = "br label %{}".format(for_incr)
    cmp = cg.compare(p[5], for_body, for_end)
    p[0]['code'] = (p[4]['code'] + cg.indent(br_cmp) + l_cmp + p[5]['code']
        + cmp + l_incr + p[6]['code'] + cg.indent(br_cmp)
        + l_body + p[8]['code'] + cg.indent(br_incr) + l_end)

    m.symbols.leave_scope()