Exemplo n.º 1
0
        public CILVariableDecl CreateStaticGlobal(SourceInfo si, CILType type, int pointerDepth, string name)
        {
            var global = new CILVariableDecl(si, type, pointerDepth, name, true);

            AddGlobal(global);
            return(global);
        }
Exemplo n.º 2
0
        public CILVariableDecl ToCILVariableDecl(CIntermediateLang cil)
        {
            // TODO: Do propery type checking between declared type and inferred/rhs type
            LllType realType;

            if (Type != null)
            {
                realType = Type.ToLllType();
            }
            else
            {
                realType = AssigningValue.TryInferType(cil);
                if (realType.IsAReference)
                {
                    // Auto-dereference the type...
                    Type = new AstType(SourceInfo, realType.Name, realType.PointerDepth - 1, 0, false, false);
                }
                else
                {
                    Type = new AstType(SourceInfo, realType.Name, realType.PointerDepth, 0, realType.IsAReference, false);
                }
            }
            var cName = NameGenerator.UniqName("var", Name);
            var cType = cil.SymTable.LookupType(realType.CName);

            if (IsFixedArray)
            {
                if (AssigningValue != null)
                {
                    throw new NotImplementedException("Assigning to a fixed size array is not implemented.");
                }
                return(new CILFixedArray(SourceInfo, cType, realType.PointerDepth, cName, Type.FixedArraySize));
            }

            // covers the case of declaration and function paramters
            if (AssigningValue == null)
            {
                var decl = new CILVariableDecl(SourceInfo, cType, realType.PointerDepth, cName);
                return(decl);
            }
            var val           = AssigningValue.ToCILExpression(cil);
            var srcTy         = AssigningValue.TryInferType(cil);
            var fixedPtrDepth = srcTy.PointerDepth;

            if (srcTy.IsAReference && !Type.IsAReference)
            {
                --fixedPtrDepth;
                val = new CILDereference(SourceInfo, val);
            }
            return(new CILVariableDecl(SourceInfo, cType, fixedPtrDepth, cName, val));
        }
Exemplo n.º 3
0
 public void CDefine(CIntermediateLang cil)
 {
     var lllStruct = LllCompiler.SymTable.LookupStruct(Name);
     foreach (var m in Members)
     {
         lllStruct.AddMember(m);
         var mType = LllCompiler.SymTable.LookupType(m.Type.TypeName);
         var cType = cil.SymTable.LookupType(mType.CName);
         CILVariableDecl member;
         if (m.IsFixedArray)
         {
             member = new CILFixedArray(m.SourceInfo, cType, m.Type.PointerDepth, m.Name, m.Type.FixedArraySize);
         }
         else {
             member = new CILVariableDecl(m.SourceInfo, cType, m.Type.PointerDepth, m.Name);
         }
         _cilStruct.AddMember(member);
     }
 }
Exemplo n.º 4
0
        public override CILExpression ToCILExpression(CIntermediateLang cil)
        {
            var type    = TryInferType(cil).Clone(PointerDepth - 1, false);
            var @sizeof = new CILSizeof(SourceInfo, string.Format("{0}{1}", type.CName, new string('*', type.PointerDepth)));
            var size    = new CILBinaryOp(SourceInfo, Expression.ToCILExpression(cil), CILBinaryOp.OpType.Mul, @sizeof);
            var alloc   = new CILCall(SourceInfo, new CILIdent(SourceInfo, "malloc"), new List <CILExpression> {
                size
            });
            var tmpName = NameGenerator.NewTemp();
            var tmp     = new CILVariableDecl(SourceInfo, cil.SymTable.LookupType(type.CName), PointerDepth, tmpName, alloc);
            var rewrite = new CILRewriteExpression(SourceInfo, new List <CILNode>
            {
                tmp,
                new CILIdent(SourceInfo, tmp.Name)
            });

            return(rewrite);

            throw new NotImplementedException("TODO: Implement AstNewArrayOp");
        }
Exemplo n.º 5
0
 public void DeclareLocalVariable(CILVariableDecl variable)
 {
     SymTable.DeclareVar(variable);
 }
Exemplo n.º 6
0
 public void AddGlobal(CILVariableDecl global)
 {
     SymTable.DeclareGlobalVar(global);
     _globals.Add(global.Name, global);
 }
Exemplo n.º 7
0
 public void DeclareGlobalVar(CILVariableDecl variable)
 {
     _symTable.Last().Add(variable.Name, variable);
 }
Exemplo n.º 8
0
 public void DeclareVar(CILVariableDecl variable)
 {
     _symTable.Peek().Add(variable.Name, variable);
 }