Exemple #1
0
def p_declarator_2(p):
    '''declarator : direct_declarator'''
    p[0] = {}
    p[0] = p[1]
    modif = '' if m.symbols.level() == 1 else '*'
    if m.symbols.level() == 1 and not 'func' in p[0]: #function arguments are copied
        p[0]['code'] = [p[1]['code']]
        arg_id = p[0]['code'][0] + m.suffix
        m.symbols.add(arg_id, last_type_seen, '')
        m.symbols.add(p[0]['code'][0], last_type_seen, '*')
        l1 = "{0} = alloca {1}".format(m.symbols.var(p[0]['code'][0]), m.symbols.dereference(p[0]['code'][0]))
        l2 = cg.assign({'id' : arg_id}, p[0]['code'][0], 'declarator function arg')
        p[0]['init'] = cg.indent(l1) + l2
        p[0]['modif'] = ['']
    else:
        p[0]['code'] = [p[1]['code']]
        p[0]['modif'] = [modif]
        if 'size' in p[1]:
            if 'declared' in p[1]:
                m.symbols.set_size(p[0]['code'][0], p[1]['size'])
            else:
                m.symbols.add(p[0]['code'][0], last_type_seen, p[0]['modif'][0], p[1]['size'])
        else:
            if not 'declared' in p[1]:
                m.symbols.add(p[0]['code'][0], last_type_seen, p[0]['modif'][0])

    global last_var_seen
    last_var_seen = p[0]['code'][0]
Exemple #2
0
def p_unary_expression_3(p):
    '''unary_expression : DEC_OP unary_expression'''
    p[0] = {}
    res2 = cg.binary_operation(p[2], {'code': '', 'const': '', 'val': '1', 'type': m._type_int}, '-')
    res3 = cg.assign(res2, p[2]['id'], "pre_decrement")
    p[0]['code'] = res2['code'] + res3
    p[0]['id'] = res2['id']
Exemple #3
0
def p_primary_expression_11(p):
    '''primary_expression : postfix_expression DEC_OP'''
    p[0] = {}
    res, tmp1 = cg.load(p[1]['id'], "post_decrement")
    res2 = cg.binary_operation(p[1], {'code': '', 'const': '', 'val': '1', 'type': m._type_int}, '-')
    res3 = cg.assign(res2, p[1]['id'], "post_increment")
    p[0]['code'] = tmp1 + res2['code'] + res3
    p[0]['id'] = res
Exemple #4
0
def p_declaration_1(p):
    '''declaration : type_specifier init_declarator_list ';' '''
    p[0] = {}
    p[0]['code'] = ''
    p[0]['alloc'] = ''
    for i in range(len(p[2]['code'])):
        l = "{0} = alloca {1}".format(m.symbols.var(p[2]['code'][i]), m.symbols.dereference(p[2]['code'][i]))
        p[0]['alloc'] += cg.indent(l)

    for i in range(len(p[2]['code'])):
        if p[2]['assignement'][i]:
            p[0]['code'] += p[2]['assignement'][i]['code']
            p[0]['code'] += cg.assign(p[2]['assignement'][i], p[2]['code'][i], 'declaration')
Exemple #5
0
def p_expression_1(p):
    '''expression : unary_expression assignment_operator relational_expression'''
    p[0] = {}
    if p[2] == '=':
        src = p[3]
    elif p[2] == '+=':
        src = cg.binary_operation({'id':p[1]['id'], 'code':''}, p[3], '+')
    elif p[2] == '-=':
        src = cg.binary_operation({'id':p[1]['id'], 'code':''}, p[3], '-')
    elif p[2] == '*=':
        src = cg.binary_operation({'id':p[1]['id'], 'code':''}, p[3], '*')

    p[0]['code'] = p[1]['code'] + src['code'] + cg.assign(src, p[1]['id'], 'expression')
    global left_var
    left_var = p[1]['id']