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
}