Example #1
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
        }
Example #2
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
        }