Ejemplo n.º 1
0
def new_node_num( val ):
    #print('#newnode num {0}'.format(val))
    newnode = asmd.Node()
    newnode.kind = ND.NUM
    newnode.val = val
    newnode.type = asmd.TypeType( TYP.INT,  4, 4, 0, 0, 0)

    return newnode
Ejemplo n.º 2
0
def new_lvar2( name, ty ):
    global locals2

    v = new_var2( name, ty, True )
    locals2[ name ] = v
    
    node = asmd.Node()
    node.kind = ND.NOP
    return node
Ejemplo n.º 3
0
def new_node( kind, lhs, rhs ):
    newnode = asmd.Node()
    newnode.kind = kind
    newnode.lhs = lhs
    newnode.rhs = rhs

    if kind == ND.ADD:
        newnode.type = asmd.TypeType( TYP.INT,  4, 4, 0, 0, 0)
    if kind == ND.LVAR:
        pass
    if kind == ND.ADDR:
        newnode.type = asmd.TypeType( TYP.PTR,  8, 8, 0, 0, 0)
    if kind == ND.DEREF:
        pass
    
    return newnode
Ejemplo n.º 4
0
    def primary_IDENT() :

        #print('######TK_IDENT')

        if saki( '(', 1 ):
            newnode = asmd.NodeFUNCCALL()
            newnode.kind = ND.FUNC
            newnode.name = asmd.tkn[0].str
            del asmd.tkn[0]
            del asmd.tkn[0]

            #
            # 関数呼び出しの場合
            #
            if consume( ')' ):
                return newnode
            
            while True :
                para = expr();
                newnode.para.append( para )

                if consume( ')' ) :
                    break
                if not consume( ',' ):
                    raise asmd.ManncError('関数呼び出し方が不正です' )
                    # sys.exit()
            return newnode

        else :
            #
            # 変数(左辺値)の場合
            #
            if asmd.tkn[0].str in asmd.glvars_t:
                #グローバル変数として宣言されている場合
                newnode = asmd.Node()
                newnode.kind = ND.LVAR
                newnode.str = asmd.tkn[0].str
                newnode.size = asmd.glvars_t[ asmd.tkn[0].str ].size
                newnode.type = asmd.glvars_t[ asmd.tkn[0].str ]
                del asmd.tkn[0]
                return newnode

            # リファクタリング用
            if asmd.tkn[0].str in locals2.keys():
                print('#0922test{0}'.format( asmd.tkn[0].str) )
                newnode = asmd.Node()
                newnode.kind = ND.LVAR2
                newnode.name2 = asmd.tkn[0].str #Nodeに変数名は必要ない?
                newnode.size = locals2[ asmd.tkn[0].str ].ty.size
                newnode.offset = locals2[ asmd.tkn[0].str ].offset
                newnode.type = locals2[ asmd.tkn[0].str ]
                del asmd.tkn[0]
                return newnode

            if not asmd.tkn[0].str in asmd.lvars :
                raise asmd.ManncError('宣言されていない変数を使用しようとしています<{0}><{1}>'.format(asmd.tkn[0].str, asmd.lvars ) )

            #配列ではない場合
            newnode = asmd.Node()
            newnode.kind = ND.LVAR
            newnode.str = asmd.tkn[0].str
            newnode.size = asmd.lvars_t[ asmd.tkn[0].str ].size

            newnode.offset = asmd.lvars[ asmd.tkn[0].str ]
            newnode.type = asmd.lvars_t[ asmd.tkn[0].str ]

            del asmd.tkn[0]
            return newnode
Ejemplo n.º 5
0
def primary():
    def primary_IDENT() :

        #print('######TK_IDENT')

        if saki( '(', 1 ):
            newnode = asmd.NodeFUNCCALL()
            newnode.kind = ND.FUNC
            newnode.name = asmd.tkn[0].str
            del asmd.tkn[0]
            del asmd.tkn[0]

            #
            # 関数呼び出しの場合
            #
            if consume( ')' ):
                return newnode
            
            while True :
                para = expr();
                newnode.para.append( para )

                if consume( ')' ) :
                    break
                if not consume( ',' ):
                    raise asmd.ManncError('関数呼び出し方が不正です' )
                    # sys.exit()
            return newnode

        else :
            #
            # 変数(左辺値)の場合
            #
            if asmd.tkn[0].str in asmd.glvars_t:
                #グローバル変数として宣言されている場合
                newnode = asmd.Node()
                newnode.kind = ND.LVAR
                newnode.str = asmd.tkn[0].str
                newnode.size = asmd.glvars_t[ asmd.tkn[0].str ].size
                newnode.type = asmd.glvars_t[ asmd.tkn[0].str ]
                del asmd.tkn[0]
                return newnode

            # リファクタリング用
            if asmd.tkn[0].str in locals2.keys():
                print('#0922test{0}'.format( asmd.tkn[0].str) )
                newnode = asmd.Node()
                newnode.kind = ND.LVAR2
                newnode.name2 = asmd.tkn[0].str #Nodeに変数名は必要ない?
                newnode.size = locals2[ asmd.tkn[0].str ].ty.size
                newnode.offset = locals2[ asmd.tkn[0].str ].offset
                newnode.type = locals2[ asmd.tkn[0].str ]
                del asmd.tkn[0]
                return newnode

            if not asmd.tkn[0].str in asmd.lvars :
                raise asmd.ManncError('宣言されていない変数を使用しようとしています<{0}><{1}>'.format(asmd.tkn[0].str, asmd.lvars ) )

            #配列ではない場合
            newnode = asmd.Node()
            newnode.kind = ND.LVAR
            newnode.str = asmd.tkn[0].str
            newnode.size = asmd.lvars_t[ asmd.tkn[0].str ].size

            newnode.offset = asmd.lvars[ asmd.tkn[0].str ]
            newnode.type = asmd.lvars_t[ asmd.tkn[0].str ]

            del asmd.tkn[0]
            return newnode

    if asmd.tkn[0].kind == TK.STRING :
        # 文字リテラルはグローバル変数として扱う
        # .dataセグメントに書き込む?
        # ラベル名=変数名になる
        #print('#parse string')
        newnode = asmd.Node()
        newnode.kind = ND.LVAR
        newnode.str = asmd.tkn[0].str
        newnode.size = len( asmd.tkn[0].str )
        newnode.type = asmd.TypeType( TYP.ARRAY,
                                        newnode.size+1,
                                        1,
                                        newnode.size + 1,
                                        asmd.TypeType( TYP.INT,  4, 4, 0, 0, 0),
                                        0)
        asmd.glvars_t[ newnode.str ] = newnode.type

        del asmd.tkn[0]
        return newnode

    #print('# primary()')
    if asmd.tkn[0].kind == TK.NUM :
        #print('#TK_NUMMMM')
        return new_node_num( expect_number() )

    if asmd.tkn[0].kind == TK.IDENT :
        #print('#primary_IDENT')
        return primary_IDENT()

    if consume( '(' ):
        node = expr()
        expect( ')' );
        return node

    # 必要?
    return new_node_num( expect_number() )
Ejemplo n.º 6
0
 def stmt_continue():
     if not consume( ';' ):
         raise asmd.ManncError('continue の後は ; が必要です')
     node = asmd.Node()
     node.kind = ND.CONTINUE
     return node
Ejemplo n.º 7
0
 def stmt_break():
     if not consume( ';' ):
         raise asmd.ManncError('break の後は ; が必要です')
     node = asmd.Node()
     node.kind = ND.BREAK
     return node
Ejemplo n.º 8
0
def stmt():
    #print("# stmt")
    #
    # 関数内関数
    #

    # declaration の最終目標↓
    # declaration = basetype declarator type-suffix ("=" lvar-initializer)? ";"
    #             | basetype ";"

    def stmt_declaration2():

        # declaration = basetype declarator ";"

        ty = basetype2()
        ( dummy, name ) = declarator2()
        #if not consume( ';' ) :
            #raise asmd.ManncError('宣言文 の後は ; が必要です')
        
        return new_lvar2( name, ty )
        
    def stmt_switch():
        if not consume( '(' ):
            raise asmd.ManncError('switch の後は ( が必要です')
        node = asmd.NodeSWITCH()
        node.kind = ND.SWITCH
        node.expr = expr()
        if not consume( ')' ) :
            raise asmd.ManncError('switch の ''('' 後は '')'' が必要です')

        sw = asmd.current_switch
        asmd.current_switch = node
        node.block = stmt()
        asmd.current_switch = sw
        
        return node

    def stmt_case():

        print("#CASE")
        if asmd.current_switch == 0:
            raise asmd.ManncError('stray case')
        val = expect_number()
        print("#VAL {0}".format(val))

        expect( ':' )
        node = asmd.NodeCASE()
        node.kind = ND.CASE
        node.block = stmt()
        node.val = val
        node.case_next = asmd.current_switch.case_next
        asmd.current_switch.case_next = node
        return node

    def stmt_break():
        if not consume( ';' ):
            raise asmd.ManncError('break の後は ; が必要です')
        node = asmd.Node()
        node.kind = ND.BREAK
        return node
        
    def stmt_continue():
        if not consume( ';' ):
            raise asmd.ManncError('continue の後は ; が必要です')
        node = asmd.Node()
        node.kind = ND.CONTINUE
        return node
        
    def stmt_while():
        if not consume( '(' ):
            raise asmd.ManncError('while の後は ( が必要です')
        node = asmd.NodeWHILE()
        node.kind = ND.WHILE
        node.expr = expr()

        if not consume( ')' ) :
            raise asmd.ManncError('while の ''('' 後は '')'' が必要です')
        node.block = stmt()
        
        return node

    def stmt_if():
        #print('#IF')
        if not consume( '(' ) :
            raise asmd.ManncError('if の後は ( が必要です')
        node = asmd.NodeIF()
        node.kind = ND.IF
        node.expr = expr()
        if not consume( ')' ) :
            raise asmd.ManncError('if の ''('' 後は '')'' が必要です')
        node.truebl = stmt()
        if consume_tk( TK.ELSE ) :
            node.elsebl = stmt()

        return node
    #
    # 関数内関数 おわり 
    #
    if consume( ';' ) :
        node = asmd.Node()
        node.kind = ND.NOP
        return node

    if consume( '{' ) :
        #print('#print BLOCK start')
        node = asmd.NodeBLOCK()
        node.kind = ND.BLOCK

        if consume( '}' ):
            return node

        while True:
            newstmt = stmt()
            node.stmts.append( newstmt )
            if consume( '}' ):
                break;
        
        return node

    # 変数の宣言文の場合    
    if asmd.tkn[0].kind in ( TK.INT, TK.CHAR ):
        tknkind = asmd.tkn[0].kind 
        
        ( thistype, vname ) =  type.declaration( 'NOTPARA' )
        print('#var dec')
        print('#{0}'.format(vname))
        thistype.myself()
        if thistype.base != 0 :
            thistype.base.myself()

        asmd.lvars_t[ vname ] = thistype
        asmd.offset = asmd.align_to( asmd.offset, thistype.align )
        asmd.offset += asmd.lvars_t[ vname ].size
        asmd.lvars[ vname ] = asmd.MYVar()
        asmd.lvars[ vname ].offset = asmd.offset
        print('#offset aaaaa')
        print('#offset  {0}'.format( asmd.offset ) )
        print('#lvars   {0}'.format( asmd.lvars[ vname ].offset ) )
        #print('#lvars_t {0}'.format( asmd.lvars_t[ vname ].offset ) )
        #del asmd.tkn[0]

        return 0
        
    if is_typename2():
        return stmt_declaration2()
    if consume_tk( TK.SWITCH ):
        return stmt_switch()
    if consume_tk( TK.CASE ):
        return stmt_case()
    if consume_tk( TK.CONTINUE ):
        return stmt_continue()
    if consume_tk( TK.BREAK ):
        return stmt_break()
    if consume_tk( TK.WHILE ):
        return stmt_while()
    if consume_tk( TK.IF ):
        return stmt_if()
    if consume_tk( TK.RETURN ) :
        node = asmd.Node()
        node.kind = ND.RETURN
        node.lhs = expr()
    else :
        node = expr()

    if consume( ';' ):
        return node
    else:
        raise asmd.ManncError(';ではないトークンです{0}'.format(asmd.tkn[0].str))