Beispiel #1
0
 def visit_FuncDef(self,funcdef):
     functype = types.parseTypeDecl(self.typeTab,funcdef.decl)
     self.symTab.addSymbol(GlobalSym(funcdef.decl.name,functype))
     self.curFunction = function.Function(funcdef.decl.name)
     self.curFunctionType = functype
     self.module.addFunction(self.curFunction)
     self.curBasicBlock = basicblock.BasicBlock()
     self.curFunction.setEntryBlock(self.curBasicBlock)
     self.symTab.pushScope()
     
     if funcdef.decl.type.args:
         for param in funcdef.decl.type.args.params:
             t = types.parseTypeDecl(self.typeTab,param)
             if type(t) not in [types.Pointer,types.Int,types.Char]:
                 raise Exception("cant handle type %s as a param yet" % t)
             
             sym = ParamSym(param.name,t)
             self.symTab.addSymbol(sym)
             self.curFunction.addArgumentSlot(sym.slot)
     
     self.symTab.pushScope()
     self.inFunctionDispatch(funcdef.body)
     self.symTab.popScope()
     self.symTab.popScope()
     
     if self.curBasicBlock.unsafeEnding():
         retval = ir.I32()
         self.curBasicBlock.append(ir.LoadConstant(retval,ir.ConstantI32(0)))
         self.curBasicBlock.append(ir.Ret(retval))
Beispiel #2
0
def test_typeMatching2():
    typeTable = types.TypeTable()
    ast = astFromString(t3)
 
    for i in range(3):
        a,b = i*2,i*2+1
        #print a,b
        typea = types.parseTypeDecl(typeTable,ast.ext[a])
        typeb = types.parseTypeDecl(typeTable,ast.ext[b])
        assert(typea.strictTypeMatch(typeb) == False)
Beispiel #3
0
    def visit_Decl(self,decl):
        t = types.parseTypeDecl(self.typeTab,decl.type)
        isstaticvar = 'static' in decl.storage
        if self.isGlobalScope() or isstaticvar:
            sym = GlobalSym(decl.name,t)
            if type(decl.type) != c_ast.FuncDecl:
                self.module.addZeroInitData(4,label=decl.name)
        else:
            sym = LocalSym(decl.name,t)
        self.symTab.addSymbol(sym)
        if not self.isGlobalScope() and not isstaticvar:
            if decl.init:
                if type(decl.type) == c_ast.ArrayDecl:
                    raise Exception("cannot currently handle Array initializers")
                elif type(decl.type) == c_ast.PtrDecl:
                    raise Exception("cannot currently handle Pointer initializers")
                initializer = self.inFunctionDispatch(decl.init)
                self.assertNonVoid(initializer)
                if not t.strictTypeMatch(initializer.type):
                    initializer = operatorgen.genCast(self.curBasicBlock,initializer,t)
                v = ir.Pointer()
                op = ir.LoadLocalAddr(v,sym)
                self.curBasicBlock.append(op)

                if initializer.lval:
                    initializer = self.genDeref(initializer)
                op = ir.Store(v,initializer.reg)
                self.curBasicBlock.append(op)
Beispiel #4
0
def test_IntTypeParsing():
    
    typeTable = types.TypeTable()
    ast = astFromString(t4)
    assert(len(t4solutions) == len(ast.ext))
    for i,ideal in enumerate(t4solutions):

        parsed = types.parseTypeDecl(typeTable,ast.ext[i])
        print i
        print ideal
        print parsed
        matches = ideal.strictTypeMatch(parsed)
        assert(matches == True)
Beispiel #5
0
def test_typeParsing():
    typeTable = types.TypeTable()
    ast = astFromString(t1)
    #ast.show()
    parsed = types.parseTypeDecl(typeTable,ast.ext[0])
    
    for t in [parsed,typeTable.lookupType('Foo',isStructType=True)]:
        assert(type(t) == types.Struct)
        assert(t.name == 'Foo')
        assert(type(t.getMember('x')) == types.Int)
        assert(type(t.getMember('y')) == types.Int)
        assert(t.getMemberOffset('x') == 0)

    parsed = types.parseTypeDecl(typeTable,ast.ext[1])

    for t in [parsed,typeTable.lookupType('Foo2',isStructType=True)]:
        assert(type(t) == types.Struct)
        assert(t.name == 'Foo2')
        assert(type(t.getMember('moo')) == types.Int)
        assert(t.getMemberOffset('moo') == 0)

    t = types.parseTypeDecl(typeTable,ast.ext[2])

    assert(type(t) == types.Struct)
    assert(t.name == None)
    assert(type(t.getMember('x')) == types.Int)
    assert(t.getMemberOffset('x') == 0)

    parsed = types.parseTypeDecl(typeTable,ast.ext[3])
    assert(type(parsed) == types.Int)
            
    #pointer to int
    parsed = types.parseTypeDecl(typeTable,ast.ext[4])
    assert(type(parsed) == types.Pointer)
    assert(type(parsed.type) == types.Int)
    
    
    parsed = types.parseTypeDecl(typeTable,ast.ext[5])
    for t in [parsed,typeTable.lookupType('Foo',isStructType=True)]:
        assert(type(t) == types.Struct)
        assert(t.name == 'Foo')
        assert(type(t.getMember('x')) == types.Int)
        assert(type(t.getMember('y')) == types.Int)
        assert(t.getMemberOffset('x') == 0)

    parsed = types.parseTypeDecl(typeTable,ast.ext[6])
    assert(type(parsed) == types.Pointer)
    assert(type(parsed.type) == types.Struct)
    assert(parsed.type.name == None)
    
    parsed = types.parseTypeDecl(typeTable,ast.ext[7])
    assert(type(parsed) == types.Pointer)
    assert(type(parsed.type) == types.Struct)
    assert(parsed.type.name == "Foobar")
    assert(typeTable.lookupType('Foobar',isStructType=True) != None)
    
    parsed = types.parseTypeDecl(typeTable,ast.ext[8])
    assert(type(parsed) == types.Array)
    assert(type(parsed.type) == types.Int)
    assert(parsed.length == 10)
    
    parsed = types.parseTypeDecl(typeTable,ast.ext[9])
    assert(type(parsed) == types.Pointer )
    
    parsed = types.parseTypeDecl(typeTable,ast.ext[10])
    assert(type(parsed) == types.Struct)  
    assert(parsed.name == "unqualified")
    
    parsed = types.parseTypeDecl(typeTable,ast.ext[11])
    assert(type(parsed) == types.Pointer)
    assert(type(parsed.type) == types.Function)
    assert(type(parsed.type.rettype) == types.Int)
    assert(type(parsed.type.args[0]) == types.Int)
    assert(len(parsed.type.args) == 2)
Beispiel #6
0
 def visit_Cast(self,cast):
     totype = types.parseTypeDecl(self.typeTab,cast.to_type)
     return operatorgen.genCast(self.curBasicBlock,self.inFunctionDispatch(cast.expr),totype)
Beispiel #7
0
 def visit_typeDef(self,td):
     t = types.parseTypeDecl(self.typeTab,td.type)
     self.typeTab.registerType(td.name,t,isTypedef=True)