// Dual licensed under the terms of the MIT X11 or GNU GPL
//
// Copyright 2011 Novell, Inc.
-// Copyright 2011 Xamarin Inc.
+// Copyright 2011-2012 Xamarin Inc.
//
using System;
using System.Collections;
#if STATIC
+using IKVM.Reflection;
using IKVM.Reflection.Emit;
#else
+using System.Reflection;
using System.Reflection.Emit;
#endif
}
}
+ public AwaitStatement Statement {
+ get {
+ return stmt;
+ }
+ }
+
protected override void CloneTo (CloneContext clonectx, Expression target)
{
var t = (Await) target;
}
}
- class AwaitStatement : YieldStatement<AsyncInitializer>
+ public class AwaitStatement : YieldStatement<AsyncInitializer>
{
sealed class AwaitableMemberAccess : MemberAccess
{
public void EmitPrologue (EmitContext ec)
{
+ awaiter = ((AsyncTaskStorey) machine_initializer.Storey).AddAwaiter (expr.Type, loc);
+
var fe_awaiter = new FieldExpr (awaiter, loc);
fe_awaiter.InstanceExpression = new CompilerGeneratedThis (ec.CurrentType, loc);
//
// awaiter = expr.GetAwaiter ();
//
- fe_awaiter.EmitAssign (ec, expr, false, false);
+ using (ec.With (BuilderContext.Options.OmitDebugInfo, true)) {
+ fe_awaiter.EmitAssign (ec, expr, false, false);
+ }
Label skip_continuation = ec.DefineLabel ();
EmitPrologue (ec);
DoEmit (ec);
- if (ResultType.Kind != MemberKind.Void) {
- var storey = (AsyncTaskStorey) machine_initializer.Storey;
+ awaiter.IsAvailableForReuse = true;
- if (storey.HoistedReturn != null)
- storey.HoistedReturn.EmitAssign (ec);
- else
- ec.Emit (OpCodes.Pop);
- }
+ if (ResultType.Kind != MemberKind.Void)
+ ec.Emit (OpCodes.Pop);
}
void Error_WrongAwaiterPattern (ResolveContext rc, TypeSpec awaiter)
//
if (type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
result_type = type;
-
- awaiter = ((AsyncTaskStorey) machine_initializer.Storey).AddAwaiter (type, loc);
-
expr = new Invocation (new MemberAccess (expr, "GetAwaiter"), args).Resolve (bc);
return true;
}
}
var awaiter_type = ama.Type;
- awaiter = ((AsyncTaskStorey) machine_initializer.Storey).AddAwaiter (awaiter_type, loc);
-
expr = ama;
//
}
}
- public Block OriginalBlock {
- get {
- return block.Parent;
- }
- }
-
public TypeInferenceContext ReturnTypeInference {
get {
return return_inference;
#endregion
- public static void Create (IMemberContext context, ParametersBlock block, ParametersCompiled parameters, TypeDefinition host, TypeSpec returnType, Location loc)
- {
- for (int i = 0; i < parameters.Count; i++) {
- Parameter p = parameters[i];
- Parameter.Modifier mod = p.ModFlags;
- if ((mod & Parameter.Modifier.RefOutMask) != 0) {
- host.Compiler.Report.Error (1988, p.Location,
- "Async methods cannot have ref or out parameters");
- return;
- }
-
- if (p is ArglistParameter) {
- host.Compiler.Report.Error (4006, p.Location,
- "__arglist is not allowed in parameter list of async methods");
- return;
- }
-
- if (parameters.Types[i].IsPointer) {
- host.Compiler.Report.Error (4005, p.Location,
- "Async methods cannot have unsafe parameters");
- return;
- }
- }
-
- if (!block.HasAwait) {
- host.Compiler.Report.Warning (1998, 1, loc,
- "Async block lacks `await' operator and will run synchronously");
- }
-
- block.WrapIntoAsyncTask (context, host, returnType);
- }
-
protected override BlockContext CreateBlockContext (ResolveContext rc)
{
var ctx = base.CreateBlockContext (rc);
PropertySpec task;
LocalVariable hoisted_return;
int locals_captured;
- Dictionary<TypeSpec, List<StackField>> stack_fields;
+ Dictionary<TypeSpec, List<Field>> stack_fields;
+ Dictionary<TypeSpec, List<Field>> awaiter_fields;
- public AsyncTaskStorey (IMemberContext context, AsyncInitializer initializer, TypeSpec type)
- : base (initializer.OriginalBlock, initializer.Host, context.CurrentMemberDefinition as MemberBase, context.CurrentTypeParameters, "async", MemberKind.Class)
+ public AsyncTaskStorey (ParametersBlock block, IMemberContext context, AsyncInitializer initializer, TypeSpec type)
+ : base (block, initializer.Host, context.CurrentMemberDefinition as MemberBase, context.CurrentTypeParameters, "async", MemberKind.Struct)
{
return_type = type;
+ awaiter_fields = new Dictionary<TypeSpec, List<Field>> ();
}
#region Properties
}
}
+ protected override TypeAttributes TypeAttr {
+ get {
+ return base.TypeAttr & ~TypeAttributes.SequentialLayout;
+ }
+ }
+
#endregion
public Field AddAwaiter (TypeSpec type, Location loc)
{
- return AddCapturedVariable ("$awaiter" + awaiters++.ToString ("X"), type);
+ if (mutator != null)
+ type = mutator.Mutate (type);
+
+ List<Field> existing_fields = null;
+ if (awaiter_fields.TryGetValue (type, out existing_fields)) {
+ foreach (var f in existing_fields) {
+ if (f.IsAvailableForReuse) {
+ f.IsAvailableForReuse = false;
+ return f;
+ }
+ }
+ }
+
+ var field = AddCompilerGeneratedField ("$awaiter" + awaiters++.ToString ("X"), new TypeExpression (type, Location), true);
+ field.Define ();
+
+ if (existing_fields == null) {
+ existing_fields = new List<Field> ();
+ awaiter_fields.Add (type, existing_fields);
+ }
+
+ existing_fields.Add (field);
+ return field;
}
- public StackField AddCapturedLocalVariable (TypeSpec type)
+ public Field AddCapturedLocalVariable (TypeSpec type)
{
if (mutator != null)
type = mutator.Mutate (type);
- List<StackField> existing_fields = null;
+ List<Field> existing_fields = null;
if (stack_fields == null) {
- stack_fields = new Dictionary<TypeSpec, List<StackField>> ();
+ stack_fields = new Dictionary<TypeSpec, List<Field>> ();
} else if (stack_fields.TryGetValue (type, out existing_fields)) {
foreach (var f in existing_fields) {
- if (f.CanBeReused) {
- f.CanBeReused = false;
+ if (f.IsAvailableForReuse) {
+ f.IsAvailableForReuse = false;
return f;
}
}
}
- const Modifiers mod = Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE;
- var field = new StackField (this, new TypeExpression (type, Location), mod, new MemberName ("$stack" + locals_captured++.ToString ("X"), Location));
- AddField (field);
-
+ var field = AddCompilerGeneratedField ("$stack" + locals_captured++.ToString ("X"), new TypeExpression (type, Location), true);
field.Define ();
if (existing_fields == null) {
- existing_fields = new List<StackField> ();
+ existing_fields = new List<Field> ();
stack_fields.Add (type, existing_fields);
}
InstanceExpression = new CompilerGeneratedThis (ec.CurrentType, Location)
};
- // TODO: CompilerGeneratedThis is enough for structs
- var temp_this = new LocalTemporary (CurrentType);
- temp_this.EmitAssign (ec, new CompilerGeneratedThis (CurrentType, Location), false, false);
-
var args = new Arguments (2);
args.Add (new Argument (awaiter, Argument.AType.Ref));
- args.Add (new Argument (temp_this, Argument.AType.Ref));
+ args.Add (new Argument (new CompilerGeneratedThis (CurrentType, Location), Argument.AType.Ref));
mg.EmitCall (ec, args);
-
- temp_this.Release (ec);
}
public void EmitInitializer (EmitContext ec)
//
// stateMachine.$builder = AsyncTaskMethodBuilder<{task-type}>.Create();
//
- instance.Emit (ec); // .AddressOf (ec, AddressOp.Store);
+ instance.AddressOf (ec, AddressOp.Store);
ec.Emit (OpCodes.Call, builder_factory);
ec.Emit (OpCodes.Stfld, builder_field);
//
// stateMachine.$builder.Start<{storey-type}>(ref stateMachine);
//
- instance.Emit (ec); //.AddressOf (ec, AddressOp.Store);
+ instance.AddressOf (ec, AddressOp.Store);
ec.Emit (OpCodes.Ldflda, builder_field);
if (Task != null)
ec.Emit (OpCodes.Dup);
protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
{
- base_type = Compiler.BuiltinTypes.Object; // ValueType;
+ base_type = Compiler.BuiltinTypes.ValueType;
base_class = null;
var istate_machine = Module.PredefinedTypes.IAsyncStateMachine;
}
}
- class StackField : Field
+ class StackFieldExpr : FieldExpr, IExpressionCleanup
{
- public StackField (TypeDefinition parent, FullNamedExpression type, Modifiers mod, MemberName name)
- : base (parent, type, mod, name, null)
+ public StackFieldExpr (Field field)
+ : base (field, Location.Null)
{
}
- public bool CanBeReused { get; set; }
- }
-
- class StackFieldExpr : FieldExpr
- {
- public StackFieldExpr (Field field)
- : base (field, Location.Null)
+ public override void AddressOf (EmitContext ec, AddressOp mode)
{
+ base.AddressOf (ec, mode);
+
+ if (mode == AddressOp.Load) {
+ var field = (Field) spec.MemberDefinition;
+ field.IsAvailableForReuse = true;
+ }
}
public override void Emit (EmitContext ec)
{
base.Emit (ec);
- var field = (StackField) spec.MemberDefinition;
- field.CanBeReused = true;
+ var field = (Field) spec.MemberDefinition;
+ field.IsAvailableForReuse = true;
+
+ //
+ // Release any captured reference type stack variables
+ // to imitate real stack behavour and help GC stuff early
+ //
+ if (TypeSpec.IsReferenceType (type)) {
+ ec.AddStatementEpilog (this);
+ }
+ }
+
+ void IExpressionCleanup.EmitCleanup (EmitContext ec)
+ {
+ EmitAssign (ec, new NullConstant (type, loc), false, false);
}
}
}