Ejemplo n.º 1
0
        public override void CGenStmt(Env env, CGenState state)
        {
            Int32 start_label    = state.RequestLabel();
            Int32 finish_label   = state.RequestLabel();
            Int32 continue_label = state.RequestLabel();

            // start:
            state.CGenLabel(start_label);

            // Body
            state.InLoop(continue_label, finish_label);
            this.Body.CGenStmt(env, state);
            state.OutLabels();

            state.CGenLabel(continue_label);

            // test Cond
            Reg ret = CGenExprStmt(env, this.Cond, state);

            CGenTest(ret, state);

            state.JNZ(start_label);

            state.CGenLabel(finish_label);
        }
Ejemplo n.º 2
0
        public override Reg CGenValue(CGenState state)
        {
            Int32 label_set = state.label_idx;

            state.label_idx++;
            Int32 label_finish = state.label_idx;

            state.label_idx++;

            Reg ret = this.Left.CGenValue(state);

            switch (ret)
            {
            case Reg.EAX:
                state.TESTL(Reg.EAX, Reg.EAX);
                state.JNZ(label_set);
                break;

            case Reg.ST0:
                state.FLDZ();
                state.FUCOMIP();
                state.FSTP(Reg.ST0);
                state.JNZ(label_set);
                break;

            default:
                throw new InvalidProgramException();
            }

            ret = this.Right.CGenValue(state);
            switch (ret)
            {
            case Reg.EAX:
                state.TESTL(Reg.EAX, Reg.EAX);
                state.JNZ(label_set);
                break;

            case Reg.ST0:
                state.FLDZ();
                state.FUCOMIP();
                state.FSTP(Reg.ST0);
                state.JNZ(label_set);
                break;

            default:
                throw new InvalidProgramException();
            }

            state.MOVL(0, Reg.EAX);

            state.JMP(label_finish);

            state.CGenLabel(label_set);

            state.MOVL(1, Reg.EAX);

            state.CGenLabel(label_finish);

            return(Reg.EAX);
        }
Ejemplo n.º 3
0
        public override void CGenStmt(Env env, CGenState state)
        {
            Int32 label = state.CaseLabel(this.Value);

            state.CGenLabel(label);
            this.Stmt.CGenStmt(env, state);
        }
Ejemplo n.º 4
0
        public override void CGenStmt(Env env, CGenState state)
        {
            Int32 label = state.DefaultLabel;

            state.CGenLabel(label);
            this.Stmt.CGenStmt(env, state);
        }
Ejemplo n.º 5
0
        //
        //          test Cond
        //          jz false ---+
        //          true_expr   |
        // +------- jmp finish  |
        // |    false: <--------+
        // |        false_expr
        // +--> finish:
        //
        public override Reg CGenValue(CGenState state)
        {
            Int32 stack_size = state.StackSize;
            Reg   ret        = this.Cond.CGenValue(state);

            state.CGenForceStackSizeTo(stack_size);

            // test Cond
            switch (ret)
            {
            case Reg.EAX:
                state.TESTL(Reg.EAX, Reg.EAX);
                break;

            case Reg.ST0:
                /// Compare Expr with 0.0
                /// < see cref = "BinaryComparisonOp.OperateFloat(CGenState)" />
                state.FLDZ();
                state.FUCOMIP();
                state.FSTP(Reg.ST0);
                break;

            default:
                throw new InvalidProgramException();
            }

            Int32 false_label  = state.RequestLabel();
            Int32 finish_label = state.RequestLabel();

            state.JZ(false_label);

            this.TrueExpr.CGenValue(state);

            state.JMP(finish_label);

            state.CGenLabel(false_label);

            ret = this.FalseExpr.CGenValue(state);

            state.CGenLabel(finish_label);

            return(ret);
        }
Ejemplo n.º 6
0
        public override void CGenStmt(Env env, CGenState state)
        {
            Reg ret = CGenExprStmt(env, this.Cond, state);

            CGenTest(ret, state);

            Int32 false_label  = state.RequestLabel();
            Int32 finish_label = state.RequestLabel();

            state.JZ(false_label);

            this.TrueStmt.CGenStmt(env, state);

            state.JMP(finish_label);

            state.CGenLabel(false_label);

            this.FalseStmt.CGenStmt(env, state);

            state.CGenLabel(finish_label);
        }
Ejemplo n.º 7
0
        public override void CGenStmt(Env env, CGenState state)
        {
            // Init
            this.Init.Map(_ => CGenExprStmt(env, _, state));

            Int32 start_label    = state.RequestLabel();
            Int32 finish_label   = state.RequestLabel();
            Int32 continue_label = state.RequestLabel();

            // start:
            state.CGenLabel(start_label);

            // test cont
            this.Cond.Map(_ => {
                Reg ret = CGenExprStmt(env, _, state);
                CGenTest(ret, state);
                return(ret);
            });

            // jz finish
            state.JZ(finish_label);

            // Body
            state.InLoop(continue_label, finish_label);
            this.Body.CGenStmt(env, state);
            state.OutLabels();

            // continue:
            state.CGenLabel(continue_label);

            // Loop
            this.Loop.Map(_ => CGenExprStmt(env, _, state));

            // jmp start
            state.JMP(start_label);

            // finish:
            state.CGenLabel(finish_label);
        }
Ejemplo n.º 8
0
        public void CGenDecln(Env env, CGenState state)
        {
            //     .text
            //     [.globl <func>]
            // <func>:
            //     pushl %ebp
            //     movl %esp, %ebp
            //
            state.TEXT();
            Env.Entry entry = env.Find(this.name).Value;
            state.COMMENT(ToString());
            switch (entry.Kind)
            {
            case Env.EntryKind.GLOBAL:
                switch (this.scs)
                {
                case StorageClass.AUTO:
                case StorageClass.EXTERN:
                    state.GLOBL(this.name);
                    break;

                case StorageClass.STATIC:
                    // static definition
                    break;

                default:
                    throw new InvalidOperationException();
                }
                break;

            default:
                throw new InvalidOperationException();
            }
            state.CGenFuncStart(this.name);

            state.InFunction(GotoLabelsGrabber.GrabLabels(this.stmt));

            this.stmt.CGenStmt(env, state);

            state.CGenLabel(state.ReturnLabel);
            state.OutFunction();

            //     leave
            //     ret
            state.LEAVE();
            state.RET();
            state.NEWLINE();
        }
        public void CGenDecln(Env env, CGenState state) {
            //     .text
            //     [.globl <func>]
            // <func>:
            //     pushl %ebp
            //     movl %esp, %ebp
            // 
            state.TEXT();
            Env.Entry entry = env.Find(name).Value;
            state.COMMENT(ToString());
            switch (entry.kind) {
            case Env.EntryKind.GLOBAL:
                switch (scs) {
                case Decln.SCS.AUTO:
                case Decln.SCS.EXTERN:
                    state.GLOBL(name);
                    break;
                case Decln.SCS.STATIC:
                    // static definition
                    break;
                default:
                    throw new InvalidOperationException();
                }
                break;
            default:
                throw new InvalidOperationException();
            }
            state.CGenFuncStart(name);

            state.InFunction(GotoLabelsGrabber.GrabLabels(this.stmt));

            stmt.CGenStmt(env, state);

            state.CGenLabel(state.ReturnLabel);
            state.OutFunction();

            //     leave
            //     ret
            state.LEAVE();
            state.RET();
            state.NEWLINE();
        }
Ejemplo n.º 10
0
        //
        //          test cond
        //          jz false ---+
        //          true_expr   |
        // +------- jmp finish  |
        // |    false: <--------+
        // |        false_expr
        // +--> finish:
        //
        public override Reg CGenValue(Env env, CGenState state)
        {
            Int32 stack_size = state.StackSize;
            Reg ret = cond.CGenValue(env, state);
            state.CGenForceStackSizeTo(stack_size);

            // test cond
            switch (ret) {
                case Reg.EAX:
                    state.TESTL(Reg.EAX, Reg.EAX);
                    break;

                case Reg.ST0:
                    /// Compare expr with 0.0
                    /// < see cref = "BinaryArithmeticComp.OperateFloat(CGenState)" />
                    state.FLDZ();
                    state.FUCOMIP();
                    state.FSTP(Reg.ST0);
                    break;

                default:
                    throw new InvalidProgramException();
            }

            Int32 false_label = state.RequestLabel();
            Int32 finish_label = state.RequestLabel();

            state.JZ(false_label);

            true_expr.CGenValue(env, state);

            state.JMP(finish_label);

            state.CGenLabel(false_label);

            ret = false_expr.CGenValue(env, state);

            state.CGenLabel(finish_label);

            return ret;
        }
Ejemplo n.º 11
0
 public override void CGenStmt(Env env, CGenState state) {
     state.CGenLabel(state.GotoLabel(this.label));
     state.CGenForceStackSizeTo(state.StackSize);
     this.stmt.CGenStmt(env, state);
 }
Ejemplo n.º 12
0
        public override void CGenStmt(Env env, CGenState state) {

            // Inside a switch statement, the initializations are ignored,
            // but the stack size should be changed.
            List<Tuple<Env, Decln>> declns;
            List<Tuple<Env, Stmt>> stmts;
            switch (stmt.kind) {
                case Kind.COMPOUND:
                    declns = ((CompoundStmt)stmt).declns;
                    stmts = ((CompoundStmt)stmt).stmts;
                    break;
                default:
                    throw new NotImplementedException();
            }

            // Track all case values.
            IReadOnlyList<Int32> values = CaseLabelsGrabber.GrabLabels(this);

            // Make sure there are no duplicates.
            if (values.Distinct().Count() != values.Count) {
                throw new InvalidOperationException("case labels not unique.");
            }
            // Request labels for these values.
            Dictionary<Int32, Int32> value_to_label = values.ToDictionary(value => value, value => state.RequestLabel());

            Int32 label_finish = state.RequestLabel();

            Int32 num_default_stmts = stmts.Count(_ => _.Item2.kind == Kind.DEFAULT);
            if (num_default_stmts > 1) {
                throw new InvalidOperationException("duplicate defaults.");
            }
            Int32 label_default =
                num_default_stmts == 1 ?
                state.RequestLabel() :
                label_finish;

            Int32 saved_stack_size = state.StackSize;
            Int32 stack_size =
                declns.Any() ?
                declns.Last().Item1.StackSize :
                saved_stack_size;

            // 1. Evaluate expr.
            CGenExprStmt(env, expr, state);

            // 2. Expand stack.
            state.CGenForceStackSizeTo(stack_size);

            // 3. Make the Jump list.
            foreach (KeyValuePair<Int32, Int32> value_label_pair in value_to_label) {
                state.CMPL(value_label_pair.Key, Reg.EAX);
                state.JZ(value_label_pair.Value);
            }
            state.JMP(label_default);

            // 4. List all the statements.
            state.InSwitch(label_finish, label_default, value_to_label);
            foreach (Tuple<Env, Stmt> env_stmt_pair in stmts) {
                env_stmt_pair.Item2.CGenStmt(env_stmt_pair.Item1, state);
            }
            state.OutLabels();

            // 5. finish:
            state.CGenLabel(label_finish);
            
            // 6. Restore stack size.
            state.CGenForceStackSizeTo(saved_stack_size);
        }
Ejemplo n.º 13
0
        public override void CGenStmt(Env env, CGenState state) {
            // init
            init.Map(_ => CGenExprStmt(env, _, state));

            Int32 start_label = state.RequestLabel();
            Int32 finish_label = state.RequestLabel();
            Int32 continue_label = state.RequestLabel();

            // start:
            state.CGenLabel(start_label);

            // test cont
            cond.Map(_ => {
                Reg ret = CGenExprStmt(env, _, state);
                CGenTest(env, ret, state);
                return ret;
            });
            
            // jz finish
            state.JZ(finish_label);

            // body
            state.InLoop(continue_label, finish_label);
            body.CGenStmt(env, state);
            state.OutLabels();

            // continue:
            state.CGenLabel(continue_label);

            // loop
            loop.Map(_ => CGenExprStmt(env, _, state));

            // jmp start
            state.JMP(start_label);

            // finish:
            state.CGenLabel(finish_label);
        }
Ejemplo n.º 14
0
        public override void CGenStmt(Env env, CGenState state) {
            Int32 start_label = state.RequestLabel();
            Int32 finish_label = state.RequestLabel();
            Int32 continue_label = state.RequestLabel();

            // start:
            state.CGenLabel(start_label);

            // body
            state.InLoop(continue_label, finish_label);
            body.CGenStmt(env, state);
            state.OutLabels();

            state.CGenLabel(continue_label);

            // test cond
            Reg ret = CGenExprStmt(env, cond, state);
            CGenTest(env, ret, state);

            state.JNZ(start_label);

            state.CGenLabel(finish_label);
        }
Ejemplo n.º 15
0
        // * function;
        // * extern function;
        // * static function;
        // * obj;
        // * obj = Init;
        // * static obj;
        // * static obj = Init;
        // * extern obj;
        // * extern obj = Init;
        public void CGenDecln(Env env, CGenState state)
        {
            if (env.IsGlobal())
            {
                if (this.initr.IsSome)
                {
                    Initr initr = this.initr.Value;
                    switch (this.scs)
                    {
                    case StorageClass.AUTO:
                        state.GLOBL(this.name);
                        break;

                    case StorageClass.EXTERN:
                        throw new InvalidProgramException();

                    case StorageClass.STATIC:
                        break;

                    case StorageClass.TYPEDEF:
                        // Ignore.
                        return;

                    default:
                        throw new InvalidProgramException();
                    }

                    state.DATA();

                    state.ALIGN(ExprType.ALIGN_LONG);

                    state.CGenLabel(this.name);

                    Int32 last = 0;
                    initr.Iterate(this.type, (Int32 offset, Expr expr) => {
                        if (offset > last)
                        {
                            state.ZERO(offset - last);
                        }

                        if (!expr.IsConstExpr)
                        {
                            throw new InvalidOperationException("Cannot initialize with non-const expression.");
                        }

                        switch (expr.Type.Kind)
                        {
                        // TODO: without const char/short, how do I initialize?
                        case ExprTypeKind.CHAR:
                        case ExprTypeKind.UCHAR:
                        case ExprTypeKind.SHORT:
                        case ExprTypeKind.USHORT:
                            throw new NotImplementedException();

                        case ExprTypeKind.LONG:
                            state.LONG(((ConstLong)expr).Value);
                            break;

                        case ExprTypeKind.ULONG:
                            state.LONG((Int32)((ConstULong)expr).Value);
                            break;

                        case ExprTypeKind.POINTER:
                            state.LONG((Int32)((ConstPtr)expr).Value);
                            break;

                        case ExprTypeKind.FLOAT:
                            byte[] float_bytes = BitConverter.GetBytes(((ConstFloat)expr).Value);
                            Int32 intval       = BitConverter.ToInt32(float_bytes, 0);
                            state.LONG(intval);
                            break;

                        case ExprTypeKind.DOUBLE:
                            byte[] double_bytes = BitConverter.GetBytes(((ConstDouble)expr).Value);
                            Int32 first_int     = BitConverter.ToInt32(double_bytes, 0);
                            Int32 second_int    = BitConverter.ToInt32(double_bytes, 4);
                            state.LONG(first_int);
                            state.LONG(second_int);
                            break;

                        default:
                            throw new InvalidProgramException();
                        }

                        last = offset + expr.Type.SizeOf;
                    });
                }
                else
                {
                    // Global without initialization.

                    switch (this.scs)
                    {
                    case StorageClass.AUTO:
                        // .comm name,size,align
                        break;

                    case StorageClass.EXTERN:
                        break;

                    case StorageClass.STATIC:
                        // .local name
                        // .comm name,size,align
                        state.LOCAL(this.name);
                        break;

                    case StorageClass.TYPEDEF:
                        // Ignore.
                        return;

                    default:
                        throw new InvalidProgramException();
                    }

                    if (this.type.Kind != ExprTypeKind.FUNCTION)
                    {
                        state.COMM(this.name, this.type.SizeOf, ExprType.ALIGN_LONG);
                    }
                }

                state.NEWLINE();
            }
            else
            {
                // stack object

                state.CGenExpandStackTo(env.StackSize, ToString());

                Int32 stack_size = env.StackSize;

                // pos should be equal to stack_size, but whatever...
                Int32 pos = env.Find(this.name).Value.Offset;
                if (this.initr.IsNone)
                {
                    return;
                }

                Initr initr = this.initr.Value;
                initr.Iterate(this.type, (Int32 offset, Expr expr) => {
                    Reg ret = expr.CGenValue(state);
                    switch (expr.Type.Kind)
                    {
                    case ExprTypeKind.CHAR:
                    case ExprTypeKind.UCHAR:
                        state.MOVB(Reg.EAX, pos + offset, Reg.EBP);
                        break;

                    case ExprTypeKind.SHORT:
                    case ExprTypeKind.USHORT:
                        state.MOVW(Reg.EAX, pos + offset, Reg.EBP);
                        break;

                    case ExprTypeKind.DOUBLE:
                        state.FSTPL(pos + offset, Reg.EBP);
                        break;

                    case ExprTypeKind.FLOAT:
                        state.FSTPS(pos + offset, Reg.EBP);
                        break;

                    case ExprTypeKind.LONG:
                    case ExprTypeKind.ULONG:
                    case ExprTypeKind.POINTER:
                        state.MOVL(Reg.EAX, pos + offset, Reg.EBP);
                        break;

                    case ExprTypeKind.STRUCT_OR_UNION:
                        state.MOVL(Reg.EAX, Reg.ESI);
                        state.LEA(pos + offset, Reg.EBP, Reg.EDI);
                        state.MOVL(expr.Type.SizeOf, Reg.ECX);
                        state.CGenMemCpy();
                        break;

                    case ExprTypeKind.ARRAY:
                    case ExprTypeKind.FUNCTION:
                        throw new InvalidProgramException($"How could a {expr.Type.Kind} be in a init list?");

                    default:
                        throw new InvalidProgramException();
                    }

                    state.CGenForceStackSizeTo(stack_size);
                });
            } // stack object
        }
Ejemplo n.º 16
0
        public override Reg CGenValue(Env env, CGenState state) {
            Int32 label_set = state.label_idx;
            state.label_idx++;
            Int32 label_finish = state.label_idx;
            state.label_idx++;

            Reg ret = lhs.CGenValue(env, state);
            switch (ret) {
                case Reg.EAX:
                    state.TESTL(Reg.EAX, Reg.EAX);
                    state.JNZ(label_set);
                    break;

                case Reg.ST0:
                    state.FLDZ();
                    state.FUCOMIP();
                    state.FSTP(Reg.ST0);
                    state.JNZ(label_set);
                    break;

                default:
                    throw new InvalidProgramException();
            }

            ret = rhs.CGenValue(env, state);
            switch (ret) {
                case Reg.EAX:
                    state.TESTL(Reg.EAX, Reg.EAX);
                    state.JNZ(label_set);
                    break;

                case Reg.ST0:
                    state.FLDZ();
                    state.FUCOMIP();
                    state.FSTP(Reg.ST0);
                    state.JNZ(label_set);
                    break;

                default:
                    throw new InvalidProgramException();
            }

            state.MOVL(0, Reg.EAX);

            state.JMP(label_finish);

            state.CGenLabel(label_set);

            state.MOVL(1, Reg.EAX);

            state.CGenLabel(label_finish);

            return Reg.EAX;
        }
Ejemplo n.º 17
0
        public override void CGenStmt(Env env, CGenState state)
        {
            // Inside a switch statement, the initializations are ignored,
            // but the stack size should be changed.
            List <Tuple <Env, Decln> > declns;
            List <Tuple <Env, Stmt> >  stmts;

            var compoundStmt = this.Stmt as CompoundStmt;

            if (compoundStmt == null)
            {
                throw new NotImplementedException();
            }

            declns = compoundStmt.Declns;
            stmts  = compoundStmt.Stmts;

            // Track all case values.
            IReadOnlyList <Int32> values = CaseLabelsGrabber.GrabLabels(this);

            // Make sure there are no duplicates.
            if (values.Distinct().Count() != values.Count)
            {
                throw new InvalidOperationException("case labels not unique.");
            }
            // Request labels for these values.
            Dictionary <Int32, Int32> value_to_label = values.ToDictionary(value => value, value => state.RequestLabel());

            Int32 label_finish = state.RequestLabel();

            Int32 num_default_stmts = stmts.Count(_ => _.Item2 is DefaultStmt);

            if (num_default_stmts > 1)
            {
                throw new InvalidOperationException("duplicate defaults.");
            }
            Int32 label_default =
                num_default_stmts == 1 ?
                state.RequestLabel() :
                label_finish;

            Int32 saved_stack_size = state.StackSize;
            Int32 stack_size       =
                declns.Any() ?
                declns.Last().Item1.StackSize :
                saved_stack_size;

            // 1. Evaluate Expr.
            CGenExprStmt(env, this.Expr, state);

            // 2. Expand stack.
            state.CGenForceStackSizeTo(stack_size);

            // 3. Make the Jump list.
            foreach (KeyValuePair <Int32, Int32> value_label_pair in value_to_label)
            {
                state.CMPL(value_label_pair.Key, Reg.EAX);
                state.JZ(value_label_pair.Value);
            }
            state.JMP(label_default);

            // 4. List all the statements.
            state.InSwitch(label_finish, label_default, value_to_label);
            foreach (Tuple <Env, Stmt> env_stmt_pair in stmts)
            {
                env_stmt_pair.Item2.CGenStmt(env_stmt_pair.Item1, state);
            }
            state.OutLabels();

            // 5. finish:
            state.CGenLabel(label_finish);

            // 6. Restore stack size.
            state.CGenForceStackSizeTo(saved_stack_size);
        }
Ejemplo n.º 18
0
 public override void CGenStmt(Env env, CGenState state) {
     Int32 label = state.CaseLabel(value);
     state.CGenLabel(label);
     stmt.CGenStmt(env, state);
 }
Ejemplo n.º 19
0
 public override void CGenStmt(Env env, CGenState state) {
     Int32 label = state.DefaultLabel;
     state.CGenLabel(label);
     stmt.CGenStmt(env, state);
 }
Ejemplo n.º 20
0
        public override void CGenStmt(Env env, CGenState state) {
            Reg ret = CGenExprStmt(env, cond, state);

            CGenTest(env, ret, state);

            Int32 false_label = state.RequestLabel();
            Int32 finish_label = state.RequestLabel();

            state.JZ(false_label);

            true_stmt.CGenStmt(env, state);

            state.JMP(finish_label);

            state.CGenLabel(false_label);

            false_stmt.CGenStmt(env, state);

            state.CGenLabel(finish_label);

        }
Ejemplo n.º 21
0
 public override void CGenStmt(Env env, CGenState state)
 {
     state.CGenLabel(state.GotoLabel(this.Label));
     state.CGenForceStackSizeTo(state.StackSize);
     this.Stmt.CGenStmt(env, state);
 }
Ejemplo n.º 22
0
        // * function;
        // * extern function;
        // * static function;
        // * obj;
        // * obj = init;
        // * static obj;
        // * static obj = init;
        // * extern obj;
        // * extern obj = init;
        public void CGenDecln(Env env, CGenState state) {

            if (env.IsGlobal()) {

                if (this.initr.IsSome) {
                    Initr initr = this.initr.Value;
                    switch (scs) {
                        case SCS.AUTO:
                            state.GLOBL(name);
                            break;

                        case SCS.EXTERN:
                            throw new InvalidProgramException();

                        case SCS.STATIC:
                            break;

                        case SCS.TYPEDEF:
                            // Ignore.
                            return;

                        default:
                            throw new InvalidProgramException();
                    }

                    state.DATA();

                    state.ALIGN(ExprType.ALIGN_LONG);

                    state.CGenLabel(name);

                    Int32 last = 0;
                    initr.Iterate(type, (Int32 offset, Expr expr) => {
                        if (offset > last) {
                            state.ZERO(offset - last);
                        }

                        if (!expr.IsConstExpr) {
                            throw new InvalidOperationException("Cannot initialize with non-const expression.");
                        }

                        switch (expr.type.kind) {
                            // TODO: without const char/short, how do I initialize?
                            case ExprType.Kind.CHAR:
                            case ExprType.Kind.UCHAR:
                            case ExprType.Kind.SHORT:
                            case ExprType.Kind.USHORT:
                                throw new NotImplementedException();
                            case ExprType.Kind.LONG:
                                state.LONG(((ConstLong)expr).value);
                                break;

                            case ExprType.Kind.ULONG:
                                state.LONG((Int32)((ConstULong)expr).value);
                                break;

                            case ExprType.Kind.POINTER:
                                state.LONG((Int32)((ConstPtr)expr).value);
                                break;

                            case ExprType.Kind.FLOAT:
                                byte[] float_bytes = BitConverter.GetBytes(((ConstFloat)expr).value);
                                Int32 intval = BitConverter.ToInt32(float_bytes, 0);
                                state.LONG(intval);
                                break;

                            case ExprType.Kind.DOUBLE:
                                byte[] double_bytes = BitConverter.GetBytes(((ConstDouble)expr).value);
                                Int32 first_int = BitConverter.ToInt32(double_bytes, 0);
                                Int32 second_int = BitConverter.ToInt32(double_bytes, 4);
                                state.LONG(first_int);
                                state.LONG(second_int);
                                break;

                            default:
                                throw new InvalidProgramException();
                        }

                        last = offset + expr.type.SizeOf;
                    });

                } else {

                    // Global without initialization.

                    switch (scs) {
                        case SCS.AUTO:
                            // .comm name,size,align
                            break;

                        case SCS.EXTERN:
                            break;

                        case SCS.STATIC:
                            // .local name
                            // .comm name,size,align
                            state.LOCAL(name);
                            break;

                        case SCS.TYPEDEF:
                            // Ignore.
                            return;

                        default:
                            throw new InvalidProgramException();
                    }

                    if (type.kind != ExprType.Kind.FUNCTION) {
                        state.COMM(name, type.SizeOf, ExprType.ALIGN_LONG);
                    }

                    
                }

                state.NEWLINE();

            } else {
                // stack object

                state.CGenExpandStackTo(env.StackSize, ToString());

                Int32 stack_size = env.StackSize;

                // pos should be equal to stack_size, but whatever...
                Int32 pos = env.Find(name).Value.offset;
                if (this.initr.IsNone) {
                    return;
                }

                Initr initr = this.initr.Value;
                initr.Iterate(type, (Int32 offset, Expr expr) => {
                    Reg ret = expr.CGenValue(env, state);
                    switch (expr.type.kind) {
                        case ExprType.Kind.CHAR:
                        case ExprType.Kind.UCHAR:
                            state.MOVB(Reg.EAX, pos + offset, Reg.EBP);
                            break;

                        case ExprType.Kind.SHORT:
                        case ExprType.Kind.USHORT:
                            state.MOVW(Reg.EAX, pos + offset, Reg.EBP);
                            break;

                        case ExprType.Kind.DOUBLE:
                            state.FSTPL(pos + offset, Reg.EBP);
                            break;

                        case ExprType.Kind.FLOAT:
                            state.FSTPS(pos + offset, Reg.EBP);
                            break;

                        case ExprType.Kind.LONG:
                        case ExprType.Kind.ULONG:
                        case ExprType.Kind.POINTER:
                            state.MOVL(Reg.EAX, pos + offset, Reg.EBP);
                            break;

                        case ExprType.Kind.STRUCT_OR_UNION:
                            state.MOVL(Reg.EAX, Reg.ESI);
                            state.LEA(pos + offset, Reg.EBP, Reg.EDI);
                            state.MOVL(expr.type.SizeOf, Reg.ECX);
                            state.CGenMemCpy();
                            break;

                        case ExprType.Kind.ARRAY:
                        case ExprType.Kind.FUNCTION:
                            throw new InvalidProgramException($"How could a {expr.type.kind} be in a init list?");

                        default:
                            throw new InvalidProgramException();
                    }

                    state.CGenForceStackSizeTo(stack_size);

                });

            } // stack object
        }