Skip to content

Commit

Permalink
Fixes (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed Mar 25, 2023
1 parent d8a5b51 commit 0c33390
Show file tree
Hide file tree
Showing 12 changed files with 1,026 additions and 1,034 deletions.
2 changes: 1 addition & 1 deletion Demos/Run-Demo.SVGAWorld.x86.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd %~dp0
cd ..\bin
start Mosa.Tool.Launcher -autostart -oMax -output-asm -output-debug -output-hash -vmdk -vmware-svga -include Include Mosa.Demo.SVGAWorld.x86.dll
start Mosa.Tool.Launcher -autostart -oMax -output-asm -output-debug -output-hash -vmdk -vmware-svga -inline-off -include Include Mosa.Demo.SVGAWorld.x86.dll
23 changes: 17 additions & 6 deletions Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,11 @@ public List<BasicBlock> AddMissingBlocksIfRequired(List<BasicBlock> blocks)

protected BaseInstruction GetLoadInstruction(MosaType type)
{
type = MosaTypeLayout.GetUnderlyingType(type);

if (type == null)
return IRInstruction.LoadCompound;

if (type.IsReferenceType)
return IRInstruction.LoadObject;
else if (type.IsPointer)
Expand Down Expand Up @@ -964,9 +969,14 @@ protected BaseInstruction GetLoadInstruction(MosaType type)

public BaseInstruction GetMoveInstruction(MosaType type)
{
type = MosaTypeLayout.GetUnderlyingType(type);

if (type == null)
return IRInstruction.MoveCompound;

if (type.IsReferenceType)
return IRInstruction.MoveObject;
if (type.IsPointer)
else if (type.IsPointer)
return Select(IRInstruction.Move32, IRInstruction.Move64);
else if (type.IsI1)
return Select(IRInstruction.SignExtend8x32, IRInstruction.SignExtend8x64);
Expand Down Expand Up @@ -1067,20 +1077,21 @@ public static BaseIRInstruction GetSetReturnInstruction(MosaType type, bool is32
if (type == null)
return null;

type = MosaTypeLayout.GetUnderlyingType(type);

if (type == null)
return IRInstruction.SetReturnCompound;

if (type.IsReferenceType)
return IRInstruction.SetReturnObject;
else if (type.IsR4)
return IRInstruction.SetReturnR4;
else if (type.IsR8)
return IRInstruction.SetReturnR8;
else if (!is32bitPlatform)
return IRInstruction.SetReturn64;
else if (type.IsUI8 || (type.IsEnum && type.ElementType.IsUI8))
return IRInstruction.SetReturn64;
else if (!MosaTypeLayout.IsUnderlyingPrimitive(type))
return IRInstruction.SetReturnCompound;

return IRInstruction.SetReturn32;
return is32bitPlatform ? IRInstruction.SetReturn32 : IRInstruction.SetReturn64;
}

public BaseIRInstruction GetStoreInstruction(MosaType type)
Expand Down
10 changes: 7 additions & 3 deletions Source/Mosa.Compiler.Framework/MethodCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -816,14 +816,18 @@ public void SetLocalVariables(IList<MosaLocal> locals)
var index = 0;
foreach (var local in locals)
{
if (MosaTypeLayout.IsUnderlyingPrimitive(local.Type) && !local.IsPinned)
var localtype = local.Type;
var underlyingType = localtype;
//var underlyingType = MosaTypeLayout.GetUnderlyingType(local.Type);

if (MosaTypeLayout.IsUnderlyingPrimitive(underlyingType) && !local.IsPinned)
{
var stacktype = Compiler.GetStackType(local.Type);
var stacktype = Compiler.GetStackType(underlyingType);
LocalVariables[index++] = CreateVirtualRegister(stacktype);
}
else
{
LocalVariables[index++] = AddStackLocal(local.Type, local.IsPinned);
LocalVariables[index++] = AddStackLocal(underlyingType, local.IsPinned);
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion Source/Mosa.Compiler.Framework/Stages/CILDecodingStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,28 @@ private void InitializePromotedLocalVariablesToVirtualRegisters()
if (!variable.IsVirtualRegister)
continue;

if (variable.IsReferenceType)
if (variable.IsValueType)
{
var type = MosaTypeLayout.GetUnderlyingType(variable.Type);

if (type.IsR4)
{
prologue.AppendInstruction(IRInstruction.MoveR4, variable, CreateConstantR4(0.0f));
}
else if (type.IsR8)
{
prologue.AppendInstruction(IRInstruction.MoveR8, variable, CreateConstantR8(0.0d));
}
else if (type.IsUI8 || (Is64BitPlatform && (type.IsU || type.IsI)))
{
prologue.AppendInstruction(IRInstruction.Move64, variable, Constant64_0);
}
else
{
prologue.AppendInstruction(IRInstruction.Move32, variable, Constant32_0);
}
}
else if (variable.IsReferenceType)
{
prologue.AppendInstruction(IRInstruction.MoveObject, variable, Operand.GetNull(variable.Type));
}
Expand Down
137 changes: 26 additions & 111 deletions Source/Mosa.Compiler.Framework/Stages/CILDecodingStageV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;

using Mosa.Compiler.Common;
Expand Down Expand Up @@ -1902,6 +1901,9 @@ private bool Castclass(Context context, Stack<StackEntry> stack)
var result = AllocateVirtualRegister(TypeSystem.BuiltIn.Object);
PushStack(stack, new StackEntry(StackType.Object, result));

// FUTURE: Check for valid cast
//var methodTable = GetMethodTablePointer(type);

if (entry.StackType == StackType.Object)
{
// TODO: Do this right
Expand Down Expand Up @@ -3489,7 +3491,6 @@ private bool Ldelem(Context context, Stack<StackEntry> stack, MosaInstruction in
var result = Allocate(StackType.ValueType, type);

context.AppendInstruction(IRInstruction.LoadCompound, result, array, totalElementOffset);
context.MosaType = type.ElementType;

PushStack(stack, new StackEntry(StackType.ValueType, result, type));
}
Expand Down Expand Up @@ -3611,18 +3612,16 @@ private bool Ldfld(Context context, Stack<StackEntry> stack, MosaInstruction ins
else
{
context.AppendInstruction(IRInstruction.LoadCompound, result, operand, fixedOffset);
context.MosaType = fieldType;
}
}
else if (isFieldPrimitive)
{
var loadInstruction = GetLoadInstruction(fieldUnderlyingType);
var loadInstruction = GetLoadInstruction(fieldUnderlyingType); // TODO: replace with element
context.AppendInstruction(loadInstruction, result, operand, fixedOffset);
}
else
{
context.AppendInstruction(IRInstruction.LoadCompound, result, operand, fixedOffset);
context.MosaType = fieldType;
}
return true;
}
Expand Down Expand Up @@ -3789,7 +3788,6 @@ private bool Ldloc(Context context, Stack<StackEntry> stack, int index)
{
var result2 = AddStackLocal(local.Type);
context.AppendInstruction(IRInstruction.MoveCompound, result2, local);
context.MosaType = local.Type;

PushStack(stack, new StackEntry(stacktype, result2, local.Type));

Expand Down Expand Up @@ -3858,9 +3856,9 @@ private bool Ldobj(Context context, Stack<StackEntry> stack, MosaInstruction ins
}
else
{
var result = MethodCompiler.AddStackLocal(type);
var result = Allocate(StackType.ValueType, type);

context.AppendInstruction(IRInstruction.LoadCompound, result, address, ConstantZero);
context.MosaType = type;
PushStack(stack, new StackEntry(StackType.ValueType, result, type));
return true;
}
Expand Down Expand Up @@ -3896,14 +3894,12 @@ private bool Ldsfld(Context context, Stack<StackEntry> stack, MosaInstruction in
var loadInstruction = GetLoadInstruction(elementType);

context.AppendInstruction(loadInstruction, result, fieldOperand, ConstantZero);
context.MosaType = type;
}
}
else
{
var result = AllocateVirtualRegister(type);
context.AppendInstruction(IRInstruction.LoadCompound, result, fieldOperand, ConstantZero);
context.MosaType = type;
PushStack(stack, new StackEntry(StackType.ValueType, result, type));
}

Expand Down Expand Up @@ -4258,9 +4254,9 @@ private bool Newobj(Context context, Stack<StackEntry> stack, MosaInstruction in
operands.Insert(0, newThis);

context.AppendInstruction(IRInstruction.CallStatic, null, symbol, operands);
//context.InvokeMethod = method; // optional??

var loadInstruction = GetLoadInstruction(newThisLocal.Type);
var loadInstruction = GetLoadInstruction(newThisLocal.Type); // TODO: replace with element

context.AppendInstruction(loadInstruction, result, StackFrame, newThisLocal);

PushStack(stack, new StackEntry(stackType, result));
Expand Down Expand Up @@ -4519,7 +4515,7 @@ private bool Ret(Context context, Stack<StackEntry> stack)

case StackType.ValueType:
context.AppendInstruction(IRInstruction.SetReturnCompound, null, entry.Operand);
context.MosaType = entry.Type;
//context.MosaType = entry.Type;
break;

case StackType.ManagedPointer when Is32BitPlatform:
Expand Down Expand Up @@ -4860,79 +4856,6 @@ private bool Stloc(Context context, Stack<StackEntry> stack, int index)
context.AppendInstruction(storeInstruction, null, StackFrame, local, source);
return true;
}

//if (local.IsVirtualRegister)
//{
// switch (stacktype)
// {
// case StackType.Int32:
// context.AppendInstruction(IRInstruction.Move32, local, source);
// return true;

// case StackType.Int64:
// context.AppendInstruction(IRInstruction.Move64, local, source);
// return true;

// case StackType.Object:
// context.AppendInstruction(IRInstruction.MoveObject, local, source);
// return true;

// case StackType.R4:
// context.AppendInstruction(IRInstruction.MoveR4, local, source);
// return true;

// case StackType.R8:
// context.AppendInstruction(IRInstruction.MoveR8, local, source);
// return true;

// case StackType.ManagedPointer when Is32BitPlatform:
// context.AppendInstruction(IRInstruction.Move32, local, source);
// return true;

// case StackType.ManagedPointer when Is64BitPlatform:
// context.AppendInstruction(IRInstruction.Move64, local, source);
// return true;

// default: return false;
// }
//}
//else
//{
// switch (stacktype)
// {
// case StackType.Int32:
// context.AppendInstruction(IRInstruction.Store32, null, StackFrame, local, source);
// return true;

// case StackType.Int64:
// context.AppendInstruction(IRInstruction.Store64, null, StackFrame, local, source);
// return true;

// case StackType.Object:
// context.AppendInstruction(IRInstruction.StoreObject, null, StackFrame, local, source);
// return true;

// case StackType.R4:
// context.AppendInstruction(IRInstruction.StoreR4, null, StackFrame, local, source);
// return true;

// case StackType.R8:
// context.AppendInstruction(IRInstruction.StoreR8, null, StackFrame, local, source);
// return true;

// case StackType.ManagedPointer when Is32BitPlatform:
// context.AppendInstruction(IRInstruction.Store32, null, StackFrame, local, source);
// return true;

// case StackType.ManagedPointer when Is64BitPlatform:
// context.AppendInstruction(IRInstruction.Store64, null, StackFrame, local, source);
// return true;

// default: return false;
// }
//}

//return false;
}

private bool Stobj(Context context, Stack<StackEntry> stack, MosaInstruction instruction)
Expand Down Expand Up @@ -5427,7 +5350,7 @@ private bool Unbox(Context context, Stack<StackEntry> stack, MosaInstruction ins
var entry = PopStack(stack);
var type = (MosaType)instruction.Operand;

var result = Allocate(StackType.ManagedPointer); // AllocateVirtualRegisterManagedPointer();
var result = Allocate(StackType.ManagedPointer, type);
PushStack(stack, new StackEntry(StackType.ManagedPointer, result));

context.AppendInstruction(IRInstruction.Unbox, result, entry.Operand);
Expand All @@ -5437,20 +5360,20 @@ private bool Unbox(Context context, Stack<StackEntry> stack, MosaInstruction ins

private bool UnboxAny(Context context, Stack<StackEntry> stack, MosaInstruction instruction)
{
var entry = PopStack(stack);

var type = (MosaType)instruction.Operand;

// FUTURE: Check for valid cast
var methodTable = GetMethodTablePointer(type);


if (type.IsReferenceType)
{
// FUTURE: treat as castclass
PushStack(stack, entry);
return true;
// treat as castclass, per spec
return Castclass(context, stack);
}

Debug.Assert(type.IsValueType);

var entry = PopStack(stack);

//equivalent to unbox followed by ldobj

var underlyingType = GetUnderlyingType(type);
var isPrimitive = IsPrimitive(underlyingType);

Expand All @@ -5462,28 +5385,20 @@ private bool UnboxAny(Context context, Stack<StackEntry> stack, MosaInstruction
var stackType = GetStackType(elementType);
var result = Allocate(stackType);

context.AppendInstruction(loadInstruction, result, entry.Operand, ConstantZero); //CreateConstant32(8)

PushStack(stack, new StackEntry(stackType, result));

context.AppendInstruction(loadInstruction, result, entry.Operand, ConstantZero);

return true;
}
else
{
var result = Allocate(StackType.ValueType, type);
var address = Allocate(StackType.ManagedPointer);

//var tmpLocal = AddStackLocal(type);
//var typeSize = Alignment.AlignUp(TypeLayout.GetTypeSize(type), TypeLayout.NativePointerAlignment);

//var adr = AllocateVirtualRegisterManagedPointer();
//context.AppendInstruction(IRInstruction.AddressOf, adr, tmpLocal);
//context.AppendInstruction(IRInstruction.UnboxAny, tmpLocal, entry.Operand, adr, CreateConstant32(typeSize));
//context.AppendInstruction(IRInstruction.LoadCompound, result, tmpLocal, ConstantZero);

//context.AppendInstruction(IRInstruction.LoadCompound, result, tmpLocal, ConstantZero);

var adr = AllocateVirtualRegisterManagedPointer();
context.AppendInstruction(IRInstruction.Unbox, adr, entry.Operand);
context.AppendInstruction(IRInstruction.LoadCompound, result, adr, ConstantZero);
context.AppendInstruction(MoveInstruction, address, entry.Operand);
// FUTURE: Add type check
context.AppendInstruction(IRInstruction.LoadCompound, result, address, ConstantZero);

PushStack(stack, new StackEntry(StackType.ValueType, result, type));
return true;
Expand Down

0 comments on commit 0c33390

Please sign in to comment.