diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index e890951..bb68102 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -155,6 +155,8 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 	public int next_coroutine_state = 1;
 	int next_block_id = 0;
 	Map<Block,int> block_map = new HashMap<Block,int> ();
+	int next_scope_id = 0;
+	Map<Block,int> scope_map = new HashMap<Block,int> ();
 
 	public DataType void_type = new VoidType ();
 	public DataType bool_type;
@@ -1671,6 +1673,15 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 		return result;
 	}
 
+	public int get_scope_id (Block b) {
+		int result = scope_map[b];
+		if (result == 0) {
+			result = ++next_scope_id;
+			scope_map[b] = result;
+		}
+		return result;
+	}
+
 	public override void visit_block (Block b) {
 		var old_symbol = current_symbol;
 		current_symbol = b;
@@ -1864,6 +1875,29 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			var dec = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, new CCodeUnaryExpression (CCodeUnaryOperator.PREFIX_DECREMENT, new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), "_ref_count_")), new CCodeConstant ("0"));
 			unref_fun.block.add_statement (new CCodeIfStatement (dec, free_block));
 			source_type_member_definition.append (unref_fun);
+		} else if (current_method != null && current_method.coroutine) {
+			int scope_id = get_scope_id (b);
+			var struct_name = "Scope%dData".printf (scope_id);
+			var scope = new CCodeStruct ("_" + struct_name);
+			foreach (var local in local_vars) {
+				if (!local.captured) {
+					scope.add_field (local.variable_type.get_cname (), get_variable_cname (local.name) + local.variable_type.get_cdeclarator_suffix ());
+					// missing delegate, arrays, init, etc.
+				}
+			}
+
+			var typedef = new CCodeTypeDefinition ("struct _" + struct_name, new CCodeVariableDeclarator (struct_name));
+			source_declarations.add_type_declaration (typedef);
+			source_declarations.add_type_definition (scope);
+
+			var scope_name = "_scope%d_".printf (scope_id);
+			// scope in the closure_struct
+			closure_struct.add_field (struct_name, scope_name);
+			// scope in the method for faster access, like with closures
+			var scope_field = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), scope_name));
+			var scope_decl = new CCodeDeclaration (struct_name + "*");
+			scope_decl.add_declarator (new CCodeVariableDeclarator (scope_name, scope_field));
+			cblock.add_statement (scope_decl);
 		}
 
 		foreach (CodeNode stmt in b.get_statements ()) {
