| ... | ... | @@ -9,31 +9,34 @@ const Type = Value.Type; |
| 9 | 9 | const assert = std.debug.assert; |
| 10 | 10 | const Token = std.zig.Token; |
| 11 | 11 | const ParsedFile = @import("parsed_file.zig").ParsedFile; |
| 12 | const Span = @import("errmsg.zig").Span; |
| 12 | 13 | |
| 13 | 14 | pub const LVal = enum { |
| 14 | 15 | None, |
| 15 | 16 | Ptr, |
| 16 | 17 | }; |
| 17 | 18 | |
| 18 | | pub const Mut = enum { |
| 19 | | Mut, |
| 20 | | Const, |
| 21 | | }; |
| 22 | | |
| 23 | | pub const Volatility = enum { |
| 24 | | NonVolatile, |
| 25 | | Volatile, |
| 26 | | }; |
| 27 | | |
| 28 | 19 | pub const IrVal = union(enum) { |
| 29 | 20 | Unknown, |
| 30 | | Known: *Value, |
| 21 | KnownType: *Type, |
| 22 | KnownValue: *Value, |
| 23 | |
| 24 | const Init = enum { |
| 25 | Unknown, |
| 26 | NoReturn, |
| 27 | Void, |
| 28 | }; |
| 31 | 29 | |
| 32 | 30 | pub fn dump(self: IrVal) void { |
| 33 | 31 | switch (self) { |
| 34 | | IrVal.Unknown => std.debug.warn("Unknown"), |
| 35 | | IrVal.Known => |value| { |
| 36 | | std.debug.warn("Known("); |
| 32 | IrVal.Unknown => typeof.dump(), |
| 33 | IrVal.KnownType => |typeof| { |
| 34 | std.debug.warn("KnownType("); |
| 35 | typeof.dump(); |
| 36 | std.debug.warn(")"); |
| 37 | }, |
| 38 | IrVal.KnownValue => |value| { |
| 39 | std.debug.warn("KnownValue("); |
| 37 | 40 | value.dump(); |
| 38 | 41 | std.debug.warn(")"); |
| 39 | 42 | }, |
| ... | ... | @@ -46,10 +49,18 @@ pub const Instruction = struct { |
| 46 | 49 | scope: *Scope, |
| 47 | 50 | debug_id: usize, |
| 48 | 51 | val: IrVal, |
| 52 | ref_count: usize, |
| 53 | span: Span, |
| 49 | 54 | |
| 50 | 55 | /// true if this instruction was generated by zig and not from user code |
| 51 | 56 | is_generated: bool, |
| 52 | 57 | |
| 58 | /// the instruction that is derived from this one in analysis |
| 59 | child: ?*Instruction, |
| 60 | |
| 61 | /// the instruction that this one derives from in analysis |
| 62 | parent: ?*Instruction, |
| 63 | |
| 53 | 64 | pub fn cast(base: *Instruction, comptime T: type) ?*T { |
| 54 | 65 | if (base.id == comptime typeToId(T)) { |
| 55 | 66 | return @fieldParentPtr(T, "base", base); |
| ... | ... | @@ -81,6 +92,47 @@ pub const Instruction = struct { |
| 81 | 92 | unreachable; |
| 82 | 93 | } |
| 83 | 94 | |
| 95 | pub fn hasSideEffects(base: *const Instruction) bool { |
| 96 | comptime var i = 0; |
| 97 | inline while (i < @memberCount(Id)) : (i += 1) { |
| 98 | if (base.id == @field(Id, @memberName(Id, i))) { |
| 99 | const T = @field(Instruction, @memberName(Id, i)); |
| 100 | return @fieldParentPtr(T, "base", base).hasSideEffects(); |
| 101 | } |
| 102 | } |
| 103 | unreachable; |
| 104 | } |
| 105 | |
| 106 | pub fn analyze(base: *Instruction, ira: *Analyze) Analyze.Error!*Instruction { |
| 107 | comptime var i = 0; |
| 108 | inline while (i < @memberCount(Id)) : (i += 1) { |
| 109 | if (base.id == @field(Id, @memberName(Id, i))) { |
| 110 | const T = @field(Instruction, @memberName(Id, i)); |
| 111 | const new_inst = try @fieldParentPtr(T, "base", base).analyze(ira); |
| 112 | new_inst.linkToParent(base); |
| 113 | return new_inst; |
| 114 | } |
| 115 | } |
| 116 | unreachable; |
| 117 | } |
| 118 | |
| 119 | fn getAsParam(param: *Instruction) !*Instruction { |
| 120 | const child = param.child orelse return error.SemanticAnalysisFailed; |
| 121 | switch (child.val) { |
| 122 | IrVal.Unknown => return error.SemanticAnalysisFailed, |
| 123 | else => return child, |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// asserts that the type is known |
| 128 | fn getKnownType(self: *Instruction) *Type { |
| 129 | switch (self.val) { |
| 130 | IrVal.KnownType => |typeof| return typeof, |
| 131 | IrVal.KnownValue => |value| return value.typeof, |
| 132 | IrVal.Unknown => unreachable, |
| 133 | } |
| 134 | } |
| 135 | |
| 84 | 136 | pub fn setGenerated(base: *Instruction) void { |
| 85 | 137 | base.is_generated = true; |
| 86 | 138 | } |
| ... | ... | @@ -88,10 +140,18 @@ pub const Instruction = struct { |
| 88 | 140 | pub fn isNoReturn(base: *const Instruction) bool { |
| 89 | 141 | switch (base.val) { |
| 90 | 142 | IrVal.Unknown => return false, |
| 91 | | IrVal.Known => |x| return x.typeof.id == Type.Id.NoReturn, |
| 143 | IrVal.KnownValue => |x| return x.typeof.id == Type.Id.NoReturn, |
| 144 | IrVal.KnownType => |typeof| return typeof.id == Type.Id.NoReturn, |
| 92 | 145 | } |
| 93 | 146 | } |
| 94 | 147 | |
| 148 | pub fn linkToParent(self: *Instruction, parent: *Instruction) void { |
| 149 | assert(self.parent == null); |
| 150 | assert(parent.child == null); |
| 151 | self.parent = parent; |
| 152 | parent.child = self; |
| 153 | } |
| 154 | |
| 95 | 155 | pub const Id = enum { |
| 96 | 156 | Return, |
| 97 | 157 | Const, |
| ... | ... | @@ -100,196 +160,231 @@ pub const Instruction = struct { |
| 100 | 160 | CheckVoidStmt, |
| 101 | 161 | Phi, |
| 102 | 162 | Br, |
| 163 | AddImplicitReturnType, |
| 103 | 164 | }; |
| 104 | 165 | |
| 105 | 166 | pub const Const = struct { |
| 106 | 167 | base: Instruction, |
| 168 | params: Params, |
| 107 | 169 | |
| 108 | | pub fn buildBool(irb: *Builder, scope: *Scope, val: bool) !*Instruction { |
| 109 | | const inst = try irb.arena().create(Const{ |
| 110 | | .base = Instruction{ |
| 111 | | .id = Instruction.Id.Const, |
| 112 | | .is_generated = false, |
| 113 | | .scope = scope, |
| 114 | | .debug_id = irb.next_debug_id, |
| 115 | | .val = IrVal{ .Known = &Value.Bool.get(irb.module, val).base }, |
| 116 | | }, |
| 117 | | }); |
| 118 | | irb.next_debug_id += 1; |
| 119 | | try irb.current_basic_block.instruction_list.append(&inst.base); |
| 120 | | return &inst.base; |
| 121 | | } |
| 122 | | |
| 123 | | pub fn buildVoid(irb: *Builder, scope: *Scope, is_generated: bool) !*Instruction { |
| 124 | | const inst = try irb.arena().create(Const{ |
| 125 | | .base = Instruction{ |
| 126 | | .id = Instruction.Id.Const, |
| 127 | | .is_generated = is_generated, |
| 128 | | .scope = scope, |
| 129 | | .debug_id = irb.next_debug_id, |
| 130 | | .val = IrVal{ .Known = &Value.Void.get(irb.module).base }, |
| 131 | | }, |
| 132 | | }); |
| 133 | | irb.next_debug_id += 1; |
| 134 | | try irb.current_basic_block.instruction_list.append(&inst.base); |
| 135 | | return &inst.base; |
| 170 | const Params = struct {}; |
| 171 | |
| 172 | // Use Builder.buildConst* methods, or, after building a Const instruction, |
| 173 | // manually set the ir_val field. |
| 174 | const ir_val_init = IrVal.Init.Unknown; |
| 175 | |
| 176 | pub fn dump(self: *const Const) void { |
| 177 | self.base.val.KnownValue.dump(); |
| 136 | 178 | } |
| 137 | 179 | |
| 138 | | pub fn dump(inst: *const Const) void { |
| 139 | | inst.base.val.Known.dump(); |
| 180 | pub fn hasSideEffects(self: *const Const) bool { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | pub fn analyze(self: *const Const, ira: *Analyze) !*Instruction { |
| 185 | const new_inst = try ira.irb.build(Const, self.base.scope, self.base.span, Params{}); |
| 186 | new_inst.val = IrVal{ .KnownValue = self.base.val.KnownValue.getRef() }; |
| 187 | return new_inst; |
| 140 | 188 | } |
| 141 | 189 | }; |
| 142 | 190 | |
| 143 | 191 | pub const Return = struct { |
| 144 | 192 | base: Instruction, |
| 145 | | return_value: *Instruction, |
| 146 | | |
| 147 | | pub fn build(irb: *Builder, scope: *Scope, return_value: *Instruction) !*Instruction { |
| 148 | | const inst = try irb.arena().create(Return{ |
| 149 | | .base = Instruction{ |
| 150 | | .id = Instruction.Id.Return, |
| 151 | | .is_generated = false, |
| 152 | | .scope = scope, |
| 153 | | .debug_id = irb.next_debug_id, |
| 154 | | .val = IrVal{ .Known = &Value.Void.get(irb.module).base }, |
| 155 | | }, |
| 156 | | .return_value = return_value, |
| 157 | | }); |
| 158 | | irb.next_debug_id += 1; |
| 159 | | try irb.current_basic_block.instruction_list.append(&inst.base); |
| 160 | | return &inst.base; |
| 193 | params: Params, |
| 194 | |
| 195 | const Params = struct { |
| 196 | return_value: *Instruction, |
| 197 | }; |
| 198 | |
| 199 | const ir_val_init = IrVal.Init.NoReturn; |
| 200 | |
| 201 | pub fn dump(self: *const Return) void { |
| 202 | std.debug.warn("#{}", self.params.return_value.debug_id); |
| 161 | 203 | } |
| 162 | 204 | |
| 163 | | pub fn dump(inst: *const Return) void { |
| 164 | | std.debug.warn("#{}", inst.return_value.debug_id); |
| 205 | pub fn hasSideEffects(self: *const Return) bool { |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | pub fn analyze(self: *const Return, ira: *Analyze) !*Instruction { |
| 210 | const value = try self.params.return_value.getAsParam(); |
| 211 | const casted_value = try ira.implicitCast(value, ira.explicit_return_type); |
| 212 | |
| 213 | // TODO detect returning local variable address |
| 214 | |
| 215 | return ira.irb.build(Return, self.base.scope, self.base.span, Params{ .return_value = casted_value }); |
| 165 | 216 | } |
| 166 | 217 | }; |
| 167 | 218 | |
| 168 | 219 | pub const Ref = struct { |
| 169 | 220 | base: Instruction, |
| 170 | | target: *Instruction, |
| 171 | | mut: Mut, |
| 172 | | volatility: Volatility, |
| 221 | params: Params, |
| 173 | 222 | |
| 174 | | pub fn build( |
| 175 | | irb: *Builder, |
| 176 | | scope: *Scope, |
| 223 | const Params = struct { |
| 177 | 224 | target: *Instruction, |
| 178 | | mut: Mut, |
| 179 | | volatility: Volatility, |
| 180 | | ) !*Instruction { |
| 181 | | const inst = try irb.arena().create(Ref{ |
| 182 | | .base = Instruction{ |
| 183 | | .id = Instruction.Id.Ref, |
| 184 | | .is_generated = false, |
| 185 | | .scope = scope, |
| 186 | | .debug_id = irb.next_debug_id, |
| 187 | | .val = IrVal.Unknown, |
| 188 | | }, |
| 225 | mut: Type.Pointer.Mut, |
| 226 | volatility: Type.Pointer.Vol, |
| 227 | }; |
| 228 | |
| 229 | const ir_val_init = IrVal.Init.Unknown; |
| 230 | |
| 231 | pub fn dump(inst: *const Ref) void {} |
| 232 | |
| 233 | pub fn hasSideEffects(inst: *const Ref) bool { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | pub fn analyze(self: *const Ref, ira: *Analyze) !*Instruction { |
| 238 | const target = try self.params.target.getAsParam(); |
| 239 | |
| 240 | if (ira.getCompTimeValOrNullUndefOk(target)) |val| { |
| 241 | return ira.getCompTimeRef( |
| 242 | val, |
| 243 | Value.Ptr.Mut.CompTimeConst, |
| 244 | self.params.mut, |
| 245 | self.params.volatility, |
| 246 | val.typeof.getAbiAlignment(ira.irb.module), |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | const new_inst = try ira.irb.build(Ref, self.base.scope, self.base.span, Params{ |
| 189 | 251 | .target = target, |
| 190 | | .mut = mut, |
| 191 | | .volatility = volatility, |
| 252 | .mut = self.params.mut, |
| 253 | .volatility = self.params.volatility, |
| 192 | 254 | }); |
| 193 | | irb.next_debug_id += 1; |
| 194 | | try irb.current_basic_block.instruction_list.append(&inst.base); |
| 195 | | return &inst.base; |
| 255 | const elem_type = target.getKnownType(); |
| 256 | const ptr_type = Type.Pointer.get( |
| 257 | ira.irb.module, |
| 258 | elem_type, |
| 259 | self.params.mut, |
| 260 | self.params.volatility, |
| 261 | Type.Pointer.Size.One, |
| 262 | elem_type.getAbiAlignment(ira.irb.module), |
| 263 | ); |
| 264 | // TODO: potentially set the hint that this is a stack pointer. But it might not be - this |
| 265 | // could be a ref of a global, for example |
| 266 | new_inst.val = IrVal{ .KnownType = &ptr_type.base }; |
| 267 | // TODO potentially add an alloca entry here |
| 268 | return new_inst; |
| 196 | 269 | } |
| 197 | | |
| 198 | | pub fn dump(inst: *const Ref) void {} |
| 199 | 270 | }; |
| 200 | 271 | |
| 201 | 272 | pub const DeclVar = struct { |
| 202 | 273 | base: Instruction, |
| 203 | | variable: *Variable, |
| 274 | params: Params, |
| 275 | |
| 276 | const Params = struct { |
| 277 | variable: *Variable, |
| 278 | }; |
| 279 | |
| 280 | const ir_val_init = IrVal.Init.Unknown; |
| 204 | 281 | |
| 205 | 282 | pub fn dump(inst: *const DeclVar) void {} |
| 283 | |
| 284 | pub fn hasSideEffects(inst: *const DeclVar) bool { |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | pub fn analyze(self: *const DeclVar, ira: *Analyze) !*Instruction { |
| 289 | return error.Unimplemented; // TODO |
| 290 | } |
| 206 | 291 | }; |
| 207 | 292 | |
| 208 | 293 | pub const CheckVoidStmt = struct { |
| 209 | 294 | base: Instruction, |
| 210 | | target: *Instruction, |
| 295 | params: Params, |
| 211 | 296 | |
| 212 | | pub fn build( |
| 213 | | irb: *Builder, |
| 214 | | scope: *Scope, |
| 297 | const Params = struct { |
| 215 | 298 | target: *Instruction, |
| 216 | | ) !*Instruction { |
| 217 | | const inst = try irb.arena().create(CheckVoidStmt{ |
| 218 | | .base = Instruction{ |
| 219 | | .id = Instruction.Id.CheckVoidStmt, |
| 220 | | .is_generated = true, |
| 221 | | .scope = scope, |
| 222 | | .debug_id = irb.next_debug_id, |
| 223 | | .val = IrVal{ .Known = &Value.Void.get(irb.module).base }, |
| 224 | | }, |
| 225 | | .target = target, |
| 226 | | }); |
| 227 | | irb.next_debug_id += 1; |
| 228 | | try irb.current_basic_block.instruction_list.append(&inst.base); |
| 229 | | return &inst.base; |
| 230 | | } |
| 299 | }; |
| 300 | |
| 301 | const ir_val_init = IrVal.Init.Unknown; |
| 231 | 302 | |
| 232 | 303 | pub fn dump(inst: *const CheckVoidStmt) void {} |
| 304 | |
| 305 | pub fn hasSideEffects(inst: *const CheckVoidStmt) bool { |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | pub fn analyze(self: *const CheckVoidStmt, ira: *Analyze) !*Instruction { |
| 310 | return error.Unimplemented; // TODO |
| 311 | } |
| 233 | 312 | }; |
| 234 | 313 | |
| 235 | 314 | pub const Phi = struct { |
| 236 | 315 | base: Instruction, |
| 237 | | incoming_blocks: []*BasicBlock, |
| 238 | | incoming_values: []*Instruction, |
| 316 | params: Params, |
| 239 | 317 | |
| 240 | | pub fn build( |
| 241 | | irb: *Builder, |
| 242 | | scope: *Scope, |
| 318 | const Params = struct { |
| 243 | 319 | incoming_blocks: []*BasicBlock, |
| 244 | 320 | incoming_values: []*Instruction, |
| 245 | | ) !*Instruction { |
| 246 | | const inst = try irb.arena().create(Phi{ |
| 247 | | .base = Instruction{ |
| 248 | | .id = Instruction.Id.Phi, |
| 249 | | .is_generated = false, |
| 250 | | .scope = scope, |
| 251 | | .debug_id = irb.next_debug_id, |
| 252 | | .val = IrVal.Unknown, |
| 253 | | }, |
| 254 | | .incoming_blocks = incoming_blocks, |
| 255 | | .incoming_values = incoming_values, |
| 256 | | }); |
| 257 | | irb.next_debug_id += 1; |
| 258 | | try irb.current_basic_block.instruction_list.append(&inst.base); |
| 259 | | return &inst.base; |
| 260 | | } |
| 321 | }; |
| 322 | |
| 323 | const ir_val_init = IrVal.Init.Unknown; |
| 261 | 324 | |
| 262 | 325 | pub fn dump(inst: *const Phi) void {} |
| 326 | |
| 327 | pub fn hasSideEffects(inst: *const Phi) bool { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | pub fn analyze(self: *const Phi, ira: *Analyze) !*Instruction { |
| 332 | return error.Unimplemented; // TODO |
| 333 | } |
| 263 | 334 | }; |
| 264 | 335 | |
| 265 | 336 | pub const Br = struct { |
| 266 | 337 | base: Instruction, |
| 267 | | dest_block: *BasicBlock, |
| 268 | | is_comptime: *Instruction, |
| 338 | params: Params, |
| 269 | 339 | |
| 270 | | pub fn build( |
| 271 | | irb: *Builder, |
| 272 | | scope: *Scope, |
| 340 | const Params = struct { |
| 273 | 341 | dest_block: *BasicBlock, |
| 274 | 342 | is_comptime: *Instruction, |
| 275 | | ) !*Instruction { |
| 276 | | const inst = try irb.arena().create(Br{ |
| 277 | | .base = Instruction{ |
| 278 | | .id = Instruction.Id.Br, |
| 279 | | .is_generated = false, |
| 280 | | .scope = scope, |
| 281 | | .debug_id = irb.next_debug_id, |
| 282 | | .val = IrVal{ .Known = &Value.NoReturn.get(irb.module).base }, |
| 283 | | }, |
| 284 | | .dest_block = dest_block, |
| 285 | | .is_comptime = is_comptime, |
| 286 | | }); |
| 287 | | irb.next_debug_id += 1; |
| 288 | | try irb.current_basic_block.instruction_list.append(&inst.base); |
| 289 | | return &inst.base; |
| 290 | | } |
| 343 | }; |
| 344 | |
| 345 | const ir_val_init = IrVal.Init.NoReturn; |
| 291 | 346 | |
| 292 | 347 | pub fn dump(inst: *const Br) void {} |
| 348 | |
| 349 | pub fn hasSideEffects(inst: *const Br) bool { |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | pub fn analyze(self: *const Br, ira: *Analyze) !*Instruction { |
| 354 | return error.Unimplemented; // TODO |
| 355 | } |
| 356 | }; |
| 357 | |
| 358 | pub const AddImplicitReturnType = struct { |
| 359 | base: Instruction, |
| 360 | params: Params, |
| 361 | |
| 362 | pub const Params = struct { |
| 363 | target: *Instruction, |
| 364 | }; |
| 365 | |
| 366 | const ir_val_init = IrVal.Init.Unknown; |
| 367 | |
| 368 | pub fn dump(inst: *const AddImplicitReturnType) void { |
| 369 | std.debug.warn("#{}", inst.params.target.debug_id); |
| 370 | } |
| 371 | |
| 372 | pub fn hasSideEffects(inst: *const AddImplicitReturnType) bool { |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | pub fn analyze(self: *const AddImplicitReturnType, ira: *Analyze) !*Instruction { |
| 377 | const target = try self.params.target.getAsParam(); |
| 378 | |
| 379 | try ira.src_implicit_return_type_list.append(target); |
| 380 | |
| 381 | return ira.irb.build( |
| 382 | AddImplicitReturnType, |
| 383 | self.base.scope, |
| 384 | self.base.span, |
| 385 | Params{ .target = target }, |
| 386 | ); |
| 387 | } |
| 293 | 388 | }; |
| 294 | 389 | }; |
| 295 | 390 | |
| ... | ... | @@ -303,16 +398,31 @@ pub const BasicBlock = struct { |
| 303 | 398 | debug_id: usize, |
| 304 | 399 | scope: *Scope, |
| 305 | 400 | instruction_list: std.ArrayList(*Instruction), |
| 401 | ref_instruction: ?*Instruction, |
| 402 | |
| 403 | /// the basic block that is derived from this one in analysis |
| 404 | child: ?*BasicBlock, |
| 405 | |
| 406 | /// the basic block that this one derives from in analysis |
| 407 | parent: ?*BasicBlock, |
| 306 | 408 | |
| 307 | 409 | pub fn ref(self: *BasicBlock) void { |
| 308 | 410 | self.ref_count += 1; |
| 309 | 411 | } |
| 412 | |
| 413 | pub fn linkToParent(self: *BasicBlock, parent: *BasicBlock) void { |
| 414 | assert(self.parent == null); |
| 415 | assert(parent.child == null); |
| 416 | self.parent = parent; |
| 417 | parent.child = self; |
| 418 | } |
| 310 | 419 | }; |
| 311 | 420 | |
| 312 | 421 | /// Stuff that survives longer than Builder |
| 313 | 422 | pub const Code = struct { |
| 314 | 423 | basic_block_list: std.ArrayList(*BasicBlock), |
| 315 | 424 | arena: std.heap.ArenaAllocator, |
| 425 | return_type: ?*Type, |
| 316 | 426 | |
| 317 | 427 | /// allocator is module.a() |
| 318 | 428 | pub fn destroy(self: *Code, allocator: *Allocator) void { |
| ... | ... | @@ -341,15 +451,13 @@ pub const Builder = struct { |
| 341 | 451 | parsed_file: *ParsedFile, |
| 342 | 452 | is_comptime: bool, |
| 343 | 453 | |
| 344 | | pub const Error = error{ |
| 345 | | OutOfMemory, |
| 346 | | Unimplemented, |
| 347 | | }; |
| 454 | pub const Error = Analyze.Error; |
| 348 | 455 | |
| 349 | 456 | pub fn init(module: *Module, parsed_file: *ParsedFile) !Builder { |
| 350 | 457 | const code = try module.a().create(Code{ |
| 351 | 458 | .basic_block_list = undefined, |
| 352 | 459 | .arena = std.heap.ArenaAllocator.init(module.a()), |
| 460 | .return_type = null, |
| 353 | 461 | }); |
| 354 | 462 | code.basic_block_list = std.ArrayList(*BasicBlock).init(&code.arena.allocator); |
| 355 | 463 | errdefer code.destroy(module.a()); |
| ... | ... | @@ -381,6 +489,9 @@ pub const Builder = struct { |
| 381 | 489 | .debug_id = self.next_debug_id, |
| 382 | 490 | .scope = scope, |
| 383 | 491 | .instruction_list = std.ArrayList(*Instruction).init(self.arena()), |
| 492 | .child = null, |
| 493 | .parent = null, |
| 494 | .ref_instruction = null, |
| 384 | 495 | }); |
| 385 | 496 | self.next_debug_id += 1; |
| 386 | 497 | return basic_block; |
| ... | ... | @@ -490,14 +601,18 @@ pub const Builder = struct { |
| 490 | 601 | |
| 491 | 602 | if (block.statements.len == 0) { |
| 492 | 603 | // {} |
| 493 | | return Instruction.Const.buildVoid(irb, child_scope, false); |
| 604 | return irb.buildConstVoid(child_scope, Span.token(block.lbrace), false); |
| 494 | 605 | } |
| 495 | 606 | |
| 496 | 607 | if (block.label) |label| { |
| 497 | 608 | block_scope.incoming_values = std.ArrayList(*Instruction).init(irb.arena()); |
| 498 | 609 | block_scope.incoming_blocks = std.ArrayList(*BasicBlock).init(irb.arena()); |
| 499 | 610 | block_scope.end_block = try irb.createBasicBlock(parent_scope, "BlockEnd"); |
| 500 | | block_scope.is_comptime = try Instruction.Const.buildBool(irb, parent_scope, irb.isCompTime(parent_scope)); |
| 611 | block_scope.is_comptime = try irb.buildConstBool( |
| 612 | parent_scope, |
| 613 | Span.token(block.lbrace), |
| 614 | irb.isCompTime(parent_scope), |
| 615 | ); |
| 501 | 616 | } |
| 502 | 617 | |
| 503 | 618 | var is_continuation_unreachable = false; |
| ... | ... | @@ -530,10 +645,15 @@ pub const Builder = struct { |
| 530 | 645 | |
| 531 | 646 | if (statement_value.cast(Instruction.DeclVar)) |decl_var| { |
| 532 | 647 | // variable declarations start a new scope |
| 533 | | child_scope = decl_var.variable.child_scope; |
| 648 | child_scope = decl_var.params.variable.child_scope; |
| 534 | 649 | } else if (!is_continuation_unreachable) { |
| 535 | 650 | // this statement's value must be void |
| 536 | | _ = Instruction.CheckVoidStmt.build(irb, child_scope, statement_value); |
| 651 | _ = irb.build( |
| 652 | Instruction.CheckVoidStmt, |
| 653 | child_scope, |
| 654 | statement_value.span, |
| 655 | Instruction.CheckVoidStmt.Params{ .target = statement_value }, |
| 656 | ); |
| 537 | 657 | } |
| 538 | 658 | } |
| 539 | 659 | |
| ... | ... | @@ -544,37 +664,34 @@ pub const Builder = struct { |
| 544 | 664 | } |
| 545 | 665 | |
| 546 | 666 | try irb.setCursorAtEndAndAppendBlock(block_scope.end_block); |
| 547 | | return Instruction.Phi.build( |
| 548 | | irb, |
| 549 | | parent_scope, |
| 550 | | block_scope.incoming_blocks.toOwnedSlice(), |
| 551 | | block_scope.incoming_values.toOwnedSlice(), |
| 552 | | ); |
| 667 | return irb.build(Instruction.Phi, parent_scope, Span.token(block.rbrace), Instruction.Phi.Params{ |
| 668 | .incoming_blocks = block_scope.incoming_blocks.toOwnedSlice(), |
| 669 | .incoming_values = block_scope.incoming_values.toOwnedSlice(), |
| 670 | }); |
| 553 | 671 | } |
| 554 | 672 | |
| 555 | 673 | if (block.label) |label| { |
| 556 | 674 | try block_scope.incoming_blocks.append(irb.current_basic_block); |
| 557 | 675 | try block_scope.incoming_values.append( |
| 558 | | try Instruction.Const.buildVoid(irb, parent_scope, true), |
| 676 | try irb.buildConstVoid(parent_scope, Span.token(block.rbrace), true), |
| 559 | 677 | ); |
| 560 | 678 | _ = try irb.genDefersForBlock(child_scope, outer_block_scope, Scope.Defer.Kind.ScopeExit); |
| 561 | | (try Instruction.Br.build( |
| 562 | | irb, |
| 563 | | parent_scope, |
| 564 | | block_scope.end_block, |
| 565 | | block_scope.is_comptime, |
| 566 | | )).setGenerated(); |
| 679 | |
| 680 | _ = try irb.buildGen(Instruction.Br, parent_scope, Span.token(block.rbrace), Instruction.Br.Params{ |
| 681 | .dest_block = block_scope.end_block, |
| 682 | .is_comptime = block_scope.is_comptime, |
| 683 | }); |
| 684 | |
| 567 | 685 | try irb.setCursorAtEndAndAppendBlock(block_scope.end_block); |
| 568 | | return Instruction.Phi.build( |
| 569 | | irb, |
| 570 | | parent_scope, |
| 571 | | block_scope.incoming_blocks.toOwnedSlice(), |
| 572 | | block_scope.incoming_values.toOwnedSlice(), |
| 573 | | ); |
| 686 | |
| 687 | return irb.build(Instruction.Phi, parent_scope, Span.token(block.rbrace), Instruction.Phi.Params{ |
| 688 | .incoming_blocks = block_scope.incoming_blocks.toOwnedSlice(), |
| 689 | .incoming_values = block_scope.incoming_values.toOwnedSlice(), |
| 690 | }); |
| 574 | 691 | } |
| 575 | 692 | |
| 576 | 693 | _ = try irb.genDefersForBlock(child_scope, outer_block_scope, Scope.Defer.Kind.ScopeExit); |
| 577 | | return try Instruction.Const.buildVoid(irb, child_scope, true); |
| 694 | return irb.buildConstVoid(child_scope, Span.token(block.rbrace), true); |
| 578 | 695 | } |
| 579 | 696 | |
| 580 | 697 | fn genDefersForBlock( |
| ... | ... | @@ -603,7 +720,12 @@ pub const Builder = struct { |
| 603 | 720 | if (instruction.isNoReturn()) { |
| 604 | 721 | is_noreturn = true; |
| 605 | 722 | } else { |
| 606 | | _ = Instruction.CheckVoidStmt.build(irb, &defer_expr_scope.base, instruction); |
| 723 | _ = try irb.build( |
| 724 | Instruction.CheckVoidStmt, |
| 725 | &defer_expr_scope.base, |
| 726 | Span.token(defer_expr_scope.expr_node.lastToken()), |
| 727 | Instruction.CheckVoidStmt.Params{ .target = instruction }, |
| 728 | ); |
| 607 | 729 | } |
| 608 | 730 | } |
| 609 | 731 | }, |
| ... | ... | @@ -626,7 +748,11 @@ pub const Builder = struct { |
| 626 | 748 | LVal.Ptr => { |
| 627 | 749 | // We needed a pointer to a value, but we got a value. So we create |
| 628 | 750 | // an instruction which just makes a const pointer of it. |
| 629 | | return Instruction.Ref.build(irb, scope, instruction, Mut.Const, Volatility.NonVolatile); |
| 751 | return irb.build(Instruction.Ref, scope, instruction.span, Instruction.Ref.Params{ |
| 752 | .target = instruction, |
| 753 | .mut = Type.Pointer.Mut.Const, |
| 754 | .volatility = Type.Pointer.Vol.Non, |
| 755 | }); |
| 630 | 756 | }, |
| 631 | 757 | } |
| 632 | 758 | } |
| ... | ... | @@ -634,9 +760,218 @@ pub const Builder = struct { |
| 634 | 760 | fn arena(self: *Builder) *Allocator { |
| 635 | 761 | return &self.code.arena.allocator; |
| 636 | 762 | } |
| 763 | |
| 764 | fn buildExtra( |
| 765 | self: *Builder, |
| 766 | comptime I: type, |
| 767 | scope: *Scope, |
| 768 | span: Span, |
| 769 | params: I.Params, |
| 770 | is_generated: bool, |
| 771 | ) !*Instruction { |
| 772 | const inst = try self.arena().create(I{ |
| 773 | .base = Instruction{ |
| 774 | .id = Instruction.typeToId(I), |
| 775 | .is_generated = is_generated, |
| 776 | .scope = scope, |
| 777 | .debug_id = self.next_debug_id, |
| 778 | .val = switch (I.ir_val_init) { |
| 779 | IrVal.Init.Unknown => IrVal.Unknown, |
| 780 | IrVal.Init.NoReturn => IrVal{ .KnownValue = &Value.NoReturn.get(self.module).base }, |
| 781 | IrVal.Init.Void => IrVal{ .KnownValue = &Value.Void.get(self.module).base }, |
| 782 | }, |
| 783 | .ref_count = 0, |
| 784 | .span = span, |
| 785 | .child = null, |
| 786 | .parent = null, |
| 787 | }, |
| 788 | .params = params, |
| 789 | }); |
| 790 | |
| 791 | // Look at the params and ref() other instructions |
| 792 | comptime var i = 0; |
| 793 | inline while (i < @memberCount(I.Params)) : (i += 1) { |
| 794 | const FieldType = comptime @typeOf(@field(I.Params(undefined), @memberName(I.Params, i))); |
| 795 | switch (FieldType) { |
| 796 | *Instruction => @field(inst.params, @memberName(I.Params, i)).ref_count += 1, |
| 797 | ?*Instruction => if (@field(inst.params, @memberName(I.Params, i))) |other| other.ref_count += 1, |
| 798 | else => {}, |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | self.next_debug_id += 1; |
| 803 | try self.current_basic_block.instruction_list.append(&inst.base); |
| 804 | return &inst.base; |
| 805 | } |
| 806 | |
| 807 | fn build( |
| 808 | self: *Builder, |
| 809 | comptime I: type, |
| 810 | scope: *Scope, |
| 811 | span: Span, |
| 812 | params: I.Params, |
| 813 | ) !*Instruction { |
| 814 | return self.buildExtra(I, scope, span, params, false); |
| 815 | } |
| 816 | |
| 817 | fn buildGen( |
| 818 | self: *Builder, |
| 819 | comptime I: type, |
| 820 | scope: *Scope, |
| 821 | span: Span, |
| 822 | params: I.Params, |
| 823 | ) !*Instruction { |
| 824 | return self.buildExtra(I, scope, span, params, true); |
| 825 | } |
| 826 | |
| 827 | fn buildConstBool(self: *Builder, scope: *Scope, span: Span, x: bool) !*Instruction { |
| 828 | const inst = try self.build(Instruction.Const, scope, span, Instruction.Const.Params{}); |
| 829 | inst.val = IrVal{ .KnownValue = &Value.Bool.get(self.module, x).base }; |
| 830 | return inst; |
| 831 | } |
| 832 | |
| 833 | fn buildConstVoid(self: *Builder, scope: *Scope, span: Span, is_generated: bool) !*Instruction { |
| 834 | const inst = try self.buildExtra(Instruction.Const, scope, span, Instruction.Const.Params{}, is_generated); |
| 835 | inst.val = IrVal{ .KnownValue = &Value.Void.get(self.module).base }; |
| 836 | return inst; |
| 837 | } |
| 838 | }; |
| 839 | |
| 840 | const Analyze = struct { |
| 841 | irb: Builder, |
| 842 | old_bb_index: usize, |
| 843 | const_predecessor_bb: ?*BasicBlock, |
| 844 | parent_basic_block: *BasicBlock, |
| 845 | instruction_index: usize, |
| 846 | src_implicit_return_type_list: std.ArrayList(*Instruction), |
| 847 | explicit_return_type: ?*Type, |
| 848 | |
| 849 | pub const Error = error{ |
| 850 | /// This is only for when we have already reported a compile error. It is the poison value. |
| 851 | SemanticAnalysisFailed, |
| 852 | |
| 853 | /// This is a placeholder - it is useful to use instead of panicking but once the compiler is |
| 854 | /// done this error code will be removed. |
| 855 | Unimplemented, |
| 856 | |
| 857 | OutOfMemory, |
| 858 | }; |
| 859 | |
| 860 | pub fn init(module: *Module, parsed_file: *ParsedFile, explicit_return_type: ?*Type) !Analyze { |
| 861 | var irb = try Builder.init(module, parsed_file); |
| 862 | errdefer irb.abort(); |
| 863 | |
| 864 | return Analyze{ |
| 865 | .irb = irb, |
| 866 | .old_bb_index = 0, |
| 867 | .const_predecessor_bb = null, |
| 868 | .parent_basic_block = undefined, // initialized with startBasicBlock |
| 869 | .instruction_index = undefined, // initialized with startBasicBlock |
| 870 | .src_implicit_return_type_list = std.ArrayList(*Instruction).init(irb.arena()), |
| 871 | .explicit_return_type = explicit_return_type, |
| 872 | }; |
| 873 | } |
| 874 | |
| 875 | pub fn abort(self: *Analyze) void { |
| 876 | self.irb.abort(); |
| 877 | } |
| 878 | |
| 879 | pub fn getNewBasicBlock(self: *Analyze, old_bb: *BasicBlock, ref_old_instruction: ?*Instruction) !*BasicBlock { |
| 880 | if (old_bb.child) |child| { |
| 881 | if (ref_old_instruction == null or child.ref_instruction != ref_old_instruction) |
| 882 | return child; |
| 883 | } |
| 884 | |
| 885 | const new_bb = try self.irb.createBasicBlock(old_bb.scope, old_bb.name_hint); |
| 886 | new_bb.linkToParent(old_bb); |
| 887 | new_bb.ref_instruction = ref_old_instruction; |
| 888 | return new_bb; |
| 889 | } |
| 890 | |
| 891 | pub fn startBasicBlock(self: *Analyze, old_bb: *BasicBlock, const_predecessor_bb: ?*BasicBlock) void { |
| 892 | self.instruction_index = 0; |
| 893 | self.parent_basic_block = old_bb; |
| 894 | self.const_predecessor_bb = const_predecessor_bb; |
| 895 | } |
| 896 | |
| 897 | pub fn finishBasicBlock(ira: *Analyze, old_code: *Code) !void { |
| 898 | try ira.irb.code.basic_block_list.append(ira.irb.current_basic_block); |
| 899 | ira.instruction_index += 1; |
| 900 | |
| 901 | while (ira.instruction_index < ira.parent_basic_block.instruction_list.len) { |
| 902 | const next_instruction = ira.parent_basic_block.instruction_list.at(ira.instruction_index); |
| 903 | |
| 904 | if (!next_instruction.is_generated) { |
| 905 | try ira.addCompileError(next_instruction.span, "unreachable code"); |
| 906 | break; |
| 907 | } |
| 908 | ira.instruction_index += 1; |
| 909 | } |
| 910 | |
| 911 | ira.old_bb_index += 1; |
| 912 | |
| 913 | var need_repeat = true; |
| 914 | while (true) { |
| 915 | while (ira.old_bb_index < old_code.basic_block_list.len) { |
| 916 | const old_bb = old_code.basic_block_list.at(ira.old_bb_index); |
| 917 | const new_bb = old_bb.child orelse { |
| 918 | ira.old_bb_index += 1; |
| 919 | continue; |
| 920 | }; |
| 921 | if (new_bb.instruction_list.len != 0) { |
| 922 | ira.old_bb_index += 1; |
| 923 | continue; |
| 924 | } |
| 925 | ira.irb.current_basic_block = new_bb; |
| 926 | |
| 927 | ira.startBasicBlock(old_bb, null); |
| 928 | return; |
| 929 | } |
| 930 | if (!need_repeat) |
| 931 | return; |
| 932 | need_repeat = false; |
| 933 | ira.old_bb_index = 0; |
| 934 | continue; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | fn addCompileError(self: *Analyze, span: Span, comptime fmt: []const u8, args: ...) !void { |
| 939 | return self.irb.module.addCompileError(self.irb.parsed_file, span, fmt, args); |
| 940 | } |
| 941 | |
| 942 | fn resolvePeerTypes(self: *Analyze, expected_type: ?*Type, peers: []const *Instruction) Analyze.Error!*Type { |
| 943 | // TODO actual implementation |
| 944 | return &Type.Void.get(self.irb.module).base; |
| 945 | } |
| 946 | |
| 947 | fn implicitCast(self: *Analyze, target: *Instruction, optional_dest_type: ?*Type) Analyze.Error!*Instruction { |
| 948 | const dest_type = optional_dest_type orelse return target; |
| 949 | @panic("TODO implicitCast"); |
| 950 | } |
| 951 | |
| 952 | fn getCompTimeValOrNullUndefOk(self: *Analyze, target: *Instruction) ?*Value { |
| 953 | @panic("TODO getCompTimeValOrNullUndefOk"); |
| 954 | } |
| 955 | |
| 956 | fn getCompTimeRef( |
| 957 | self: *Analyze, |
| 958 | value: *Value, |
| 959 | ptr_mut: Value.Ptr.Mut, |
| 960 | mut: Type.Pointer.Mut, |
| 961 | volatility: Type.Pointer.Vol, |
| 962 | ptr_align: u32, |
| 963 | ) Analyze.Error!*Instruction { |
| 964 | @panic("TODO getCompTimeRef"); |
| 965 | } |
| 637 | 966 | }; |
| 638 | 967 | |
| 639 | | pub async fn gen(module: *Module, body_node: *ast.Node, scope: *Scope, parsed_file: *ParsedFile) !*Code { |
| 968 | pub async fn gen( |
| 969 | module: *Module, |
| 970 | body_node: *ast.Node, |
| 971 | scope: *Scope, |
| 972 | end_span: Span, |
| 973 | parsed_file: *ParsedFile, |
| 974 | ) !*Code { |
| 640 | 975 | var irb = try Builder.init(module, parsed_file); |
| 641 | 976 | errdefer irb.abort(); |
| 642 | 977 | |
| ... | ... | @@ -646,8 +981,61 @@ pub async fn gen(module: *Module, body_node: *ast.Node, scope: *Scope, parsed_fi |
| 646 | 981 | |
| 647 | 982 | const result = try irb.genNode(body_node, scope, LVal.None); |
| 648 | 983 | if (!result.isNoReturn()) { |
| 649 | | (try Instruction.Return.build(&irb, scope, result)).setGenerated(); |
| 984 | _ = irb.buildGen( |
| 985 | Instruction.AddImplicitReturnType, |
| 986 | scope, |
| 987 | end_span, |
| 988 | Instruction.AddImplicitReturnType.Params{ .target = result }, |
| 989 | ); |
| 990 | _ = irb.buildGen( |
| 991 | Instruction.Return, |
| 992 | scope, |
| 993 | end_span, |
| 994 | Instruction.Return.Params{ .return_value = result }, |
| 995 | ); |
| 650 | 996 | } |
| 651 | 997 | |
| 652 | 998 | return irb.finish(); |
| 653 | 999 | } |
| 1000 | |
| 1001 | pub async fn analyze(module: *Module, parsed_file: *ParsedFile, old_code: *Code, expected_type: ?*Type) !*Code { |
| 1002 | var ira = try Analyze.init(module, parsed_file, expected_type); |
| 1003 | errdefer ira.abort(); |
| 1004 | |
| 1005 | const old_entry_bb = old_code.basic_block_list.at(0); |
| 1006 | |
| 1007 | const new_entry_bb = try ira.getNewBasicBlock(old_entry_bb, null); |
| 1008 | new_entry_bb.ref(); |
| 1009 | |
| 1010 | ira.irb.current_basic_block = new_entry_bb; |
| 1011 | |
| 1012 | ira.startBasicBlock(old_entry_bb, null); |
| 1013 | |
| 1014 | while (ira.old_bb_index < old_code.basic_block_list.len) { |
| 1015 | const old_instruction = ira.parent_basic_block.instruction_list.at(ira.instruction_index); |
| 1016 | |
| 1017 | if (old_instruction.ref_count == 0 and !old_instruction.hasSideEffects()) { |
| 1018 | ira.instruction_index += 1; |
| 1019 | continue; |
| 1020 | } |
| 1021 | |
| 1022 | const return_inst = try old_instruction.analyze(&ira); |
| 1023 | // Note: if we ever modify the above to handle error.CompileError by continuing analysis, |
| 1024 | // then here we want to check if ira.isCompTime() and return early if true |
| 1025 | |
| 1026 | if (return_inst.isNoReturn()) { |
| 1027 | try ira.finishBasicBlock(old_code); |
| 1028 | continue; |
| 1029 | } |
| 1030 | |
| 1031 | ira.instruction_index += 1; |
| 1032 | } |
| 1033 | |
| 1034 | if (ira.src_implicit_return_type_list.len == 0) { |
| 1035 | ira.irb.code.return_type = &Type.NoReturn.get(module).base; |
| 1036 | return ira.irb.finish(); |
| 1037 | } |
| 1038 | |
| 1039 | ira.irb.code.return_type = try ira.resolvePeerTypes(expected_type, ira.src_implicit_return_type_list.toSliceConst()); |
| 1040 | return ira.irb.finish(); |
| 1041 | } |