Commit da0cc732ea

Robin Voetter <robin@voetter.nl>
2021-05-15 03:27:59
SPIR-V: Function parameter generation
1 parent 074cb9f
Changed files (2)
src
codegen
link
src/codegen/spirv.zig
@@ -57,6 +57,7 @@ pub const DeclGen = struct {
     module: *Module,
     spv: *SPIRVModule,
 
+    args: std.ArrayList(u32),
     types: TypeMap,
 
     decl: *Decl,
@@ -213,7 +214,17 @@ pub const DeclGen = struct {
                 prototype_id,
             });
 
-            // TODO: Parameters
+            const params = tv.ty.fnParamLen();
+            var i: usize = 0;
+
+            try self.args.ensureCapacity(params);
+            while (i < params) : (i += 1) {
+                const param_type_id = self.types.get(tv.ty.fnParamType(i)).?;
+                const arg_result_id = self.spv.allocResultId();
+                try writeInstruction(&self.spv.fn_decls, .OpFunctionParameter, &[_]u32{ param_type_id, arg_result_id });
+                self.args.appendAssumeCapacity(arg_result_id);
+            }
+
             // TODO: Body
 
             try writeInstruction(&self.spv.fn_decls, .OpFunctionEnd, &[_]u32{});
src/link/SpirV.zig
@@ -140,23 +140,26 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void {
     // Now, actually generate the code for all declarations.
     {
         // We are just going to re-use this same DeclGen for every Decl, and we are just going to
-        // change the decl. Otherwise, we would have to keep a separate `types`, and re-construct this
+        // change the decl. Otherwise, we would have to keep a separate `args` and `types`, and re-construct this
         // structure every time.
         var decl_gen = codegen.DeclGen{
             .module = module,
             .spv = &spv,
+            .args = std.ArrayList(u32).init(self.base.allocator),
             .types = codegen.TypeMap.init(self.base.allocator),
             .decl = undefined,
             .error_msg = undefined,
         };
 
         defer decl_gen.types.deinit();
+        defer decl_gen.args.deinit();
 
         for (module.decl_table.items()) |entry| {
             const decl = entry.value;
             if (decl.typed_value != .most_recent)
                 continue;
 
+            decl_gen.args.items.len = 0;
             decl_gen.decl = decl;
             decl_gen.error_msg = null;