Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed May 1, 2024
1 parent 6cf9bb3 commit ec9ac77
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 136 deletions.
4 changes: 2 additions & 2 deletions chibild/chibild.core/Generating/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ internal sealed partial class CodeGenerator
private readonly ModuleDefinition targetModule;
private readonly bool produceDebuggingInformation;

private readonly Queue<Action<ModuleDefinition>> delayLookingUpEntries1 = new();
private readonly Queue<Action<ModuleDefinition>> delayLookingUpEntries2 = new();
private readonly Queue<Action> delayLookingUpEntries1 = new();
private readonly Queue<Action> delayLookingUpEntries2 = new();
private readonly Queue<Action<Dictionary<string, Document>>> delayDebuggingInsertionEntries = new();

private bool caughtError;
Expand Down
90 changes: 45 additions & 45 deletions chibild/chibild.core/Generating/CodeGenerator_Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using MethodCallingConvention = Mono.Cecil.MethodCallingConvention;

namespace chibild.Generating;

Expand Down Expand Up @@ -63,9 +62,9 @@ private void ConsumeGlobalVariable(
context,
globalVariable.Type,
false,
(targetModule, type) => CecilUtilities.SetFieldType(
type => CecilUtilities.SetFieldType(
field,
targetModule.SafeImport(type)));
context.SafeImport(type)));

switch (globalVariable.Scope.Scope)
{
Expand Down Expand Up @@ -109,7 +108,7 @@ private void ConsumeGlobalConstant(
context,
globalConstant.Type,
false,
(_, fieldType) =>
fieldType =>
{
var isConstTypeIdentity = new IdentityNode(
globalConstant.Type.Token.AsText(
Expand All @@ -119,21 +118,21 @@ private void ConsumeGlobalConstant(
context,
isConstTypeIdentity,
false,
(targetModule, isConstType) =>
isConstType =>
{
var modifiedType = targetModule.SafeImport(fieldType).
var modifiedType = context.SafeImport(fieldType).
MakeOptionalModifierType(
targetModule.SafeImport(isConstType));
context.SafeImport(isConstType));
CecilUtilities.SetFieldType(
field,
modifiedType);
},
targetModule =>
() =>
{
CecilUtilities.SetFieldType(
field,
targetModule.SafeImport(fieldType));
context.SafeImport(fieldType));
this.OutputWarning(
isConstTypeIdentity.Token,
Expand Down Expand Up @@ -203,7 +202,7 @@ private void ConsumeLocalVariables(
context,
localVariable.Type,
false,
(targetModule, type) =>
type =>
{
// Mark pinned.
if (type.IsByReference)
Expand All @@ -212,7 +211,7 @@ private void ConsumeLocalVariables(
var elementType = resolved.GetElementType();
if (elementType.IsValueType)
{
variable.VariableType = targetModule.SafeImport(
variable.VariableType = context.SafeImport(
type).
MakePinnedType();
return;
Expand Down Expand Up @@ -323,8 +322,8 @@ private void ConsumeInstructions(
context,
field,
false,
(targetModule, f) => cilInstruction.Operand =
targetModule.SafeImport(f));
f => cilInstruction.Operand =
context.SafeImport(f));
break;

case TypeInstructionNode(_, var type):
Expand All @@ -335,8 +334,8 @@ private void ConsumeInstructions(
context,
type,
false,
(targetModule, t) => cilInstruction.Operand =
targetModule.SafeImport(t));
t => cilInstruction.Operand =
context.SafeImport(t));
break;

case MetadataTokenInstructionNode(_, var identity, var signature):
Expand All @@ -348,8 +347,8 @@ private void ConsumeInstructions(
identity,
signature,
false,
(targetModule, m) => cilInstruction.Operand =
targetModule.SafeImport(m));
m => cilInstruction.Operand =
context.SafeImport(m));
break;

case NumericValueInstructionNode(_, var (value, _)):
Expand Down Expand Up @@ -451,10 +450,10 @@ private void ConsumeInstructions(
function,
signature,
false,
(targetModule, m, ptrs) =>
(m, ptrs) =>
{
// Will make specialized method reference with variadic parameters.
if (m.CallingConvention == MethodCallingConvention.VarArg)
// Will construct specialized method reference with variadic parameters.
if (m.CallingConvention == Mono.Cecil.MethodCallingConvention.VarArg)
{
if (signature == null)
{
Expand All @@ -465,24 +464,28 @@ private void ConsumeInstructions(
}
var emr = new MethodReference(
m.Name, m.ReturnType, m.DeclaringType);
emr.CallingConvention = MethodCallingConvention.VarArg;
m.Name,
context.SafeImport(m.ReturnType),
context.SafeImport(m.DeclaringType));
emr.CallingConvention = Mono.Cecil.MethodCallingConvention.VarArg;
foreach (var parameter in m.Parameters)
{
var pt = context.SafeImport(parameter.ParameterType);
emr.Parameters.Add(new(
parameter.Name,
parameter.Attributes,
parameter.ParameterType));
pt));
}
// Append sentinel parameters each lack parameter types.
var first = true;
foreach (var parameterType in
ptrs.Skip(m.Parameters.Count))
{
var pt = targetModule.SafeImport(parameterType);
var pt = context.SafeImport(parameterType);
if (first)
{
// Mark only first sentinel.
emr.Parameters.Add(new(pt.MakeSentinelType()));
first = false;
}
Expand All @@ -491,11 +494,10 @@ private void ConsumeInstructions(
emr.Parameters.Add(new(pt));
}
}
m = emr;
}
cilInstruction.Operand =
targetModule.SafeImport(m);
context.SafeImport(m);
});
break;

Expand All @@ -521,8 +523,8 @@ private void ConsumeInstructions(
context,
signature.ReturnType,
false,
(targetModule, t) => callSite.ReturnType =
targetModule.SafeImport(t));
rt => callSite.ReturnType =
context.SafeImport(rt));

foreach (var (parameterType, parameterName, _) in signature.Parameters)
{
Expand All @@ -535,8 +537,8 @@ private void ConsumeInstructions(
context,
parameterType,
false,
(targetModule, t) => parameter.ParameterType =
targetModule.SafeImport(t));
pt => parameter.ParameterType =
context.SafeImport(pt));
}
break;

Expand Down Expand Up @@ -648,17 +650,17 @@ private void ConsumeFunction(
context,
function.Signature.ReturnType,
false,
(targetModule, type) =>
rt =>
{
method.ReturnType =
targetModule.SafeImport(type);
context.SafeImport(rt);
// Special case: Force 1 byte footprint on boolean type.
if (type.FullName == "System.Boolean")
if (rt.FullName == "System.Boolean")
{
method.MethodReturnType.MarshalInfo = new(NativeType.U1);
}
else if (type.FullName == "System.Char")
else if (rt.FullName == "System.Char")
{
method.MethodReturnType.MarshalInfo = new(NativeType.U2);
}
Expand All @@ -672,17 +674,17 @@ private void ConsumeFunction(
context,
function.Signature.Parameters[index].ParameterType,
false,
(targetModule, type) =>
pt =>
{
capturedParameter.ParameterType =
targetModule.SafeImport(type);
context.SafeImport(pt);
// Special case: Force 1 byte footprint on boolean type.
if (type.FullName == "System.Boolean")
if (pt.FullName == "System.Boolean")
{
capturedParameter.MarshalInfo = new(NativeType.U1);
}
else if (type.FullName == "System.Char")
else if (pt.FullName == "System.Char")
{
capturedParameter.MarshalInfo = new(NativeType.U2);
}
Expand Down Expand Up @@ -735,9 +737,7 @@ private void ConsumeInitializer(
this.CreatePlaceholderType(),
Utilities.Empty<ParameterDefinition>(),
MethodAttributes.Private | MethodAttributes.Static);

this.DelayLookingUpAction1(
targetModule => method.ReturnType = targetModule.TypeSystem.Void);
method.ReturnType = context.FallbackModule.TypeSystem.Void;

var vdis = new Dictionary<string, VariableDebugInformation>();

Expand Down Expand Up @@ -827,9 +827,9 @@ private void ConsumeEnumeration(
context,
enumeration.UnderlyingType,
false,
(targetModule, utr) =>
utr =>
{
var ut = targetModule.SafeImport(utr);
var ut = context.SafeImport(utr);
var enumerationValueField = new FieldDefinition(
"value__",
Expand Down Expand Up @@ -941,9 +941,9 @@ private void ConsumeStructure(
context,
structureField.Type,
false,
(_, ftr) => CecilUtilities.SetFieldType(
ftr => CecilUtilities.SetFieldType(
field,
targetModule.SafeImport(ftr)));
context.SafeImport(ftr)));

if (typeAttributes.HasFlag(TypeAttributes.ExplicitLayout))
{
Expand Down
Loading

0 comments on commit ec9ac77

Please sign in to comment.