Skip to content

Commit

Permalink
[BigStep] Implement Ldsfld for Int32 fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdageek authored and lewurm committed Jul 30, 2018
1 parent 122a0d6 commit c6fb485
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mcs/class/Mono.Compiler/Mono.Compiler.BigStep/BigStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public CompilationResult CompileMethod (MethodInfo methodInfo, out NativeCodeHan
result = NativeCodeHandle.Invalid;
try
{
BitCodeEmitter processor = new BitCodeEmitter(methodInfo){
BitCodeEmitter processor = new BitCodeEmitter (RuntimeInfo, methodInfo) {
// PrintDebugInfo = true,
VerifyGeneratedCode = true
};
CILSymbolicExecutor exec = new CILSymbolicExecutor(processor, RuntimeInfo, methodInfo);
CILSymbolicExecutor exec = new CILSymbolicExecutor (processor, RuntimeInfo, methodInfo);
exec.Execute();
result = processor.Yield();
return CompilationResult.Ok;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Mono.Compiler.BigStep {
public class CILSymbolicExecutor : INameGenerator {
private IOperationProcessor processor;
private IRuntimeInformation runtime;
private MethodInfo methodInfo;
private MethodBody body;

private Stack<TempOperand> stack;
Expand All @@ -40,6 +41,7 @@ public CILSymbolicExecutor(IOperationProcessor processor, IRuntimeInformation ru
{
this.processor = processor;
this.runtime = runtime;
this.methodInfo = methodInfo;
this.body = methodInfo.Body;

this.stack = new Stack<TempOperand>();
Expand Down Expand Up @@ -309,6 +311,12 @@ private void Pass2 ()
operands.Add (locals[opParam]);
break;
// TODO: ExtendedOpcode.Stloc
case Opcode.Ldsfld:
int token = iter.DecodeParamI ();
FieldInfo fieldInfo = runtime.GetFieldInfoForToken (methodInfo, token);
operands.Add (new Int32ConstOperand (token));
output = new TempOperand (this, runtime.Int32Type); /* FIXME: look up the field info! */
break;
}

// 2) Determine the result type for values to push into stack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ public class BitCodeEmitter : IOperationProcessor {
public bool PrintDebugInfo { get; set; }
public bool VerifyGeneratedCode { get; set; }

public BitCodeEmitter (MethodInfo method)
internal MethodInfo MethodInfo { get; }
internal IRuntimeInformation RuntimeInfo { get; }

public BitCodeEmitter (IRuntimeInformation runtimeInfo, MethodInfo method)
{
RuntimeInfo = runtimeInfo;
MethodInfo = method;
int seq = Interlocked.Increment (ref s_moduleSeq);
string modName = "llvmmodule_" + seq;
module = LLVM.ModuleCreateWithName(modName);
Expand Down Expand Up @@ -200,6 +205,22 @@ public void Process (OperationInfo opInfo)
return new NamedTempValue (tmp, tempName);
});
break;
case Opcode.Ldsfld:
// const => tmp
int token = (operands[0] as Int32ConstOperand).Value;
InvokeOperation (op, exop, operands,
vm => {
// TODO: It would be nice if operand[0] just carried the fieldInfo here
FieldInfo fieldInfo = RuntimeInfo.GetFieldInfoForToken (MethodInfo, token);
LLVMValueRef fieldAddress = GetConstValue (RuntimeInfo.ComputeFieldAddress (fieldInfo));
LLVMTypeRef fieldType = LLVM.Int32Type (); /* FIXME: get from field info */
LLVMValueRef address = LLVM.ConstIntToPtr (fieldAddress, LLVM.PointerType (fieldType, 0));
LLVMValueRef tmp = LLVM.BuildLoad (builder, address, tempName);
return new NamedTempValue (tmp, tempName);
});
break;
case Opcode.Add:
case Opcode.AddOvf: // TODO - Handle overflow
case Opcode.AddOvfUn: // TODO - Handle overflow, unsigned
Expand Down Expand Up @@ -456,6 +477,11 @@ private LLVMValueRef GetConstValue (IOperand operand)
throw new Exception ("Unexpected. The const operand is tno recognized.");
}

private LLVMValueRef GetConstValue (IntPtr constant)
{
return LLVM.ConstInt (TranslateType (RuntimeInformation.NativeIntType), (ulong)constant, true);
}

private static LLVMTypeRef TranslateType (ClrType ctyp)
{
if (ctyp == RuntimeInformation.BoolType) {
Expand All @@ -480,6 +506,7 @@ private static LLVMTypeRef TranslateType (ClrType ctyp)
return LLVM.FloatType ();
}
if (ctyp == RuntimeInformation.NativeIntType || ctyp == RuntimeInformation.NativeUnsignedIntType) {
/* FIXME: target platform dependent */
return LLVM.Int64Type ();
}
if (ctyp == RuntimeInformation.StringType) {
Expand Down

0 comments on commit c6fb485

Please sign in to comment.