| ... | ... | @@ -6,7 +6,7 @@ |
| 6 | 6 | //! This is the the heart of the Zig compiler. |
| 7 | 7 | |
| 8 | 8 | mod: *Module, |
| 9 | | /// Same as `mod.gpa`. |
| 9 | /// Alias to `mod.gpa`. |
| 10 | 10 | gpa: *Allocator, |
| 11 | 11 | /// Points to the arena allocator of the Decl. |
| 12 | 12 | arena: *Allocator, |
| ... | ... | @@ -53,22 +53,6 @@ const InnerError = Module.InnerError; |
| 53 | 53 | const Decl = Module.Decl; |
| 54 | 54 | const LazySrcLoc = Module.LazySrcLoc; |
| 55 | 55 | |
| 56 | | // TODO when memory layout of TZIR is reworked, this can be simplified. |
| 57 | | const const_tzir_inst_list = blk: { |
| 58 | | var result: [zir.const_inst_list.len]ir.Inst.Const = undefined; |
| 59 | | for (result) |*tzir_const, i| { |
| 60 | | tzir_const.* = .{ |
| 61 | | .base = .{ |
| 62 | | .tag = .constant, |
| 63 | | .ty = zir.const_inst_list[i].ty, |
| 64 | | .src = 0, |
| 65 | | }, |
| 66 | | .val = zir.const_inst_list[i].val, |
| 67 | | }; |
| 68 | | } |
| 69 | | break :blk result; |
| 70 | | }; |
| 71 | | |
| 72 | 56 | pub fn root(sema: *Sema, root_block: *Scope.Block) !void { |
| 73 | 57 | const root_body = sema.code.extra[sema.code.root_start..][0..sema.code.root_len]; |
| 74 | 58 | return sema.analyzeBody(root_block, root_body); |
| ... | ... | @@ -246,27 +230,26 @@ pub fn analyzeBody(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Inde |
| 246 | 230 | } |
| 247 | 231 | } |
| 248 | 232 | |
| 249 | | pub fn resolveInst(sema: *Sema, block: *Scope.Block, zir_ref: zir.Inst.Ref) *const ir.Inst { |
| 233 | /// TODO when we rework TZIR memory layout, this function will no longer have a possible error. |
| 234 | pub fn resolveInst(sema: *Sema, zir_ref: zir.Inst.Ref) error{OutOfMemory}!*ir.Inst { |
| 250 | 235 | var i = zir_ref; |
| 251 | 236 | |
| 252 | 237 | // First section of indexes correspond to a set number of constant values. |
| 253 | | if (i < const_tzir_inst_list.len) { |
| 254 | | return &const_tzir_inst_list[i]; |
| 238 | if (i < zir.const_inst_list.len) { |
| 239 | // TODO when we rework TZIR memory layout, this function can be as simple as: |
| 240 | // if (zir_ref < zir.const_inst_list.len + sema.param_count) |
| 241 | // return zir_ref; |
| 242 | // Until then we allocate memory for a new, mutable `ir.Inst` to match what |
| 243 | // TZIR expects. |
| 244 | return sema.mod.constInst(sema.arena, .unneeded, zir.const_inst_list[i]); |
| 255 | 245 | } |
| 256 | | i -= const_tzir_inst_list.len; |
| 246 | i -= zir.const_inst_list.len; |
| 257 | 247 | |
| 258 | 248 | // Next section of indexes correspond to function parameters, if any. |
| 259 | | if (block.inlining) |inlining| { |
| 260 | | if (i < inlining.casted_args.len) { |
| 261 | | return inlining.casted_args[i]; |
| 262 | | } |
| 263 | | i -= inlining.casted_args.len; |
| 264 | | } else { |
| 265 | | if (i < sema.param_inst_list.len) { |
| 266 | | return sema.param_inst_list[i]; |
| 267 | | } |
| 268 | | i -= sema.param_inst_list.len; |
| 249 | if (i < sema.param_inst_list.len) { |
| 250 | return sema.param_inst_list[i]; |
| 269 | 251 | } |
| 252 | i -= sema.param_inst_list.len; |
| 270 | 253 | |
| 271 | 254 | // Finally, the last section of indexes refers to the map of ZIR=>TZIR. |
| 272 | 255 | return sema.inst_map[i]; |
| ... | ... | @@ -278,17 +261,17 @@ fn resolveConstString( |
| 278 | 261 | src: LazySrcLoc, |
| 279 | 262 | zir_ref: zir.Inst.Ref, |
| 280 | 263 | ) ![]u8 { |
| 281 | | const tzir_inst = sema.resolveInst(block, zir_ref); |
| 264 | const tzir_inst = try sema.resolveInst(zir_ref); |
| 282 | 265 | const wanted_type = Type.initTag(.const_slice_u8); |
| 283 | | const coerced_inst = try sema.coerce(block, wanted_type, tzir_inst); |
| 266 | const coerced_inst = try sema.coerce(block, wanted_type, tzir_inst, src); |
| 284 | 267 | const val = try sema.resolveConstValue(block, src, coerced_inst); |
| 285 | 268 | return val.toAllocatedBytes(sema.arena); |
| 286 | 269 | } |
| 287 | 270 | |
| 288 | 271 | fn resolveType(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, zir_ref: zir.Inst.Ref) !Type { |
| 289 | | const tzir_inst = sema.resolveInt(block, zir_ref); |
| 272 | const tzir_inst = try sema.resolveInst(zir_ref); |
| 290 | 273 | const wanted_type = Type.initTag(.@"type"); |
| 291 | | const coerced_inst = try sema.coerce(block, wanted_type, tzir_inst); |
| 274 | const coerced_inst = try sema.coerce(block, wanted_type, tzir_inst, src); |
| 292 | 275 | const val = try sema.resolveConstValue(block, src, coerced_inst); |
| 293 | 276 | return val.toType(sema.arena); |
| 294 | 277 | } |
| ... | ... | @@ -319,7 +302,7 @@ fn resolveAlreadyCoercedInt( |
| 319 | 302 | comptime Int: type, |
| 320 | 303 | ) !Int { |
| 321 | 304 | comptime assert(@typeInfo(Int).Int.bits <= 64); |
| 322 | | const tzir_inst = sema.resolveInst(block, zir_ref); |
| 305 | const tzir_inst = try sema.resolveInst(zir_ref); |
| 323 | 306 | const val = try sema.resolveConstValue(block, src, tzir_inst); |
| 324 | 307 | switch (@typeInfo(Int).Int.signedness) { |
| 325 | 308 | .signed => return @intCast(Int, val.toSignedInt()), |
| ... | ... | @@ -334,8 +317,8 @@ fn resolveInt( |
| 334 | 317 | zir_ref: zir.Inst.Ref, |
| 335 | 318 | dest_type: Type, |
| 336 | 319 | ) !u64 { |
| 337 | | const tzir_inst = sema.resolveInst(block, zir_ref); |
| 338 | | const coerced = try sema.coerce(scope, dest_type, tzir_inst); |
| 320 | const tzir_inst = try sema.resolveInst(zir_ref); |
| 321 | const coerced = try sema.coerce(block, dest_type, tzir_inst, src); |
| 339 | 322 | const val = try sema.resolveConstValue(block, src, coerced); |
| 340 | 323 | |
| 341 | 324 | return val.toUnsignedInt(); |
| ... | ... | @@ -347,7 +330,7 @@ fn resolveInstConst( |
| 347 | 330 | src: LazySrcLoc, |
| 348 | 331 | zir_ref: zir.Inst.Ref, |
| 349 | 332 | ) InnerError!TypedValue { |
| 350 | | const tzir_inst = sema.resolveInst(block, zir_ref); |
| 333 | const tzir_inst = try sema.resolveInst(zir_ref); |
| 351 | 334 | const val = try sema.resolveConstValue(block, src, tzir_inst); |
| 352 | 335 | return TypedValue{ |
| 353 | 336 | .ty = tzir_inst.ty, |
| ... | ... | @@ -355,42 +338,46 @@ fn resolveInstConst( |
| 355 | 338 | }; |
| 356 | 339 | } |
| 357 | 340 | |
| 358 | | fn zirConst(sema: *Sema, block: *Scope.Block, const_inst: zir.Inst.Index) InnerError!*Inst { |
| 341 | fn zirConst(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 359 | 342 | const tracy = trace(@src()); |
| 360 | 343 | defer tracy.end(); |
| 344 | |
| 345 | const tv_ptr = sema.code.instructions.items(.data)[inst].@"const"; |
| 361 | 346 | // Move the TypedValue from old memory to new memory. This allows freeing the ZIR instructions |
| 362 | | // after analysis. |
| 363 | | const typed_value_copy = try const_inst.positionals.typed_value.copy(sema.arena); |
| 364 | | return sema.mod.constInst(scope, const_inst.base.src, typed_value_copy); |
| 347 | // after analysis. This happens, for example, with variable declaration initialization |
| 348 | // expressions. |
| 349 | const typed_value_copy = try tv_ptr.copy(sema.arena); |
| 350 | return sema.mod.constInst(sema.arena, .unneeded, typed_value_copy); |
| 365 | 351 | } |
| 366 | 352 | |
| 367 | 353 | fn zirBitcastRef(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 368 | 354 | const tracy = trace(@src()); |
| 369 | 355 | defer tracy.end(); |
| 370 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement zir_sema.zirBitcastRef", .{}); |
| 356 | return sema.mod.fail(&block.base, sema.src, "TODO implement zir_sema.zirBitcastRef", .{}); |
| 371 | 357 | } |
| 372 | 358 | |
| 373 | 359 | fn zirBitcastResultPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 374 | 360 | const tracy = trace(@src()); |
| 375 | 361 | defer tracy.end(); |
| 376 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement zir_sema.zirBitcastResultPtr", .{}); |
| 362 | return sema.mod.fail(&block.base, sema.src, "TODO implement zir_sema.zirBitcastResultPtr", .{}); |
| 377 | 363 | } |
| 378 | 364 | |
| 379 | 365 | fn zirCoerceResultPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 380 | 366 | const tracy = trace(@src()); |
| 381 | 367 | defer tracy.end(); |
| 382 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement zirCoerceResultPtr", .{}); |
| 368 | return sema.mod.fail(&block.base, sema.src, "TODO implement zirCoerceResultPtr", .{}); |
| 383 | 369 | } |
| 384 | 370 | |
| 385 | 371 | fn zirRetPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 386 | 372 | const tracy = trace(@src()); |
| 387 | 373 | defer tracy.end(); |
| 388 | 374 | |
| 389 | | try sema.requireFunctionBlock(block, inst.base.src); |
| 390 | | const fn_ty = block.func.?.owner_decl.typed_value.most_recent.typed_value.ty; |
| 375 | const src: LazySrcLoc = .unneeded; |
| 376 | try sema.requireFunctionBlock(block, src); |
| 377 | const fn_ty = sema.func.?.owner_decl.typed_value.most_recent.typed_value.ty; |
| 391 | 378 | const ret_type = fn_ty.fnReturnType(); |
| 392 | 379 | const ptr_type = try sema.mod.simplePtrType(sema.arena, ret_type, true, .One); |
| 393 | | return block.addNoOp(inst.base.src, ptr_type, .alloc); |
| 380 | return block.addNoOp(src, ptr_type, .alloc); |
| 394 | 381 | } |
| 395 | 382 | |
| 396 | 383 | fn zirRef(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -398,17 +385,19 @@ fn zirRef(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*In |
| 398 | 385 | defer tracy.end(); |
| 399 | 386 | |
| 400 | 387 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 401 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 388 | const operand = try sema.resolveInst(inst_data.operand); |
| 402 | 389 | return sema.analyzeRef(block, inst_data.src(), operand); |
| 403 | 390 | } |
| 404 | 391 | |
| 405 | 392 | fn zirRetType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 406 | 393 | const tracy = trace(@src()); |
| 407 | 394 | defer tracy.end(); |
| 408 | | try sema.requireFunctionBlock(block, inst.base.src); |
| 409 | | const fn_ty = b.func.?.owner_decl.typed_value.most_recent.typed_value.ty; |
| 395 | |
| 396 | const src: LazySrcLoc = .unneeded; |
| 397 | try sema.requireFunctionBlock(block, src); |
| 398 | const fn_ty = sema.func.?.owner_decl.typed_value.most_recent.typed_value.ty; |
| 410 | 399 | const ret_type = fn_ty.fnReturnType(); |
| 411 | | return sema.mod.constType(sema.arena, inst.base.src, ret_type); |
| 400 | return sema.mod.constType(sema.arena, src, ret_type); |
| 412 | 401 | } |
| 413 | 402 | |
| 414 | 403 | fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -416,7 +405,7 @@ fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) I |
| 416 | 405 | defer tracy.end(); |
| 417 | 406 | |
| 418 | 407 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; |
| 419 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 408 | const operand = try sema.resolveInst(inst_data.operand); |
| 420 | 409 | const src = inst_data.src(); |
| 421 | 410 | switch (operand.ty.zigTypeTag()) { |
| 422 | 411 | .Void, .NoReturn => return sema.mod.constVoid(sema.arena, .unneeded), |
| ... | ... | @@ -429,7 +418,7 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Inde |
| 429 | 418 | defer tracy.end(); |
| 430 | 419 | |
| 431 | 420 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; |
| 432 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 421 | const operand = try sema.resolveInst(inst_data.operand); |
| 433 | 422 | const src = inst_data.src(); |
| 434 | 423 | switch (operand.ty.zigTypeTag()) { |
| 435 | 424 | .ErrorSet, .ErrorUnion => return sema.mod.fail(&block.base, src, "error is discarded", .{}), |
| ... | ... | @@ -442,7 +431,8 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In |
| 442 | 431 | defer tracy.end(); |
| 443 | 432 | |
| 444 | 433 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; |
| 445 | | const array_ptr = sema.resolveInst(block, inst_data.operand); |
| 434 | const src = inst_data.src(); |
| 435 | const array_ptr = try sema.resolveInst(inst_data.operand); |
| 446 | 436 | |
| 447 | 437 | const elem_ty = array_ptr.ty.elemType(); |
| 448 | 438 | if (!elem_ty.isIndexable()) { |
| ... | ... | @@ -454,7 +444,7 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In |
| 454 | 444 | "type '{}' does not support indexing", |
| 455 | 445 | .{elem_ty}, |
| 456 | 446 | ); |
| 457 | | errdefer msg.destroy(mod.gpa); |
| 447 | errdefer msg.destroy(sema.gpa); |
| 458 | 448 | try sema.mod.errNote( |
| 459 | 449 | &block.base, |
| 460 | 450 | cond_src, |
| ... | ... | @@ -464,10 +454,10 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In |
| 464 | 454 | ); |
| 465 | 455 | break :msg msg; |
| 466 | 456 | }; |
| 467 | | return mod.failWithOwnedErrorMsg(scope, msg); |
| 457 | return sema.mod.failWithOwnedErrorMsg(&block.base, msg); |
| 468 | 458 | } |
| 469 | | const result_ptr = try sema.namedFieldPtr(block, inst.base.src, array_ptr, "len", inst.base.src); |
| 470 | | return sema.analyzeDeref(block, inst.base.src, result_ptr, result_ptr.src); |
| 459 | const result_ptr = try sema.namedFieldPtr(block, src, array_ptr, "len", src); |
| 460 | return sema.analyzeDeref(block, src, result_ptr, result_ptr.src); |
| 471 | 461 | } |
| 472 | 462 | |
| 473 | 463 | fn zirAlloc(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -505,6 +495,10 @@ fn zirAllocInferred( |
| 505 | 495 | ) InnerError!*Inst { |
| 506 | 496 | const tracy = trace(@src()); |
| 507 | 497 | defer tracy.end(); |
| 498 | |
| 499 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; |
| 500 | const src = inst_data.src(); |
| 501 | |
| 508 | 502 | const val_payload = try sema.arena.create(Value.Payload.InferredAlloc); |
| 509 | 503 | val_payload.* = .{ |
| 510 | 504 | .data = .{}, |
| ... | ... | @@ -513,11 +507,11 @@ fn zirAllocInferred( |
| 513 | 507 | // not needed in the case of constant values. However here, we plan to "downgrade" |
| 514 | 508 | // to a normal instruction when we hit `resolve_inferred_alloc`. So we append |
| 515 | 509 | // to the block even though it is currently a `.constant`. |
| 516 | | const result = try sema.mod.constInst(scope, inst.base.src, .{ |
| 510 | const result = try sema.mod.constInst(sema.arena, src, .{ |
| 517 | 511 | .ty = inferred_alloc_ty, |
| 518 | 512 | .val = Value.initPayload(&val_payload.base), |
| 519 | 513 | }); |
| 520 | | try sema.requireFunctionBlock(block, inst.base.src); |
| 514 | try sema.requireFunctionBlock(block, src); |
| 521 | 515 | try block.instructions.append(sema.gpa, result); |
| 522 | 516 | return result; |
| 523 | 517 | } |
| ... | ... | @@ -532,7 +526,7 @@ fn zirResolveInferredAlloc( |
| 532 | 526 | |
| 533 | 527 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; |
| 534 | 528 | const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node }; |
| 535 | | const ptr = sema.resolveInst(block, inst_data.operand); |
| 529 | const ptr = try sema.resolveInst(inst_data.operand); |
| 536 | 530 | const ptr_val = ptr.castTag(.constant).?.val; |
| 537 | 531 | const inferred_alloc = ptr_val.castTag(.inferred_alloc).?; |
| 538 | 532 | const peer_inst_list = inferred_alloc.data.stored_inst_list.items; |
| ... | ... | @@ -563,14 +557,15 @@ fn zirStoreToBlockPtr( |
| 563 | 557 | defer tracy.end(); |
| 564 | 558 | |
| 565 | 559 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 566 | | const ptr = sema.resolveInst(bin_inst.lhs); |
| 567 | | const value = sema.resolveInst(bin_inst.rhs); |
| 560 | const ptr = try sema.resolveInst(bin_inst.lhs); |
| 561 | const value = try sema.resolveInst(bin_inst.rhs); |
| 568 | 562 | const ptr_ty = try sema.mod.simplePtrType(sema.arena, value.ty, true, .One); |
| 569 | 563 | // TODO detect when this store should be done at compile-time. For example, |
| 570 | 564 | // if expressions should force it when the condition is compile-time known. |
| 565 | const src: LazySrcLoc = .unneeded; |
| 571 | 566 | try sema.requireRuntimeBlock(block, src); |
| 572 | | const bitcasted_ptr = try block.addUnOp(inst.base.src, ptr_ty, .bitcast, ptr); |
| 573 | | return mod.storePtr(scope, inst.base.src, bitcasted_ptr, value); |
| 567 | const bitcasted_ptr = try block.addUnOp(src, ptr_ty, .bitcast, ptr); |
| 568 | return sema.storePtr(block, src, bitcasted_ptr, value); |
| 574 | 569 | } |
| 575 | 570 | |
| 576 | 571 | fn zirStoreToInferredPtr( |
| ... | ... | @@ -581,9 +576,10 @@ fn zirStoreToInferredPtr( |
| 581 | 576 | const tracy = trace(@src()); |
| 582 | 577 | defer tracy.end(); |
| 583 | 578 | |
| 579 | const src: LazySrcLoc = .unneeded; |
| 584 | 580 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 585 | | const ptr = sema.resolveInst(bin_inst.lhs); |
| 586 | | const value = sema.resolveInst(bin_inst.rhs); |
| 581 | const ptr = try sema.resolveInst(bin_inst.lhs); |
| 582 | const value = try sema.resolveInst(bin_inst.rhs); |
| 587 | 583 | const inferred_alloc = ptr.castTag(.constant).?.val.castTag(.inferred_alloc).?; |
| 588 | 584 | // Add the stored instruction to the set we will use to resolve peer types |
| 589 | 585 | // for the inferred allocation. |
| ... | ... | @@ -591,8 +587,8 @@ fn zirStoreToInferredPtr( |
| 591 | 587 | // Create a runtime bitcast instruction with exactly the type the pointer wants. |
| 592 | 588 | const ptr_ty = try sema.mod.simplePtrType(sema.arena, value.ty, true, .One); |
| 593 | 589 | try sema.requireRuntimeBlock(block, src); |
| 594 | | const bitcasted_ptr = try block.addUnOp(inst.base.src, ptr_ty, .bitcast, ptr); |
| 595 | | return mod.storePtr(scope, inst.base.src, bitcasted_ptr, value); |
| 590 | const bitcasted_ptr = try block.addUnOp(src, ptr_ty, .bitcast, ptr); |
| 591 | return sema.storePtr(block, src, bitcasted_ptr, value); |
| 596 | 592 | } |
| 597 | 593 | |
| 598 | 594 | fn zirSetEvalBranchQuota( |
| ... | ... | @@ -614,17 +610,18 @@ fn zirStore(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!* |
| 614 | 610 | defer tracy.end(); |
| 615 | 611 | |
| 616 | 612 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 617 | | const ptr = sema.resolveInst(bin_inst.lhs); |
| 618 | | const value = sema.resolveInst(bin_inst.rhs); |
| 619 | | return mod.storePtr(scope, inst.base.src, ptr, value); |
| 613 | const ptr = try sema.resolveInst(bin_inst.lhs); |
| 614 | const value = try sema.resolveInst(bin_inst.rhs); |
| 615 | return sema.storePtr(block, .unneeded, ptr, value); |
| 620 | 616 | } |
| 621 | 617 | |
| 622 | 618 | fn zirParamType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 623 | 619 | const tracy = trace(@src()); |
| 624 | 620 | defer tracy.end(); |
| 625 | 621 | |
| 622 | const src: LazySrcLoc = .todo; |
| 626 | 623 | const inst_data = sema.code.instructions.items(.data)[inst].param_type; |
| 627 | | const fn_inst = sema.resolveInst(inst_data.callee); |
| 624 | const fn_inst = try sema.resolveInst(inst_data.callee); |
| 628 | 625 | const param_index = inst_data.param_index; |
| 629 | 626 | |
| 630 | 627 | const fn_ty: Type = switch (fn_inst.ty.zigTypeTag()) { |
| ... | ... | @@ -640,9 +637,9 @@ fn zirParamType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr |
| 640 | 637 | const param_count = fn_ty.fnParamLen(); |
| 641 | 638 | if (param_index >= param_count) { |
| 642 | 639 | if (fn_ty.fnIsVarArgs()) { |
| 643 | | return sema.mod.constType(sema.arena, inst.base.src, Type.initTag(.var_args_param)); |
| 640 | return sema.mod.constType(sema.arena, src, Type.initTag(.var_args_param)); |
| 644 | 641 | } |
| 645 | | return sema.mod.fail(&block.base, inst.base.src, "arg index {d} out of bounds; '{}' has {d} argument(s)", .{ |
| 642 | return sema.mod.fail(&block.base, src, "arg index {d} out of bounds; '{}' has {d} argument(s)", .{ |
| 646 | 643 | param_index, |
| 647 | 644 | fn_ty, |
| 648 | 645 | param_count, |
| ... | ... | @@ -651,20 +648,25 @@ fn zirParamType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr |
| 651 | 648 | |
| 652 | 649 | // TODO support generic functions |
| 653 | 650 | const param_type = fn_ty.fnParamType(param_index); |
| 654 | | return sema.mod.constType(sema.arena, inst.base.src, param_type); |
| 651 | return sema.mod.constType(sema.arena, src, param_type); |
| 655 | 652 | } |
| 656 | 653 | |
| 657 | | fn zirStr(sema: *Sema, block: *Scope.Block, str_inst: zir.Inst.Index) InnerError!*Inst { |
| 654 | fn zirStr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 658 | 655 | const tracy = trace(@src()); |
| 659 | 656 | defer tracy.end(); |
| 660 | 657 | |
| 661 | | // The bytes references memory inside the ZIR module, which is fine. Multiple |
| 662 | | // anonymous Decls may have strings which point to within the same ZIR module. |
| 663 | | const bytes = sema.code.instructions.items(.data)[inst].str.get(sema.code); |
| 658 | const zir_bytes = sema.code.instructions.items(.data)[inst].str.get(sema.code); |
| 659 | |
| 660 | // `zir_bytes` references memory inside the ZIR module, which can get deallocated |
| 661 | // after semantic analysis is complete, for example in the case of the initialization |
| 662 | // expression of a variable declaration. We need the memory to be in the new |
| 663 | // anonymous Decl's arena. |
| 664 | 664 | |
| 665 | 665 | var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa); |
| 666 | 666 | errdefer new_decl_arena.deinit(); |
| 667 | 667 | |
| 668 | const bytes = try new_decl_arena.allocator.dupe(u8, zir_bytes); |
| 669 | |
| 668 | 670 | const decl_ty = try Type.Tag.array_u8_sentinel_0.create(&new_decl_arena.allocator, bytes.len); |
| 669 | 671 | const decl_val = try Value.Tag.bytes.create(&new_decl_arena.allocator, bytes); |
| 670 | 672 | |
| ... | ... | @@ -679,7 +681,8 @@ fn zirInt(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*In |
| 679 | 681 | const tracy = trace(@src()); |
| 680 | 682 | defer tracy.end(); |
| 681 | 683 | |
| 682 | | return mod.constIntBig(scope, inst.base.src, Type.initTag(.comptime_int), inst.positionals.int); |
| 684 | const int = sema.code.instructions.items(.data)[inst].int; |
| 685 | return sema.mod.constIntUnsigned(sema.arena, .unneeded, Type.initTag(.comptime_int), int); |
| 683 | 686 | } |
| 684 | 687 | |
| 685 | 688 | fn zirCompileError(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -694,8 +697,8 @@ fn zirCompileError(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner |
| 694 | 697 | } |
| 695 | 698 | |
| 696 | 699 | fn zirCompileLog(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 697 | | var managed = mod.compile_log_text.toManaged(mod.gpa); |
| 698 | | defer mod.compile_log_text = managed.moveToUnmanaged(); |
| 700 | var managed = sema.mod.compile_log_text.toManaged(sema.gpa); |
| 701 | defer sema.mod.compile_log_text = managed.moveToUnmanaged(); |
| 699 | 702 | const writer = managed.writer(); |
| 700 | 703 | |
| 701 | 704 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| ... | ... | @@ -703,7 +706,7 @@ fn zirCompileLog(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr |
| 703 | 706 | for (sema.code.extra[extra.end..][0..extra.data.operands_len]) |arg_ref, i| { |
| 704 | 707 | if (i != 0) try writer.print(", ", .{}); |
| 705 | 708 | |
| 706 | | const arg = sema.resolveInst(block, arg_ref); |
| 709 | const arg = try sema.resolveInst(arg_ref); |
| 707 | 710 | if (arg.value()) |val| { |
| 708 | 711 | try writer.print("@as({}, {})", .{ arg.ty, val }); |
| 709 | 712 | } else { |
| ... | ... | @@ -712,12 +715,9 @@ fn zirCompileLog(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr |
| 712 | 715 | } |
| 713 | 716 | try writer.print("\n", .{}); |
| 714 | 717 | |
| 715 | | const gop = try mod.compile_log_decls.getOrPut(mod.gpa, scope.ownerDecl().?); |
| 718 | const gop = try sema.mod.compile_log_decls.getOrPut(sema.gpa, sema.owner_decl); |
| 716 | 719 | if (!gop.found_existing) { |
| 717 | | gop.entry.value = .{ |
| 718 | | .file_scope = block.getFileScope(), |
| 719 | | .lazy = inst_data.src(), |
| 720 | | }; |
| 720 | gop.entry.value = inst_data.src().toSrcLoc(&block.base); |
| 721 | 721 | } |
| 722 | 722 | return sema.mod.constVoid(sema.arena, .unneeded); |
| 723 | 723 | } |
| ... | ... | @@ -726,6 +726,11 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerE |
| 726 | 726 | const tracy = trace(@src()); |
| 727 | 727 | defer tracy.end(); |
| 728 | 728 | |
| 729 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 730 | const src = inst_data.src(); |
| 731 | const extra = sema.code.extraData(zir.Inst.MultiOp, inst_data.payload_index); |
| 732 | const body = sema.code.extra[extra.end..][0..extra.data.operands_len]; |
| 733 | |
| 729 | 734 | // Reserve space for a Loop instruction so that generated Break instructions can |
| 730 | 735 | // point to it, even if it doesn't end up getting used because the code ends up being |
| 731 | 736 | // comptime evaluated. |
| ... | ... | @@ -734,52 +739,57 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerE |
| 734 | 739 | .base = .{ |
| 735 | 740 | .tag = Inst.Loop.base_tag, |
| 736 | 741 | .ty = Type.initTag(.noreturn), |
| 737 | | .src = inst.base.src, |
| 742 | .src = src, |
| 738 | 743 | }, |
| 739 | 744 | .body = undefined, |
| 740 | 745 | }; |
| 741 | 746 | |
| 742 | 747 | var child_block: Scope.Block = .{ |
| 743 | 748 | .parent = parent_block, |
| 744 | | .inst_table = parent_block.inst_table, |
| 745 | | .func = parent_block.func, |
| 746 | | .owner_decl = parent_block.owner_decl, |
| 749 | .sema = sema, |
| 747 | 750 | .src_decl = parent_block.src_decl, |
| 748 | 751 | .instructions = .{}, |
| 749 | | .arena = sema.arena, |
| 750 | 752 | .inlining = parent_block.inlining, |
| 751 | 753 | .is_comptime = parent_block.is_comptime, |
| 752 | | .branch_quota = parent_block.branch_quota, |
| 753 | 754 | }; |
| 754 | | defer child_block.instructions.deinit(mod.gpa); |
| 755 | defer child_block.instructions.deinit(sema.gpa); |
| 755 | 756 | |
| 756 | | try sema.analyzeBody(&child_block, inst.positionals.body); |
| 757 | try sema.analyzeBody(&child_block, body); |
| 757 | 758 | |
| 758 | 759 | // Loop repetition is implied so the last instruction may or may not be a noreturn instruction. |
| 759 | 760 | |
| 760 | | try parent_block.instructions.append(mod.gpa, &loop_inst.base); |
| 761 | try parent_block.instructions.append(sema.gpa, &loop_inst.base); |
| 761 | 762 | loop_inst.body = .{ .instructions = try sema.arena.dupe(*Inst, child_block.instructions.items) }; |
| 762 | 763 | return &loop_inst.base; |
| 763 | 764 | } |
| 764 | 765 | |
| 765 | | fn zirBlockFlat(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index, is_comptime: bool) InnerError!*Inst { |
| 766 | fn zirBlockFlat( |
| 767 | sema: *Sema, |
| 768 | parent_block: *Scope.Block, |
| 769 | inst: zir.Inst.Index, |
| 770 | is_comptime: bool, |
| 771 | ) InnerError!*Inst { |
| 766 | 772 | const tracy = trace(@src()); |
| 767 | 773 | defer tracy.end(); |
| 768 | 774 | |
| 775 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 776 | const src = inst_data.src(); |
| 777 | const extra = sema.code.extraData(zir.Inst.MultiOp, inst_data.payload_index); |
| 778 | const body = sema.code.extra[extra.end..][0..extra.data.operands_len]; |
| 779 | |
| 769 | 780 | var child_block = parent_block.makeSubBlock(); |
| 770 | | defer child_block.instructions.deinit(mod.gpa); |
| 781 | defer child_block.instructions.deinit(sema.gpa); |
| 771 | 782 | child_block.is_comptime = child_block.is_comptime or is_comptime; |
| 772 | 783 | |
| 773 | | try sema.analyzeBody(&child_block, inst.positionals.body); |
| 784 | try sema.analyzeBody(&child_block, body); |
| 774 | 785 | |
| 775 | 786 | // Move the analyzed instructions into the parent block arena. |
| 776 | 787 | const copied_instructions = try sema.arena.dupe(*Inst, child_block.instructions.items); |
| 777 | | try parent_block.instructions.appendSlice(mod.gpa, copied_instructions); |
| 788 | try parent_block.instructions.appendSlice(sema.gpa, copied_instructions); |
| 778 | 789 | |
| 779 | 790 | // The result of a flat block is the last instruction. |
| 780 | | const zir_inst_list = inst.positionals.body.instructions; |
| 781 | | const last_zir_inst = zir_inst_list[zir_inst_list.len - 1]; |
| 782 | | return sema.inst_map[last_zir_inst]; |
| 791 | const last_zir_inst = body[body.len - 1]; |
| 792 | return sema.resolveInst(last_zir_inst); |
| 783 | 793 | } |
| 784 | 794 | |
| 785 | 795 | fn zirBlock( |
| ... | ... | @@ -791,6 +801,11 @@ fn zirBlock( |
| 791 | 801 | const tracy = trace(@src()); |
| 792 | 802 | defer tracy.end(); |
| 793 | 803 | |
| 804 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 805 | const src = inst_data.src(); |
| 806 | const extra = sema.code.extraData(zir.Inst.MultiOp, inst_data.payload_index); |
| 807 | const body = sema.code.extra[extra.end..][0..extra.data.operands_len]; |
| 808 | |
| 794 | 809 | // Reserve space for a Block instruction so that generated Break instructions can |
| 795 | 810 | // point to it, even if it doesn't end up getting used because the code ends up being |
| 796 | 811 | // comptime evaluated. |
| ... | ... | @@ -799,19 +814,16 @@ fn zirBlock( |
| 799 | 814 | .base = .{ |
| 800 | 815 | .tag = Inst.Block.base_tag, |
| 801 | 816 | .ty = undefined, // Set after analysis. |
| 802 | | .src = inst.base.src, |
| 817 | .src = src, |
| 803 | 818 | }, |
| 804 | 819 | .body = undefined, |
| 805 | 820 | }; |
| 806 | 821 | |
| 807 | 822 | var child_block: Scope.Block = .{ |
| 808 | 823 | .parent = parent_block, |
| 809 | | .inst_table = parent_block.inst_table, |
| 810 | | .func = parent_block.func, |
| 811 | | .owner_decl = parent_block.owner_decl, |
| 824 | .sema = sema, |
| 812 | 825 | .src_decl = parent_block.src_decl, |
| 813 | 826 | .instructions = .{}, |
| 814 | | .arena = sema.arena, |
| 815 | 827 | // TODO @as here is working around a stage1 miscompilation bug :( |
| 816 | 828 | .label = @as(?Scope.Block.Label, Scope.Block.Label{ |
| 817 | 829 | .zir_block = inst, |
| ... | ... | @@ -823,17 +835,16 @@ fn zirBlock( |
| 823 | 835 | }), |
| 824 | 836 | .inlining = parent_block.inlining, |
| 825 | 837 | .is_comptime = is_comptime or parent_block.is_comptime, |
| 826 | | .branch_quota = parent_block.branch_quota, |
| 827 | 838 | }; |
| 828 | 839 | const merges = &child_block.label.?.merges; |
| 829 | 840 | |
| 830 | | defer child_block.instructions.deinit(mod.gpa); |
| 831 | | defer merges.results.deinit(mod.gpa); |
| 832 | | defer merges.br_list.deinit(mod.gpa); |
| 841 | defer child_block.instructions.deinit(sema.gpa); |
| 842 | defer merges.results.deinit(sema.gpa); |
| 843 | defer merges.br_list.deinit(sema.gpa); |
| 833 | 844 | |
| 834 | | try sema.analyzeBody(&child_block, inst.positionals.body); |
| 845 | try sema.analyzeBody(&child_block, body); |
| 835 | 846 | |
| 836 | | return analyzeBlockBody(mod, scope, &child_block, merges); |
| 847 | return sema.analyzeBlockBody(parent_block, &child_block, merges); |
| 837 | 848 | } |
| 838 | 849 | |
| 839 | 850 | fn analyzeBlockBody( |
| ... | ... | @@ -853,7 +864,7 @@ fn analyzeBlockBody( |
| 853 | 864 | // No need for a block instruction. We can put the new instructions |
| 854 | 865 | // directly into the parent block. |
| 855 | 866 | const copied_instructions = try sema.arena.dupe(*Inst, child_block.instructions.items); |
| 856 | | try parent_block.instructions.appendSlice(mod.gpa, copied_instructions); |
| 867 | try parent_block.instructions.appendSlice(sema.gpa, copied_instructions); |
| 857 | 868 | return copied_instructions[copied_instructions.len - 1]; |
| 858 | 869 | } |
| 859 | 870 | if (merges.results.items.len == 1) { |
| ... | ... | @@ -864,7 +875,7 @@ fn analyzeBlockBody( |
| 864 | 875 | // No need for a block instruction. We can put the new instructions directly |
| 865 | 876 | // into the parent block. Here we omit the break instruction. |
| 866 | 877 | const copied_instructions = try sema.arena.dupe(*Inst, child_block.instructions.items[0..last_inst_index]); |
| 867 | | try parent_block.instructions.appendSlice(mod.gpa, copied_instructions); |
| 878 | try parent_block.instructions.appendSlice(sema.gpa, copied_instructions); |
| 868 | 879 | return merges.results.items[0]; |
| 869 | 880 | } |
| 870 | 881 | } |
| ... | ... | @@ -874,7 +885,7 @@ fn analyzeBlockBody( |
| 874 | 885 | |
| 875 | 886 | // Need to set the type and emit the Block instruction. This allows machine code generation |
| 876 | 887 | // to emit a jump instruction to after the block when it encounters the break. |
| 877 | | try parent_block.instructions.append(mod.gpa, &merges.block_inst.base); |
| 888 | try parent_block.instructions.append(sema.gpa, &merges.block_inst.base); |
| 878 | 889 | const resolved_ty = try sema.resolvePeerTypes(parent_block, merges.results.items); |
| 879 | 890 | merges.block_inst.base.ty = resolved_ty; |
| 880 | 891 | merges.block_inst.body = .{ |
| ... | ... | @@ -888,8 +899,8 @@ fn analyzeBlockBody( |
| 888 | 899 | continue; |
| 889 | 900 | } |
| 890 | 901 | var coerce_block = parent_block.makeSubBlock(); |
| 891 | | defer coerce_block.instructions.deinit(mod.gpa); |
| 892 | | const coerced_operand = try sema.coerce(&coerce_block.base, resolved_ty, br.operand); |
| 902 | defer coerce_block.instructions.deinit(sema.gpa); |
| 903 | const coerced_operand = try sema.coerce(&coerce_block, resolved_ty, br.operand, .todo); |
| 893 | 904 | // If no instructions were produced, such as in the case of a coercion of a |
| 894 | 905 | // constant value to a new type, we can simply point the br operand to it. |
| 895 | 906 | if (coerce_block.instructions.items.len == 0) { |
| ... | ... | @@ -921,8 +932,10 @@ fn zirBreakpoint(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr |
| 921 | 932 | const tracy = trace(@src()); |
| 922 | 933 | defer tracy.end(); |
| 923 | 934 | |
| 935 | const src_node = sema.code.instructions.items(.data)[inst].node; |
| 936 | const src: LazySrcLoc = .{ .node_offset = src_node }; |
| 924 | 937 | try sema.requireRuntimeBlock(block, src); |
| 925 | | return block.addNoOp(inst.base.src, Type.initTag(.void), .breakpoint); |
| 938 | return block.addNoOp(src, Type.initTag(.void), .breakpoint); |
| 926 | 939 | } |
| 927 | 940 | |
| 928 | 941 | fn zirBreak(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -930,9 +943,9 @@ fn zirBreak(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!* |
| 930 | 943 | defer tracy.end(); |
| 931 | 944 | |
| 932 | 945 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 933 | | const operand = sema.resolveInst(block, bin_inst.rhs); |
| 946 | const operand = try sema.resolveInst(bin_inst.rhs); |
| 934 | 947 | const zir_block = bin_inst.lhs; |
| 935 | | return analyzeBreak(mod, block, sema.src, zir_block, operand); |
| 948 | return sema.analyzeBreak(block, sema.src, zir_block, operand); |
| 936 | 949 | } |
| 937 | 950 | |
| 938 | 951 | fn zirBreakVoidTok(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -942,25 +955,25 @@ fn zirBreakVoidTok(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner |
| 942 | 955 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 943 | 956 | const zir_block = inst_data.operand; |
| 944 | 957 | const void_inst = try sema.mod.constVoid(sema.arena, .unneeded); |
| 945 | | return analyzeBreak(mod, block, inst_data.src(), zir_block, void_inst); |
| 958 | return sema.analyzeBreak(block, inst_data.src(), zir_block, void_inst); |
| 946 | 959 | } |
| 947 | 960 | |
| 948 | 961 | fn analyzeBreak( |
| 949 | 962 | sema: *Sema, |
| 950 | | block: *Scope.Block, |
| 963 | start_block: *Scope.Block, |
| 951 | 964 | src: LazySrcLoc, |
| 952 | 965 | zir_block: zir.Inst.Index, |
| 953 | 966 | operand: *Inst, |
| 954 | 967 | ) InnerError!*Inst { |
| 955 | | var opt_block = scope.cast(Scope.Block); |
| 956 | | while (opt_block) |block| { |
| 968 | var block = start_block; |
| 969 | while (true) { |
| 957 | 970 | if (block.label) |*label| { |
| 958 | 971 | if (label.zir_block == zir_block) { |
| 959 | 972 | try sema.requireFunctionBlock(block, src); |
| 960 | 973 | // Here we add a br instruction, but we over-allocate a little bit |
| 961 | 974 | // (if necessary) to make it possible to convert the instruction into |
| 962 | 975 | // a br_block_flat instruction later. |
| 963 | | const br = @ptrCast(*Inst.Br, try b.arena.alignedAlloc( |
| 976 | const br = @ptrCast(*Inst.Br, try sema.arena.alignedAlloc( |
| 964 | 977 | u8, |
| 965 | 978 | Inst.convertable_br_align, |
| 966 | 979 | Inst.convertable_br_size, |
| ... | ... | @@ -974,21 +987,21 @@ fn analyzeBreak( |
| 974 | 987 | .operand = operand, |
| 975 | 988 | .block = label.merges.block_inst, |
| 976 | 989 | }; |
| 977 | | try b.instructions.append(mod.gpa, &br.base); |
| 978 | | try label.merges.results.append(mod.gpa, operand); |
| 979 | | try label.merges.br_list.append(mod.gpa, br); |
| 990 | try block.instructions.append(sema.gpa, &br.base); |
| 991 | try label.merges.results.append(sema.gpa, operand); |
| 992 | try label.merges.br_list.append(sema.gpa, br); |
| 980 | 993 | return &br.base; |
| 981 | 994 | } |
| 982 | 995 | } |
| 983 | | opt_block = block.parent; |
| 984 | | } else unreachable; |
| 996 | block = block.parent.?; |
| 997 | } |
| 985 | 998 | } |
| 986 | 999 | |
| 987 | 1000 | fn zirDbgStmtNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 988 | 1001 | const tracy = trace(@src()); |
| 989 | 1002 | defer tracy.end(); |
| 990 | 1003 | |
| 991 | | if (b.is_comptime) { |
| 1004 | if (block.is_comptime) { |
| 992 | 1005 | return sema.mod.constVoid(sema.arena, .unneeded); |
| 993 | 1006 | } |
| 994 | 1007 | |
| ... | ... | @@ -1048,9 +1061,9 @@ fn analyzeCall( |
| 1048 | 1061 | func_src: LazySrcLoc, |
| 1049 | 1062 | call_src: LazySrcLoc, |
| 1050 | 1063 | modifier: std.builtin.CallOptions.Modifier, |
| 1051 | | zir_args: []const Ref, |
| 1064 | zir_args: []const zir.Inst.Ref, |
| 1052 | 1065 | ) InnerError!*ir.Inst { |
| 1053 | | const func = sema.resolveInst(zir_func); |
| 1066 | const func = try sema.resolveInst(zir_func); |
| 1054 | 1067 | |
| 1055 | 1068 | if (func.ty.zigTypeTag() != .Fn) |
| 1056 | 1069 | return sema.mod.fail(&block.base, func_src, "type '{}' not a function", .{func.ty}); |
| ... | ... | @@ -1091,20 +1104,20 @@ fn analyzeCall( |
| 1091 | 1104 | return sema.mod.fail(&block.base, call_src, "TODO implement comptime function calls", .{}); |
| 1092 | 1105 | } |
| 1093 | 1106 | if (modifier != .auto) { |
| 1094 | | return sema.mod.fail(&block.base, call_src, "TODO implement call with modifier {}", .{inst.positionals.modifier}); |
| 1107 | return sema.mod.fail(&block.base, call_src, "TODO implement call with modifier {}", .{modifier}); |
| 1095 | 1108 | } |
| 1096 | 1109 | |
| 1097 | 1110 | // TODO handle function calls of generic functions |
| 1098 | 1111 | const casted_args = try sema.arena.alloc(*Inst, zir_args.len); |
| 1099 | 1112 | for (zir_args) |zir_arg, i| { |
| 1100 | 1113 | // the args are already casted to the result of a param type instruction. |
| 1101 | | casted_args[i] = sema.resolveInst(block, zir_arg); |
| 1114 | casted_args[i] = try sema.resolveInst(zir_arg); |
| 1102 | 1115 | } |
| 1103 | 1116 | |
| 1104 | 1117 | const ret_type = func.ty.fnReturnType(); |
| 1105 | 1118 | |
| 1106 | 1119 | try sema.requireFunctionBlock(block, call_src); |
| 1107 | | const is_comptime_call = b.is_comptime or modifier == .compile_time; |
| 1120 | const is_comptime_call = block.is_comptime or modifier == .compile_time; |
| 1108 | 1121 | const is_inline_call = is_comptime_call or modifier == .always_inline or |
| 1109 | 1122 | func.ty.fnCallingConvention() == .Inline; |
| 1110 | 1123 | if (is_inline_call) { |
| ... | ... | @@ -1135,70 +1148,75 @@ fn analyzeCall( |
| 1135 | 1148 | // Otherwise we pass on the shared data from the parent scope. |
| 1136 | 1149 | var shared_inlining: Scope.Block.Inlining.Shared = .{ |
| 1137 | 1150 | .branch_count = 0, |
| 1138 | | .caller = b.func, |
| 1151 | .caller = sema.func, |
| 1139 | 1152 | }; |
| 1140 | 1153 | // This one is shared among sub-blocks within the same callee, but not |
| 1141 | 1154 | // shared among the entire inline/comptime call stack. |
| 1142 | 1155 | var inlining: Scope.Block.Inlining = .{ |
| 1143 | | .shared = if (b.inlining) |inlining| inlining.shared else &shared_inlining, |
| 1144 | | .param_index = 0, |
| 1145 | | .casted_args = casted_args, |
| 1156 | .shared = if (block.inlining) |inlining| inlining.shared else &shared_inlining, |
| 1146 | 1157 | .merges = .{ |
| 1147 | 1158 | .results = .{}, |
| 1148 | 1159 | .br_list = .{}, |
| 1149 | 1160 | .block_inst = block_inst, |
| 1150 | 1161 | }, |
| 1151 | 1162 | }; |
| 1152 | | var inst_table = Scope.Block.InstTable.init(mod.gpa); |
| 1153 | | defer inst_table.deinit(); |
| 1163 | var inline_sema: Sema = .{ |
| 1164 | .mod = sema.mod, |
| 1165 | .gpa = sema.mod.gpa, |
| 1166 | .arena = sema.arena, |
| 1167 | .code = module_fn.zir, |
| 1168 | .inst_map = try sema.gpa.alloc(*ir.Inst, module_fn.zir.instructions.len), |
| 1169 | .owner_decl = sema.owner_decl, |
| 1170 | .func = module_fn, |
| 1171 | .param_inst_list = casted_args, |
| 1172 | }; |
| 1173 | defer sema.gpa.free(inline_sema.inst_map); |
| 1154 | 1174 | |
| 1155 | 1175 | var child_block: Scope.Block = .{ |
| 1156 | 1176 | .parent = null, |
| 1157 | | .inst_table = &inst_table, |
| 1158 | | .func = module_fn, |
| 1159 | | .owner_decl = scope.ownerDecl().?, |
| 1177 | .sema = &inline_sema, |
| 1160 | 1178 | .src_decl = module_fn.owner_decl, |
| 1161 | 1179 | .instructions = .{}, |
| 1162 | | .arena = sema.arena, |
| 1163 | 1180 | .label = null, |
| 1164 | 1181 | .inlining = &inlining, |
| 1165 | 1182 | .is_comptime = is_comptime_call, |
| 1166 | | .branch_quota = b.branch_quota, |
| 1167 | 1183 | }; |
| 1168 | 1184 | |
| 1169 | 1185 | const merges = &child_block.inlining.?.merges; |
| 1170 | 1186 | |
| 1171 | | defer child_block.instructions.deinit(mod.gpa); |
| 1172 | | defer merges.results.deinit(mod.gpa); |
| 1173 | | defer merges.br_list.deinit(mod.gpa); |
| 1187 | defer child_block.instructions.deinit(sema.gpa); |
| 1188 | defer merges.results.deinit(sema.gpa); |
| 1189 | defer merges.br_list.deinit(sema.gpa); |
| 1174 | 1190 | |
| 1175 | | try mod.emitBackwardBranch(&child_block, call_src); |
| 1191 | try sema.emitBackwardBranch(&child_block, call_src); |
| 1176 | 1192 | |
| 1177 | 1193 | // This will have return instructions analyzed as break instructions to |
| 1178 | 1194 | // the block_inst above. |
| 1179 | | try sema.analyzeBody(&child_block, module_fn.zir); |
| 1195 | try sema.root(&child_block); |
| 1180 | 1196 | |
| 1181 | | return analyzeBlockBody(mod, scope, &child_block, merges); |
| 1197 | return sema.analyzeBlockBody(block, &child_block, merges); |
| 1182 | 1198 | } |
| 1183 | 1199 | |
| 1184 | 1200 | return block.addCall(call_src, ret_type, func, casted_args); |
| 1185 | 1201 | } |
| 1186 | 1202 | |
| 1187 | | fn zirIntType(sema: *Sema, block: *Scope.Block, inttype: zir.Inst.Index) InnerError!*Inst { |
| 1203 | fn zirIntType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1188 | 1204 | const tracy = trace(@src()); |
| 1189 | 1205 | defer tracy.end(); |
| 1190 | | return sema.mod.fail(&block.base, inttype.base.src, "TODO implement inttype", .{}); |
| 1206 | |
| 1207 | return sema.mod.fail(&block.base, sema.src, "TODO implement inttype", .{}); |
| 1191 | 1208 | } |
| 1192 | 1209 | |
| 1193 | | fn zirOptionalType(sema: *Sema, block: *Scope.Block, optional: zir.Inst.Index) InnerError!*Inst { |
| 1210 | fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1194 | 1211 | const tracy = trace(@src()); |
| 1195 | 1212 | defer tracy.end(); |
| 1196 | 1213 | |
| 1197 | 1214 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 1198 | | const child_type = try sema.resolveType(block, inst_data.operand); |
| 1199 | | const opt_type = try mod.optionalType(sema.arena, child_type); |
| 1215 | const src = inst_data.src(); |
| 1216 | const child_type = try sema.resolveType(block, src, inst_data.operand); |
| 1217 | const opt_type = try sema.mod.optionalType(sema.arena, child_type); |
| 1200 | 1218 | |
| 1201 | | return sema.mod.constType(sema.arena, inst_data.src(), opt_type); |
| 1219 | return sema.mod.constType(sema.arena, src, opt_type); |
| 1202 | 1220 | } |
| 1203 | 1221 | |
| 1204 | 1222 | fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -1206,32 +1224,39 @@ fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: zir.Inst.I |
| 1206 | 1224 | defer tracy.end(); |
| 1207 | 1225 | |
| 1208 | 1226 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 1209 | | const ptr = sema.resolveInst(block, inst_data.operand); |
| 1227 | const ptr = try sema.resolveInst(inst_data.operand); |
| 1210 | 1228 | const elem_ty = ptr.ty.elemType(); |
| 1211 | | const opt_ty = try mod.optionalType(sema.arena, elem_ty); |
| 1229 | const opt_ty = try sema.mod.optionalType(sema.arena, elem_ty); |
| 1212 | 1230 | |
| 1213 | 1231 | return sema.mod.constType(sema.arena, inst_data.src(), opt_ty); |
| 1214 | 1232 | } |
| 1215 | 1233 | |
| 1216 | | fn zirArrayType(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index) InnerError!*Inst { |
| 1234 | fn zirArrayType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1217 | 1235 | const tracy = trace(@src()); |
| 1218 | 1236 | defer tracy.end(); |
| 1237 | |
| 1219 | 1238 | // TODO these should be lazily evaluated |
| 1220 | | const len = try resolveInstConst(mod, scope, array.positionals.lhs); |
| 1221 | | const elem_type = try sema.resolveType(block, array.positionals.rhs); |
| 1239 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1240 | const len = try sema.resolveInstConst(block, .unneeded, bin_inst.lhs); |
| 1241 | const elem_type = try sema.resolveType(block, .unneeded, bin_inst.rhs); |
| 1242 | const array_ty = try sema.mod.arrayType(sema.arena, len.val.toUnsignedInt(), null, elem_type); |
| 1222 | 1243 | |
| 1223 | | return sema.mod.constType(sema.arena, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), null, elem_type)); |
| 1244 | return sema.mod.constType(sema.arena, .unneeded, array_ty); |
| 1224 | 1245 | } |
| 1225 | 1246 | |
| 1226 | | fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index) InnerError!*Inst { |
| 1247 | fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1227 | 1248 | const tracy = trace(@src()); |
| 1228 | 1249 | defer tracy.end(); |
| 1250 | |
| 1229 | 1251 | // TODO these should be lazily evaluated |
| 1230 | | const len = try resolveInstConst(mod, scope, array.positionals.len); |
| 1231 | | const sentinel = try resolveInstConst(mod, scope, array.positionals.sentinel); |
| 1232 | | const elem_type = try sema.resolveType(block, array.positionals.elem_type); |
| 1252 | const inst_data = sema.code.instructions.items(.data)[inst].array_type_sentinel; |
| 1253 | const len = try sema.resolveInstConst(block, .unneeded, inst_data.len); |
| 1254 | const extra = sema.code.extraData(zir.Inst.ArrayTypeSentinel, inst_data.payload_index).data; |
| 1255 | const sentinel = try sema.resolveInstConst(block, .unneeded, extra.sentinel); |
| 1256 | const elem_type = try sema.resolveType(block, .unneeded, extra.elem_type); |
| 1257 | const array_ty = try sema.mod.arrayType(sema.arena, len.val.toUnsignedInt(), sentinel.val, elem_type); |
| 1233 | 1258 | |
| 1234 | | return sema.mod.constType(sema.arena, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), sentinel.val, elem_type)); |
| 1259 | return sema.mod.constType(sema.arena, .unneeded, array_ty); |
| 1235 | 1260 | } |
| 1236 | 1261 | |
| 1237 | 1262 | fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -1239,14 +1264,15 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn |
| 1239 | 1264 | defer tracy.end(); |
| 1240 | 1265 | |
| 1241 | 1266 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1242 | | const error_union = try sema.resolveType(block, bin_inst.lhs); |
| 1243 | | const payload = try sema.resolveType(block, bin_inst.rhs); |
| 1267 | const error_union = try sema.resolveType(block, .unneeded, bin_inst.lhs); |
| 1268 | const payload = try sema.resolveType(block, .unneeded, bin_inst.rhs); |
| 1244 | 1269 | |
| 1245 | 1270 | if (error_union.zigTypeTag() != .ErrorSet) { |
| 1246 | | return sema.mod.fail(&block.base, inst.base.src, "expected error set type, found {}", .{error_union.elemType()}); |
| 1271 | return sema.mod.fail(&block.base, .todo, "expected error set type, found {}", .{error_union.elemType()}); |
| 1247 | 1272 | } |
| 1273 | const err_union_ty = try sema.mod.errorUnionType(sema.arena, error_union, payload); |
| 1248 | 1274 | |
| 1249 | | return sema.mod.constType(sema.arena, inst.base.src, try mod.errorUnionType(scope, error_union, payload)); |
| 1275 | return sema.mod.constType(sema.arena, .unneeded, err_union_ty); |
| 1250 | 1276 | } |
| 1251 | 1277 | |
| 1252 | 1278 | fn zirAnyframeType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -1266,8 +1292,10 @@ fn zirErrorSet(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro |
| 1266 | 1292 | const tracy = trace(@src()); |
| 1267 | 1293 | defer tracy.end(); |
| 1268 | 1294 | |
| 1295 | if (true) @panic("TODO update zirErrorSet in zir-memory-layout branch"); |
| 1296 | |
| 1269 | 1297 | // The owner Decl arena will store the hashmap. |
| 1270 | | var new_decl_arena = std.heap.ArenaAllocator.init(mod.gpa); |
| 1298 | var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa); |
| 1271 | 1299 | errdefer new_decl_arena.deinit(); |
| 1272 | 1300 | |
| 1273 | 1301 | const payload = try new_decl_arena.allocator.create(Value.Payload.ErrorSet); |
| ... | ... | @@ -1281,28 +1309,31 @@ fn zirErrorSet(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro |
| 1281 | 1309 | try payload.data.fields.ensureCapacity(&new_decl_arena.allocator, @intCast(u32, inst.positionals.fields.len)); |
| 1282 | 1310 | |
| 1283 | 1311 | for (inst.positionals.fields) |field_name| { |
| 1284 | | const entry = try mod.getErrorValue(field_name); |
| 1312 | const entry = try sema.mod.getErrorValue(field_name); |
| 1285 | 1313 | if (payload.data.fields.fetchPutAssumeCapacity(entry.key, {})) |_| { |
| 1286 | 1314 | return sema.mod.fail(&block.base, inst.base.src, "duplicate error: '{s}'", .{field_name}); |
| 1287 | 1315 | } |
| 1288 | 1316 | } |
| 1289 | 1317 | // TODO create name in format "error:line:column" |
| 1290 | | const new_decl = try mod.createAnonymousDecl(scope, &new_decl_arena, .{ |
| 1318 | const new_decl = try sema.mod.createAnonymousDecl(&block.base, &new_decl_arena, .{ |
| 1291 | 1319 | .ty = Type.initTag(.type), |
| 1292 | 1320 | .val = Value.initPayload(&payload.base), |
| 1293 | 1321 | }); |
| 1294 | 1322 | payload.data.decl = new_decl; |
| 1295 | | return mod.analyzeDeclVal(scope, inst.base.src, new_decl); |
| 1323 | return sema.analyzeDeclVal(block, inst.base.src, new_decl); |
| 1296 | 1324 | } |
| 1297 | 1325 | |
| 1298 | 1326 | fn zirErrorValue(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1299 | 1327 | const tracy = trace(@src()); |
| 1300 | 1328 | defer tracy.end(); |
| 1301 | 1329 | |
| 1330 | const inst_data = sema.code.instructions.items(.data)[inst].str_tok; |
| 1331 | const src = inst_data.src(); |
| 1332 | |
| 1302 | 1333 | // Create an anonymous error set type with only this error value, and return the value. |
| 1303 | | const entry = try mod.getErrorValue(inst.positionals.name); |
| 1334 | const entry = try sema.mod.getErrorValue(inst_data.get(sema.code)); |
| 1304 | 1335 | const result_type = try Type.Tag.error_set_single.create(sema.arena, entry.key); |
| 1305 | | return sema.mod.constInst(scope, inst.base.src, .{ |
| 1336 | return sema.mod.constInst(sema.arena, src, .{ |
| 1306 | 1337 | .ty = result_type, |
| 1307 | 1338 | .val = try Value.Tag.@"error".create(sema.arena, .{ |
| 1308 | 1339 | .name = entry.key, |
| ... | ... | @@ -1314,9 +1345,11 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn |
| 1314 | 1345 | const tracy = trace(@src()); |
| 1315 | 1346 | defer tracy.end(); |
| 1316 | 1347 | |
| 1348 | if (true) @panic("TODO update zirMergeErrorSets in zir-memory-layout branch"); |
| 1349 | |
| 1317 | 1350 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1318 | | const lhs_ty = try sema.resolveType(block, bin_inst.lhs); |
| 1319 | | const rhs_ty = try sema.resolveType(block, bin_inst.rhs); |
| 1351 | const lhs_ty = try sema.resolveType(block, .unneeded, bin_inst.lhs); |
| 1352 | const rhs_ty = try sema.resolveType(block, .unneeded, bin_inst.rhs); |
| 1320 | 1353 | if (rhs_ty.zigTypeTag() != .ErrorSet) |
| 1321 | 1354 | return sema.mod.fail(&block.base, inst.positionals.rhs.src, "expected error set type, found {}", .{rhs_ty}); |
| 1322 | 1355 | if (lhs_ty.zigTypeTag() != .ErrorSet) |
| ... | ... | @@ -1324,12 +1357,12 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn |
| 1324 | 1357 | |
| 1325 | 1358 | // anything merged with anyerror is anyerror |
| 1326 | 1359 | if (lhs_ty.tag() == .anyerror or rhs_ty.tag() == .anyerror) |
| 1327 | | return sema.mod.constInst(scope, inst.base.src, .{ |
| 1360 | return sema.mod.constInst(sema.arena, inst.base.src, .{ |
| 1328 | 1361 | .ty = Type.initTag(.type), |
| 1329 | 1362 | .val = Value.initTag(.anyerror_type), |
| 1330 | 1363 | }); |
| 1331 | 1364 | // The declarations arena will store the hashmap. |
| 1332 | | var new_decl_arena = std.heap.ArenaAllocator.init(mod.gpa); |
| 1365 | var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa); |
| 1333 | 1366 | errdefer new_decl_arena.deinit(); |
| 1334 | 1367 | |
| 1335 | 1368 | const payload = try new_decl_arena.allocator.create(Value.Payload.ErrorSet); |
| ... | ... | @@ -1380,21 +1413,23 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn |
| 1380 | 1413 | else => unreachable, |
| 1381 | 1414 | } |
| 1382 | 1415 | // TODO create name in format "error:line:column" |
| 1383 | | const new_decl = try mod.createAnonymousDecl(scope, &new_decl_arena, .{ |
| 1416 | const new_decl = try sema.mod.createAnonymousDecl(&block.base, &new_decl_arena, .{ |
| 1384 | 1417 | .ty = Type.initTag(.type), |
| 1385 | 1418 | .val = Value.initPayload(&payload.base), |
| 1386 | 1419 | }); |
| 1387 | 1420 | payload.data.decl = new_decl; |
| 1388 | 1421 | |
| 1389 | | return mod.analyzeDeclVal(scope, inst.base.src, new_decl); |
| 1422 | return sema.analyzeDeclVal(block, inst.base.src, new_decl); |
| 1390 | 1423 | } |
| 1391 | 1424 | |
| 1392 | | fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, zir_inst: zir.Inst.Index) InnerError!*Inst { |
| 1425 | fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1393 | 1426 | const tracy = trace(@src()); |
| 1394 | 1427 | defer tracy.end(); |
| 1395 | 1428 | |
| 1396 | | const duped_name = try sema.arena.dupe(u8, inst.positionals.name); |
| 1397 | | return sema.mod.constInst(scope, inst.base.src, .{ |
| 1429 | const inst_data = sema.code.instructions.items(.data)[inst].str_tok; |
| 1430 | const src = inst_data.src(); |
| 1431 | const duped_name = try sema.arena.dupe(u8, inst_data.get(sema.code)); |
| 1432 | return sema.mod.constInst(sema.arena, src, .{ |
| 1398 | 1433 | .ty = Type.initTag(.enum_literal), |
| 1399 | 1434 | .val = try Value.Tag.enum_literal.create(sema.arena, duped_name), |
| 1400 | 1435 | }); |
| ... | ... | @@ -1411,7 +1446,7 @@ fn zirOptionalPayloadPtr( |
| 1411 | 1446 | defer tracy.end(); |
| 1412 | 1447 | |
| 1413 | 1448 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 1414 | | const optional_ptr = sema.resolveInst(block, inst_data.operand); |
| 1449 | const optional_ptr = try sema.resolveInst(inst_data.operand); |
| 1415 | 1450 | assert(optional_ptr.ty.zigTypeTag() == .Pointer); |
| 1416 | 1451 | const src = inst_data.src(); |
| 1417 | 1452 | |
| ... | ... | @@ -1429,7 +1464,7 @@ fn zirOptionalPayloadPtr( |
| 1429 | 1464 | return sema.mod.fail(&block.base, src, "unable to unwrap null", .{}); |
| 1430 | 1465 | } |
| 1431 | 1466 | // The same Value represents the pointer to the optional and the payload. |
| 1432 | | return sema.mod.constInst(scope, src, .{ |
| 1467 | return sema.mod.constInst(sema.arena, src, .{ |
| 1433 | 1468 | .ty = child_pointer, |
| 1434 | 1469 | .val = pointer_val, |
| 1435 | 1470 | }); |
| ... | ... | @@ -1438,7 +1473,7 @@ fn zirOptionalPayloadPtr( |
| 1438 | 1473 | try sema.requireRuntimeBlock(block, src); |
| 1439 | 1474 | if (safety_check and block.wantSafety()) { |
| 1440 | 1475 | const is_non_null = try block.addUnOp(src, Type.initTag(.bool), .is_non_null_ptr, optional_ptr); |
| 1441 | | try mod.addSafetyCheck(b, is_non_null, .unwrap_null); |
| 1476 | try sema.addSafetyCheck(block, is_non_null, .unwrap_null); |
| 1442 | 1477 | } |
| 1443 | 1478 | return block.addUnOp(src, child_pointer, .optional_payload_ptr, optional_ptr); |
| 1444 | 1479 | } |
| ... | ... | @@ -1455,7 +1490,7 @@ fn zirOptionalPayload( |
| 1455 | 1490 | |
| 1456 | 1491 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 1457 | 1492 | const src = inst_data.src(); |
| 1458 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 1493 | const operand = try sema.resolveInst(inst_data.operand); |
| 1459 | 1494 | const opt_type = operand.ty; |
| 1460 | 1495 | if (opt_type.zigTypeTag() != .Optional) { |
| 1461 | 1496 | return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type}); |
| ... | ... | @@ -1467,7 +1502,7 @@ fn zirOptionalPayload( |
| 1467 | 1502 | if (val.isNull()) { |
| 1468 | 1503 | return sema.mod.fail(&block.base, src, "unable to unwrap null", .{}); |
| 1469 | 1504 | } |
| 1470 | | return sema.mod.constInst(scope, src, .{ |
| 1505 | return sema.mod.constInst(sema.arena, src, .{ |
| 1471 | 1506 | .ty = child_type, |
| 1472 | 1507 | .val = val, |
| 1473 | 1508 | }); |
| ... | ... | @@ -1476,7 +1511,7 @@ fn zirOptionalPayload( |
| 1476 | 1511 | try sema.requireRuntimeBlock(block, src); |
| 1477 | 1512 | if (safety_check and block.wantSafety()) { |
| 1478 | 1513 | const is_non_null = try block.addUnOp(src, Type.initTag(.bool), .is_non_null, operand); |
| 1479 | | try mod.addSafetyCheck(b, is_non_null, .unwrap_null); |
| 1514 | try sema.addSafetyCheck(block, is_non_null, .unwrap_null); |
| 1480 | 1515 | } |
| 1481 | 1516 | return block.addUnOp(src, child_type, .optional_payload, operand); |
| 1482 | 1517 | } |
| ... | ... | @@ -1493,7 +1528,7 @@ fn zirErrUnionPayload( |
| 1493 | 1528 | |
| 1494 | 1529 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 1495 | 1530 | const src = inst_data.src(); |
| 1496 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 1531 | const operand = try sema.resolveInst(inst_data.operand); |
| 1497 | 1532 | if (operand.ty.zigTypeTag() != .ErrorUnion) |
| 1498 | 1533 | return sema.mod.fail(&block.base, operand.src, "expected error union type, found '{}'", .{operand.ty}); |
| 1499 | 1534 | |
| ... | ... | @@ -1502,7 +1537,7 @@ fn zirErrUnionPayload( |
| 1502 | 1537 | return sema.mod.fail(&block.base, src, "caught unexpected error '{s}'", .{name}); |
| 1503 | 1538 | } |
| 1504 | 1539 | const data = val.castTag(.error_union).?.data; |
| 1505 | | return sema.mod.constInst(scope, src, .{ |
| 1540 | return sema.mod.constInst(sema.arena, src, .{ |
| 1506 | 1541 | .ty = operand.ty.castTag(.error_union).?.data.payload, |
| 1507 | 1542 | .val = data, |
| 1508 | 1543 | }); |
| ... | ... | @@ -1510,7 +1545,7 @@ fn zirErrUnionPayload( |
| 1510 | 1545 | try sema.requireRuntimeBlock(block, src); |
| 1511 | 1546 | if (safety_check and block.wantSafety()) { |
| 1512 | 1547 | const is_non_err = try block.addUnOp(src, Type.initTag(.bool), .is_err, operand); |
| 1513 | | try mod.addSafetyCheck(b, is_non_err, .unwrap_errunion); |
| 1548 | try sema.addSafetyCheck(block, is_non_err, .unwrap_errunion); |
| 1514 | 1549 | } |
| 1515 | 1550 | return block.addUnOp(src, operand.ty.castTag(.error_union).?.data.payload, .unwrap_errunion_payload, operand); |
| 1516 | 1551 | } |
| ... | ... | @@ -1527,7 +1562,7 @@ fn zirErrUnionPayloadPtr( |
| 1527 | 1562 | |
| 1528 | 1563 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 1529 | 1564 | const src = inst_data.src(); |
| 1530 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 1565 | const operand = try sema.resolveInst(inst_data.operand); |
| 1531 | 1566 | assert(operand.ty.zigTypeTag() == .Pointer); |
| 1532 | 1567 | |
| 1533 | 1568 | if (operand.ty.elemType().zigTypeTag() != .ErrorUnion) |
| ... | ... | @@ -1542,7 +1577,7 @@ fn zirErrUnionPayloadPtr( |
| 1542 | 1577 | } |
| 1543 | 1578 | const data = val.castTag(.error_union).?.data; |
| 1544 | 1579 | // The same Value represents the pointer to the error union and the payload. |
| 1545 | | return sema.mod.constInst(scope, src, .{ |
| 1580 | return sema.mod.constInst(sema.arena, src, .{ |
| 1546 | 1581 | .ty = operand_pointer_ty, |
| 1547 | 1582 | .val = try Value.Tag.ref_val.create( |
| 1548 | 1583 | sema.arena, |
| ... | ... | @@ -1554,7 +1589,7 @@ fn zirErrUnionPayloadPtr( |
| 1554 | 1589 | try sema.requireRuntimeBlock(block, src); |
| 1555 | 1590 | if (safety_check and block.wantSafety()) { |
| 1556 | 1591 | const is_non_err = try block.addUnOp(src, Type.initTag(.bool), .is_err, operand); |
| 1557 | | try mod.addSafetyCheck(b, is_non_err, .unwrap_errunion); |
| 1592 | try sema.addSafetyCheck(block, is_non_err, .unwrap_errunion); |
| 1558 | 1593 | } |
| 1559 | 1594 | return block.addUnOp(src, operand_pointer_ty, .unwrap_errunion_payload_ptr, operand); |
| 1560 | 1595 | } |
| ... | ... | @@ -1566,14 +1601,14 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner |
| 1566 | 1601 | |
| 1567 | 1602 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 1568 | 1603 | const src = inst_data.src(); |
| 1569 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 1604 | const operand = try sema.resolveInst(inst_data.operand); |
| 1570 | 1605 | if (operand.ty.zigTypeTag() != .ErrorUnion) |
| 1571 | 1606 | return sema.mod.fail(&block.base, src, "expected error union type, found '{}'", .{operand.ty}); |
| 1572 | 1607 | |
| 1573 | 1608 | if (operand.value()) |val| { |
| 1574 | 1609 | assert(val.getError() != null); |
| 1575 | 1610 | const data = val.castTag(.error_union).?.data; |
| 1576 | | return sema.mod.constInst(scope, src, .{ |
| 1611 | return sema.mod.constInst(sema.arena, src, .{ |
| 1577 | 1612 | .ty = operand.ty.castTag(.error_union).?.data.error_set, |
| 1578 | 1613 | .val = data, |
| 1579 | 1614 | }); |
| ... | ... | @@ -1590,7 +1625,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In |
| 1590 | 1625 | |
| 1591 | 1626 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 1592 | 1627 | const src = inst_data.src(); |
| 1593 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 1628 | const operand = try sema.resolveInst(inst_data.operand); |
| 1594 | 1629 | assert(operand.ty.zigTypeTag() == .Pointer); |
| 1595 | 1630 | |
| 1596 | 1631 | if (operand.ty.elemType().zigTypeTag() != .ErrorUnion) |
| ... | ... | @@ -1600,7 +1635,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In |
| 1600 | 1635 | const val = try pointer_val.pointerDeref(sema.arena); |
| 1601 | 1636 | assert(val.getError() != null); |
| 1602 | 1637 | const data = val.castTag(.error_union).?.data; |
| 1603 | | return sema.mod.constInst(scope, src, .{ |
| 1638 | return sema.mod.constInst(sema.arena, src, .{ |
| 1604 | 1639 | .ty = operand.ty.elemType().castTag(.error_union).?.data.error_set, |
| 1605 | 1640 | .val = data, |
| 1606 | 1641 | }); |
| ... | ... | @@ -1616,7 +1651,7 @@ fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Inde |
| 1616 | 1651 | |
| 1617 | 1652 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 1618 | 1653 | const src = inst_data.src(); |
| 1619 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 1654 | const operand = try sema.resolveInst(inst_data.operand); |
| 1620 | 1655 | if (operand.ty.zigTypeTag() != .ErrorUnion) |
| 1621 | 1656 | return sema.mod.fail(&block.base, src, "expected error union type, found '{}'", .{operand.ty}); |
| 1622 | 1657 | if (operand.ty.castTag(.error_union).?.data.payload.zigTypeTag() != .Void) { |
| ... | ... | @@ -1651,7 +1686,7 @@ fn zirFnTypeCc(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index, var_args: |
| 1651 | 1686 | const extra = sema.code.extraData(zir.Inst.FnTypeCc, inst_data.payload_index); |
| 1652 | 1687 | const param_types = sema.code.extra[extra.end..][0..extra.data.param_types_len]; |
| 1653 | 1688 | |
| 1654 | | const cc_tv = try resolveInstConst(mod, scope, extra.data.cc); |
| 1689 | const cc_tv = try sema.resolveInstConst(block, .todo, extra.data.cc); |
| 1655 | 1690 | // TODO once we're capable of importing and analyzing decls from |
| 1656 | 1691 | // std.builtin, this needs to change |
| 1657 | 1692 | const cc_str = cc_tv.val.castTag(.enum_literal).?.data; |
| ... | ... | @@ -1676,7 +1711,7 @@ fn fnTypeCommon( |
| 1676 | 1711 | cc: std.builtin.CallingConvention, |
| 1677 | 1712 | var_args: bool, |
| 1678 | 1713 | ) InnerError!*Inst { |
| 1679 | | const return_type = try sema.resolveType(block, zir_return_type); |
| 1714 | const return_type = try sema.resolveType(block, src, zir_return_type); |
| 1680 | 1715 | |
| 1681 | 1716 | // Hot path for some common function types. |
| 1682 | 1717 | if (zir_param_types.len == 0 and !var_args) { |
| ... | ... | @@ -1699,7 +1734,7 @@ fn fnTypeCommon( |
| 1699 | 1734 | |
| 1700 | 1735 | const param_types = try sema.arena.alloc(Type, zir_param_types.len); |
| 1701 | 1736 | for (zir_param_types) |param_type, i| { |
| 1702 | | const resolved = try sema.resolveType(block, param_type); |
| 1737 | const resolved = try sema.resolveType(block, src, param_type); |
| 1703 | 1738 | // TODO skip for comptime params |
| 1704 | 1739 | if (!resolved.isValidVarType(false)) { |
| 1705 | 1740 | return sema.mod.fail(&block.base, .todo, "parameter of type '{}' must be declared comptime", .{resolved}); |
| ... | ... | @@ -1721,9 +1756,9 @@ fn zirAs(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Ins |
| 1721 | 1756 | defer tracy.end(); |
| 1722 | 1757 | |
| 1723 | 1758 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1724 | | const dest_type = try sema.resolveType(block, bin_inst.lhs); |
| 1725 | | const tzir_inst = sema.resolveInst(block, bin_inst.rhs); |
| 1726 | | return sema.coerce(scope, dest_type, tzir_inst); |
| 1759 | const dest_type = try sema.resolveType(block, .todo, bin_inst.lhs); |
| 1760 | const tzir_inst = try sema.resolveInst(bin_inst.rhs); |
| 1761 | return sema.coerce(block, dest_type, tzir_inst, .todo); |
| 1727 | 1762 | } |
| 1728 | 1763 | |
| 1729 | 1764 | fn zirPtrtoint(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -1731,7 +1766,7 @@ fn zirPtrtoint(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro |
| 1731 | 1766 | defer tracy.end(); |
| 1732 | 1767 | |
| 1733 | 1768 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; |
| 1734 | | const ptr = sema.resolveInst(block, inst_data.operand); |
| 1769 | const ptr = try sema.resolveInst(inst_data.operand); |
| 1735 | 1770 | if (ptr.ty.zigTypeTag() != .Pointer) { |
| 1736 | 1771 | const ptr_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 1737 | 1772 | return sema.mod.fail(&block.base, ptr_src, "expected pointer, found '{}'", .{ptr.ty}); |
| ... | ... | @@ -1752,7 +1787,7 @@ fn zirFieldVal(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro |
| 1752 | 1787 | const field_name_src: LazySrcLoc = .{ .node_offset_field_name = inst_data.src_node }; |
| 1753 | 1788 | const extra = sema.code.extraData(zir.Inst.Field, inst_data.payload_index).data; |
| 1754 | 1789 | const field_name = sema.code.string_bytes[extra.field_name_start..][0..extra.field_name_len]; |
| 1755 | | const object = sema.resolveInst(block, extra.lhs); |
| 1790 | const object = try sema.resolveInst(extra.lhs); |
| 1756 | 1791 | const object_ptr = try sema.analyzeRef(block, src, object); |
| 1757 | 1792 | const result_ptr = try sema.namedFieldPtr(block, src, object_ptr, field_name, field_name_src); |
| 1758 | 1793 | return sema.analyzeDeref(block, src, result_ptr, result_ptr.src); |
| ... | ... | @@ -1767,7 +1802,7 @@ fn zirFieldPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro |
| 1767 | 1802 | const field_name_src: LazySrcLoc = .{ .node_offset_field_name = inst_data.src_node }; |
| 1768 | 1803 | const extra = sema.code.extraData(zir.Inst.Field, inst_data.payload_index).data; |
| 1769 | 1804 | const field_name = sema.code.string_bytes[extra.field_name_start..][0..extra.field_name_len]; |
| 1770 | | const object_ptr = sema.resolveInst(block, extra.lhs); |
| 1805 | const object_ptr = try sema.resolveInst(extra.lhs); |
| 1771 | 1806 | return sema.namedFieldPtr(block, src, object_ptr, field_name, field_name_src); |
| 1772 | 1807 | } |
| 1773 | 1808 | |
| ... | ... | @@ -1779,7 +1814,7 @@ fn zirFieldValNamed(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inne |
| 1779 | 1814 | const src = inst_data.src(); |
| 1780 | 1815 | const field_name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; |
| 1781 | 1816 | const extra = sema.code.extraData(zir.Inst.FieldNamed, inst_data.payload_index).data; |
| 1782 | | const object = sema.resolveInst(block, extra.lhs); |
| 1817 | const object = try sema.resolveInst(extra.lhs); |
| 1783 | 1818 | const field_name = try sema.resolveConstString(block, field_name_src, extra.field_name); |
| 1784 | 1819 | const object_ptr = try sema.analyzeRef(block, src, object); |
| 1785 | 1820 | const result_ptr = try sema.namedFieldPtr(block, src, object_ptr, field_name, field_name_src); |
| ... | ... | @@ -1794,7 +1829,7 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inne |
| 1794 | 1829 | const src = inst_data.src(); |
| 1795 | 1830 | const field_name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; |
| 1796 | 1831 | const extra = sema.code.extraData(zir.Inst.FieldNamed, inst_data.payload_index).data; |
| 1797 | | const object_ptr = sema.resolveInst(block, extra.lhs); |
| 1832 | const object_ptr = try sema.resolveInst(extra.lhs); |
| 1798 | 1833 | const field_name = try sema.resolveConstString(block, field_name_src, extra.field_name); |
| 1799 | 1834 | return sema.namedFieldPtr(block, src, object_ptr, field_name, field_name_src); |
| 1800 | 1835 | } |
| ... | ... | @@ -1803,40 +1838,43 @@ fn zirIntcast(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 1803 | 1838 | const tracy = trace(@src()); |
| 1804 | 1839 | defer tracy.end(); |
| 1805 | 1840 | |
| 1806 | | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1807 | | const dest_type = try sema.resolveType(block, bin_inst.lhs); |
| 1808 | | const operand = sema.resolveInst(bin_inst.rhs); |
| 1841 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 1842 | const src = inst_data.src(); |
| 1843 | const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 1844 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; |
| 1845 | const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data; |
| 1846 | |
| 1847 | const dest_type = try sema.resolveType(block, dest_ty_src, extra.lhs); |
| 1848 | const operand = try sema.resolveInst(extra.rhs); |
| 1809 | 1849 | |
| 1810 | 1850 | const dest_is_comptime_int = switch (dest_type.zigTypeTag()) { |
| 1811 | 1851 | .ComptimeInt => true, |
| 1812 | 1852 | .Int => false, |
| 1813 | | else => return mod.fail( |
| 1814 | | scope, |
| 1815 | | inst.positionals.lhs.src, |
| 1853 | else => return sema.mod.fail( |
| 1854 | &block.base, |
| 1855 | dest_ty_src, |
| 1816 | 1856 | "expected integer type, found '{}'", |
| 1817 | | .{ |
| 1818 | | dest_type, |
| 1819 | | }, |
| 1857 | .{dest_type}, |
| 1820 | 1858 | ), |
| 1821 | 1859 | }; |
| 1822 | 1860 | |
| 1823 | 1861 | switch (operand.ty.zigTypeTag()) { |
| 1824 | 1862 | .ComptimeInt, .Int => {}, |
| 1825 | | else => return mod.fail( |
| 1826 | | scope, |
| 1827 | | inst.positionals.rhs.src, |
| 1863 | else => return sema.mod.fail( |
| 1864 | &block.base, |
| 1865 | operand_src, |
| 1828 | 1866 | "expected integer type, found '{}'", |
| 1829 | 1867 | .{operand.ty}, |
| 1830 | 1868 | ), |
| 1831 | 1869 | } |
| 1832 | 1870 | |
| 1833 | 1871 | if (operand.value() != null) { |
| 1834 | | return sema.coerce(scope, dest_type, operand); |
| 1872 | return sema.coerce(block, dest_type, operand, operand_src); |
| 1835 | 1873 | } else if (dest_is_comptime_int) { |
| 1836 | | return sema.mod.fail(&block.base, inst.base.src, "unable to cast runtime value to 'comptime_int'", .{}); |
| 1874 | return sema.mod.fail(&block.base, src, "unable to cast runtime value to 'comptime_int'", .{}); |
| 1837 | 1875 | } |
| 1838 | 1876 | |
| 1839 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement analyze widen or shorten int", .{}); |
| 1877 | return sema.mod.fail(&block.base, src, "TODO implement analyze widen or shorten int", .{}); |
| 1840 | 1878 | } |
| 1841 | 1879 | |
| 1842 | 1880 | fn zirBitcast(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -1844,49 +1882,52 @@ fn zirBitcast(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 1844 | 1882 | defer tracy.end(); |
| 1845 | 1883 | |
| 1846 | 1884 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1847 | | const dest_type = try sema.resolveType(block, bin_inst.lhs); |
| 1848 | | const operand = sema.resolveInst(bin_inst.rhs); |
| 1849 | | return mod.bitcast(scope, dest_type, operand); |
| 1885 | const dest_type = try sema.resolveType(block, .todo, bin_inst.lhs); |
| 1886 | const operand = try sema.resolveInst(bin_inst.rhs); |
| 1887 | return sema.bitcast(block, dest_type, operand); |
| 1850 | 1888 | } |
| 1851 | 1889 | |
| 1852 | 1890 | fn zirFloatcast(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1853 | 1891 | const tracy = trace(@src()); |
| 1854 | 1892 | defer tracy.end(); |
| 1855 | 1893 | |
| 1856 | | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1857 | | const dest_type = try sema.resolveType(block, bin_inst.lhs); |
| 1858 | | const operand = sema.resolveInst(bin_inst.rhs); |
| 1894 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 1895 | const src = inst_data.src(); |
| 1896 | const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 1897 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; |
| 1898 | const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data; |
| 1899 | |
| 1900 | const dest_type = try sema.resolveType(block, dest_ty_src, extra.lhs); |
| 1901 | const operand = try sema.resolveInst(extra.rhs); |
| 1859 | 1902 | |
| 1860 | 1903 | const dest_is_comptime_float = switch (dest_type.zigTypeTag()) { |
| 1861 | 1904 | .ComptimeFloat => true, |
| 1862 | 1905 | .Float => false, |
| 1863 | | else => return mod.fail( |
| 1864 | | scope, |
| 1865 | | inst.positionals.lhs.src, |
| 1906 | else => return sema.mod.fail( |
| 1907 | &block.base, |
| 1908 | dest_ty_src, |
| 1866 | 1909 | "expected float type, found '{}'", |
| 1867 | | .{ |
| 1868 | | dest_type, |
| 1869 | | }, |
| 1910 | .{dest_type}, |
| 1870 | 1911 | ), |
| 1871 | 1912 | }; |
| 1872 | 1913 | |
| 1873 | 1914 | switch (operand.ty.zigTypeTag()) { |
| 1874 | 1915 | .ComptimeFloat, .Float, .ComptimeInt => {}, |
| 1875 | | else => return mod.fail( |
| 1876 | | scope, |
| 1877 | | inst.positionals.rhs.src, |
| 1916 | else => return sema.mod.fail( |
| 1917 | &block.base, |
| 1918 | operand_src, |
| 1878 | 1919 | "expected float type, found '{}'", |
| 1879 | 1920 | .{operand.ty}, |
| 1880 | 1921 | ), |
| 1881 | 1922 | } |
| 1882 | 1923 | |
| 1883 | 1924 | if (operand.value() != null) { |
| 1884 | | return sema.coerce(scope, dest_type, operand); |
| 1925 | return sema.coerce(block, dest_type, operand, operand_src); |
| 1885 | 1926 | } else if (dest_is_comptime_float) { |
| 1886 | | return sema.mod.fail(&block.base, inst.base.src, "unable to cast runtime value to 'comptime_float'", .{}); |
| 1927 | return sema.mod.fail(&block.base, src, "unable to cast runtime value to 'comptime_float'", .{}); |
| 1887 | 1928 | } |
| 1888 | 1929 | |
| 1889 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement analyze widen or shorten float", .{}); |
| 1930 | return sema.mod.fail(&block.base, src, "TODO implement analyze widen or shorten float", .{}); |
| 1890 | 1931 | } |
| 1891 | 1932 | |
| 1892 | 1933 | fn zirElemVal(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -1894,9 +1935,9 @@ fn zirElemVal(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 1894 | 1935 | defer tracy.end(); |
| 1895 | 1936 | |
| 1896 | 1937 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1897 | | const array = sema.resolveInst(block, bin_inst.lhs); |
| 1938 | const array = try sema.resolveInst(bin_inst.lhs); |
| 1898 | 1939 | const array_ptr = try sema.analyzeRef(block, sema.src, array); |
| 1899 | | const elem_index = sema.resolveInst(block, bin_inst.rhs); |
| 1940 | const elem_index = try sema.resolveInst(bin_inst.rhs); |
| 1900 | 1941 | const result_ptr = try sema.elemPtr(block, sema.src, array_ptr, elem_index, sema.src); |
| 1901 | 1942 | return sema.analyzeDeref(block, sema.src, result_ptr, sema.src); |
| 1902 | 1943 | } |
| ... | ... | @@ -1909,9 +1950,9 @@ fn zirElemValNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE |
| 1909 | 1950 | const src = inst_data.src(); |
| 1910 | 1951 | const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node }; |
| 1911 | 1952 | const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data; |
| 1912 | | const array = sema.resolveInst(block, extra.lhs); |
| 1953 | const array = try sema.resolveInst(extra.lhs); |
| 1913 | 1954 | const array_ptr = try sema.analyzeRef(block, src, array); |
| 1914 | | const elem_index = sema.resolveInst(block, extra.rhs); |
| 1955 | const elem_index = try sema.resolveInst(extra.rhs); |
| 1915 | 1956 | const result_ptr = try sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src); |
| 1916 | 1957 | return sema.analyzeDeref(block, src, result_ptr, src); |
| 1917 | 1958 | } |
| ... | ... | @@ -1921,8 +1962,8 @@ fn zirElemPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 1921 | 1962 | defer tracy.end(); |
| 1922 | 1963 | |
| 1923 | 1964 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1924 | | const array_ptr = sema.resolveInst(block, bin_inst.lhs); |
| 1925 | | const elem_index = sema.resolveInst(block, bin_inst.rhs); |
| 1965 | const array_ptr = try sema.resolveInst(bin_inst.lhs); |
| 1966 | const elem_index = try sema.resolveInst(bin_inst.rhs); |
| 1926 | 1967 | return sema.elemPtr(block, sema.src, array_ptr, elem_index, sema.src); |
| 1927 | 1968 | } |
| 1928 | 1969 | |
| ... | ... | @@ -1934,8 +1975,8 @@ fn zirElemPtrNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE |
| 1934 | 1975 | const src = inst_data.src(); |
| 1935 | 1976 | const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node }; |
| 1936 | 1977 | const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data; |
| 1937 | | const array_ptr = sema.resolveInst(block, extra.lhs); |
| 1938 | | const elem_index = sema.resolveInst(block, extra.rhs); |
| 1978 | const array_ptr = try sema.resolveInst(extra.lhs); |
| 1979 | const elem_index = try sema.resolveInst(extra.rhs); |
| 1939 | 1980 | return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src); |
| 1940 | 1981 | } |
| 1941 | 1982 | |
| ... | ... | @@ -1946,8 +1987,8 @@ fn zirSliceStart(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr |
| 1946 | 1987 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 1947 | 1988 | const src = inst_data.src(); |
| 1948 | 1989 | const extra = sema.code.extraData(zir.Inst.SliceStart, inst_data.payload_index).data; |
| 1949 | | const array_ptr = sema.resolveInst(extra.lhs); |
| 1950 | | const start = sema.resolveInst(extra.start); |
| 1990 | const array_ptr = try sema.resolveInst(extra.lhs); |
| 1991 | const start = try sema.resolveInst(extra.start); |
| 1951 | 1992 | |
| 1952 | 1993 | return sema.analyzeSlice(block, src, array_ptr, start, null, null, .unneeded); |
| 1953 | 1994 | } |
| ... | ... | @@ -1959,9 +2000,9 @@ fn zirSliceEnd(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro |
| 1959 | 2000 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 1960 | 2001 | const src = inst_data.src(); |
| 1961 | 2002 | const extra = sema.code.extraData(zir.Inst.SliceEnd, inst_data.payload_index).data; |
| 1962 | | const array_ptr = sema.resolveInst(extra.lhs); |
| 1963 | | const start = sema.resolveInst(extra.start); |
| 1964 | | const end = sema.resolveInst(extra.end); |
| 2003 | const array_ptr = try sema.resolveInst(extra.lhs); |
| 2004 | const start = try sema.resolveInst(extra.start); |
| 2005 | const end = try sema.resolveInst(extra.end); |
| 1965 | 2006 | |
| 1966 | 2007 | return sema.analyzeSlice(block, src, array_ptr, start, end, null, .unneeded); |
| 1967 | 2008 | } |
| ... | ... | @@ -1974,21 +2015,22 @@ fn zirSliceSentinel(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inne |
| 1974 | 2015 | const src = inst_data.src(); |
| 1975 | 2016 | const sentinel_src: LazySrcLoc = .{ .node_offset_slice_sentinel = inst_data.src_node }; |
| 1976 | 2017 | const extra = sema.code.extraData(zir.Inst.SliceSentinel, inst_data.payload_index).data; |
| 1977 | | const array_ptr = sema.resolveInst(extra.lhs); |
| 1978 | | const start = sema.resolveInst(extra.start); |
| 1979 | | const end = sema.resolveInst(extra.end); |
| 1980 | | const sentinel = sema.resolveInst(extra.sentinel); |
| 2018 | const array_ptr = try sema.resolveInst(extra.lhs); |
| 2019 | const start = try sema.resolveInst(extra.start); |
| 2020 | const end = try sema.resolveInst(extra.end); |
| 2021 | const sentinel = try sema.resolveInst(extra.sentinel); |
| 1981 | 2022 | |
| 1982 | | return sema.analyzeSlice(block, inst.base.src, array_ptr, start, end, sentinel, sentinel_src); |
| 2023 | return sema.analyzeSlice(block, src, array_ptr, start, end, sentinel, sentinel_src); |
| 1983 | 2024 | } |
| 1984 | 2025 | |
| 1985 | 2026 | fn zirSwitchRange(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1986 | 2027 | const tracy = trace(@src()); |
| 1987 | 2028 | defer tracy.end(); |
| 1988 | 2029 | |
| 2030 | const src: LazySrcLoc = .todo; |
| 1989 | 2031 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 1990 | | const start = sema.resolveInst(bin_inst.lhs); |
| 1991 | | const end = sema.resolveInst(bin_inst.rhs); |
| 2032 | const start = try sema.resolveInst(bin_inst.lhs); |
| 2033 | const end = try sema.resolveInst(bin_inst.rhs); |
| 1992 | 2034 | |
| 1993 | 2035 | switch (start.ty.zigTypeTag()) { |
| 1994 | 2036 | .Int, .ComptimeInt => {}, |
| ... | ... | @@ -2002,7 +2044,7 @@ fn zirSwitchRange(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE |
| 2002 | 2044 | const start_val = start.value().?; |
| 2003 | 2045 | const end_val = end.value().?; |
| 2004 | 2046 | if (start_val.compare(.gte, end_val)) { |
| 2005 | | return sema.mod.fail(&block.base, inst.base.src, "range start value must be smaller than the end value", .{}); |
| 2047 | return sema.mod.fail(&block.base, src, "range start value must be smaller than the end value", .{}); |
| 2006 | 2048 | } |
| 2007 | 2049 | return sema.mod.constVoid(sema.arena, .unneeded); |
| 2008 | 2050 | } |
| ... | ... | @@ -2018,32 +2060,32 @@ fn zirSwitchBr( |
| 2018 | 2060 | |
| 2019 | 2061 | if (true) @panic("TODO rework with zir-memory-layout in mind"); |
| 2020 | 2062 | |
| 2021 | | const target_ptr = sema.resolveInst(block, inst.positionals.target); |
| 2063 | const target_ptr = try sema.resolveInst(inst.positionals.target); |
| 2022 | 2064 | const target = if (ref) |
| 2023 | | try sema.analyzeDeref(block, inst.base.src, target_ptr, inst.positionals.target.src) |
| 2065 | try sema.analyzeDeref(parent_block, inst.base.src, target_ptr, inst.positionals.target.src) |
| 2024 | 2066 | else |
| 2025 | 2067 | target_ptr; |
| 2026 | | try validateSwitch(mod, scope, target, inst); |
| 2068 | try sema.validateSwitch(parent_block, target, inst); |
| 2027 | 2069 | |
| 2028 | | if (try mod.resolveDefinedValue(scope, target)) |target_val| { |
| 2070 | if (try sema.resolveDefinedValue(parent_block, inst.base.src, target)) |target_val| { |
| 2029 | 2071 | for (inst.positionals.cases) |case| { |
| 2030 | | const resolved = sema.resolveInst(block, case.item); |
| 2031 | | const casted = try sema.coerce(scope, target.ty, resolved); |
| 2072 | const resolved = try sema.resolveInst(case.item); |
| 2073 | const casted = try sema.coerce(block, target.ty, resolved, resolved_src); |
| 2032 | 2074 | const item = try sema.resolveConstValue(parent_block, case_src, casted); |
| 2033 | 2075 | |
| 2034 | 2076 | if (target_val.eql(item)) { |
| 2035 | | try sema.analyzeBody(scope.cast(Scope.Block).?, case.body); |
| 2036 | | return mod.constNoReturn(scope, inst.base.src); |
| 2077 | try sema.analyzeBody(parent_block, case.body); |
| 2078 | return sema.mod.constNoReturn(sema.arena, inst.base.src); |
| 2037 | 2079 | } |
| 2038 | 2080 | } |
| 2039 | | try sema.analyzeBody(scope.cast(Scope.Block).?, inst.positionals.else_body); |
| 2040 | | return mod.constNoReturn(scope, inst.base.src); |
| 2081 | try sema.analyzeBody(parent_block, inst.positionals.else_body); |
| 2082 | return sema.mod.constNoReturn(sema.arena, inst.base.src); |
| 2041 | 2083 | } |
| 2042 | 2084 | |
| 2043 | 2085 | if (inst.positionals.cases.len == 0) { |
| 2044 | 2086 | // no cases just analyze else_branch |
| 2045 | | try sema.analyzeBody(scope.cast(Scope.Block).?, inst.positionals.else_body); |
| 2046 | | return mod.constNoReturn(scope, inst.base.src); |
| 2087 | try sema.analyzeBody(parent_block, inst.positionals.else_body); |
| 2088 | return sema.mod.constNoReturn(sema.arena, inst.base.src); |
| 2047 | 2089 | } |
| 2048 | 2090 | |
| 2049 | 2091 | try sema.requireRuntimeBlock(parent_block, inst.base.src); |
| ... | ... | @@ -2051,24 +2093,20 @@ fn zirSwitchBr( |
| 2051 | 2093 | |
| 2052 | 2094 | var case_block: Scope.Block = .{ |
| 2053 | 2095 | .parent = parent_block, |
| 2054 | | .inst_table = parent_block.inst_table, |
| 2055 | | .func = parent_block.func, |
| 2056 | | .owner_decl = parent_block.owner_decl, |
| 2096 | .sema = sema, |
| 2057 | 2097 | .src_decl = parent_block.src_decl, |
| 2058 | 2098 | .instructions = .{}, |
| 2059 | | .arena = sema.arena, |
| 2060 | 2099 | .inlining = parent_block.inlining, |
| 2061 | 2100 | .is_comptime = parent_block.is_comptime, |
| 2062 | | .branch_quota = parent_block.branch_quota, |
| 2063 | 2101 | }; |
| 2064 | | defer case_block.instructions.deinit(mod.gpa); |
| 2102 | defer case_block.instructions.deinit(sema.gpa); |
| 2065 | 2103 | |
| 2066 | 2104 | for (inst.positionals.cases) |case, i| { |
| 2067 | 2105 | // Reset without freeing. |
| 2068 | 2106 | case_block.instructions.items.len = 0; |
| 2069 | 2107 | |
| 2070 | | const resolved = sema.resolveInst(block, case.item); |
| 2071 | | const casted = try sema.coerce(scope, target.ty, resolved); |
| 2108 | const resolved = try sema.resolveInst(case.item); |
| 2109 | const casted = try sema.coerce(block, target.ty, resolved, resolved_src); |
| 2072 | 2110 | const item = try sema.resolveConstValue(parent_block, case_src, casted); |
| 2073 | 2111 | |
| 2074 | 2112 | try sema.analyzeBody(&case_block, case.body); |
| ... | ... | @@ -2113,15 +2151,15 @@ fn validateSwitch(sema: *Sema, block: *Scope.Block, target: *Inst, inst: zir.Ins |
| 2113 | 2151 | .ErrorSet => return sema.mod.fail(&block.base, inst.base.src, "TODO validateSwitch .ErrorSet", .{}), |
| 2114 | 2152 | .Union => return sema.mod.fail(&block.base, inst.base.src, "TODO validateSwitch .Union", .{}), |
| 2115 | 2153 | .Int, .ComptimeInt => { |
| 2116 | | var range_set = @import("RangeSet.zig").init(mod.gpa); |
| 2154 | var range_set = @import("RangeSet.zig").init(sema.gpa); |
| 2117 | 2155 | defer range_set.deinit(); |
| 2118 | 2156 | |
| 2119 | 2157 | for (inst.positionals.items) |item| { |
| 2120 | 2158 | const maybe_src = if (item.castTag(.switch_range)) |range| blk: { |
| 2121 | | const start_resolved = sema.resolveInst(block, range.positionals.lhs); |
| 2122 | | const start_casted = try sema.coerce(scope, target.ty, start_resolved); |
| 2123 | | const end_resolved = sema.resolveInst(block, range.positionals.rhs); |
| 2124 | | const end_casted = try sema.coerce(scope, target.ty, end_resolved); |
| 2159 | const start_resolved = try sema.resolveInst(range.positionals.lhs); |
| 2160 | const start_casted = try sema.coerce(block, target.ty, start_resolved); |
| 2161 | const end_resolved = try sema.resolveInst(range.positionals.rhs); |
| 2162 | const end_casted = try sema.coerce(block, target.ty, end_resolved); |
| 2125 | 2163 | |
| 2126 | 2164 | break :blk try range_set.add( |
| 2127 | 2165 | try sema.resolveConstValue(block, range_start_src, start_casted), |
| ... | ... | @@ -2129,8 +2167,8 @@ fn validateSwitch(sema: *Sema, block: *Scope.Block, target: *Inst, inst: zir.Ins |
| 2129 | 2167 | item.src, |
| 2130 | 2168 | ); |
| 2131 | 2169 | } else blk: { |
| 2132 | | const resolved = sema.resolveInst(block, item); |
| 2133 | | const casted = try sema.coerce(scope, target.ty, resolved); |
| 2170 | const resolved = try sema.resolveInst(item); |
| 2171 | const casted = try sema.coerce(block, target.ty, resolved); |
| 2134 | 2172 | const value = try sema.resolveConstValue(block, item_src, casted); |
| 2135 | 2173 | break :blk try range_set.add(value, value, item.src); |
| 2136 | 2174 | }; |
| ... | ... | @@ -2142,7 +2180,7 @@ fn validateSwitch(sema: *Sema, block: *Scope.Block, target: *Inst, inst: zir.Ins |
| 2142 | 2180 | } |
| 2143 | 2181 | |
| 2144 | 2182 | if (target.ty.zigTypeTag() == .Int) { |
| 2145 | | var arena = std.heap.ArenaAllocator.init(mod.gpa); |
| 2183 | var arena = std.heap.ArenaAllocator.init(sema.gpa); |
| 2146 | 2184 | defer arena.deinit(); |
| 2147 | 2185 | |
| 2148 | 2186 | const start = try target.ty.minInt(&arena, mod.getTarget()); |
| ... | ... | @@ -2163,8 +2201,8 @@ fn validateSwitch(sema: *Sema, block: *Scope.Block, target: *Inst, inst: zir.Ins |
| 2163 | 2201 | var true_count: u8 = 0; |
| 2164 | 2202 | var false_count: u8 = 0; |
| 2165 | 2203 | for (inst.positionals.items) |item| { |
| 2166 | | const resolved = sema.resolveInst(block, item); |
| 2167 | | const casted = try sema.coerce(scope, Type.initTag(.bool), resolved); |
| 2204 | const resolved = try sema.resolveInst(item); |
| 2205 | const casted = try sema.coerce(block, Type.initTag(.bool), resolved); |
| 2168 | 2206 | if ((try sema.resolveConstValue(block, item_src, casted)).toBool()) { |
| 2169 | 2207 | true_count += 1; |
| 2170 | 2208 | } else { |
| ... | ... | @@ -2187,12 +2225,12 @@ fn validateSwitch(sema: *Sema, block: *Scope.Block, target: *Inst, inst: zir.Ins |
| 2187 | 2225 | return sema.mod.fail(&block.base, inst.base.src, "else prong required when switching on type '{}'", .{target.ty}); |
| 2188 | 2226 | } |
| 2189 | 2227 | |
| 2190 | | var seen_values = std.HashMap(Value, usize, Value.hash, Value.eql, std.hash_map.DefaultMaxLoadPercentage).init(mod.gpa); |
| 2228 | var seen_values = std.HashMap(Value, usize, Value.hash, Value.eql, std.hash_map.DefaultMaxLoadPercentage).init(sema.gpa); |
| 2191 | 2229 | defer seen_values.deinit(); |
| 2192 | 2230 | |
| 2193 | 2231 | for (inst.positionals.items) |item| { |
| 2194 | | const resolved = sema.resolveInst(block, item); |
| 2195 | | const casted = try sema.coerce(scope, target.ty, resolved); |
| 2232 | const resolved = try sema.resolveInst(item); |
| 2233 | const casted = try sema.coerce(block, target.ty, resolved); |
| 2196 | 2234 | const val = try sema.resolveConstValue(block, item_src, casted); |
| 2197 | 2235 | |
| 2198 | 2236 | if (try seen_values.fetchPut(val, item.src)) |prev| { |
| ... | ... | @@ -2249,27 +2287,30 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError! |
| 2249 | 2287 | fn zirShl(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2250 | 2288 | const tracy = trace(@src()); |
| 2251 | 2289 | defer tracy.end(); |
| 2252 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement zirShl", .{}); |
| 2290 | return sema.mod.fail(&block.base, sema.src, "TODO implement zirShl", .{}); |
| 2253 | 2291 | } |
| 2254 | 2292 | |
| 2255 | 2293 | fn zirShr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2256 | 2294 | const tracy = trace(@src()); |
| 2257 | 2295 | defer tracy.end(); |
| 2258 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement zirShr", .{}); |
| 2296 | return sema.mod.fail(&block.base, sema.src, "TODO implement zirShr", .{}); |
| 2259 | 2297 | } |
| 2260 | 2298 | |
| 2261 | 2299 | fn zirBitwise(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2262 | 2300 | const tracy = trace(@src()); |
| 2263 | 2301 | defer tracy.end(); |
| 2264 | 2302 | |
| 2303 | if (true) @panic("TODO rework with zir-memory-layout in mind"); |
| 2304 | |
| 2265 | 2305 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 2266 | | const lhs = sema.resolveInst(bin_inst.lhs); |
| 2267 | | const rhs = sema.resolveInst(bin_inst.rhs); |
| 2306 | const src: LazySrcLoc = .todo; |
| 2307 | const lhs = try sema.resolveInst(bin_inst.lhs); |
| 2308 | const rhs = try sema.resolveInst(bin_inst.rhs); |
| 2268 | 2309 | |
| 2269 | 2310 | const instructions = &[_]*Inst{ lhs, rhs }; |
| 2270 | 2311 | const resolved_type = try sema.resolvePeerTypes(block, instructions); |
| 2271 | | const casted_lhs = try sema.coerce(scope, resolved_type, lhs); |
| 2272 | | const casted_rhs = try sema.coerce(scope, resolved_type, rhs); |
| 2312 | const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs.src); |
| 2313 | const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs.src); |
| 2273 | 2314 | |
| 2274 | 2315 | const scalar_type = if (resolved_type.zigTypeTag() == .Vector) |
| 2275 | 2316 | resolved_type.elemType() |
| ... | ... | @@ -2280,14 +2321,14 @@ fn zirBitwise(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 2280 | 2321 | |
| 2281 | 2322 | if (lhs.ty.zigTypeTag() == .Vector and rhs.ty.zigTypeTag() == .Vector) { |
| 2282 | 2323 | if (lhs.ty.arrayLen() != rhs.ty.arrayLen()) { |
| 2283 | | return sema.mod.fail(&block.base, inst.base.src, "vector length mismatch: {d} and {d}", .{ |
| 2324 | return sema.mod.fail(&block.base, src, "vector length mismatch: {d} and {d}", .{ |
| 2284 | 2325 | lhs.ty.arrayLen(), |
| 2285 | 2326 | rhs.ty.arrayLen(), |
| 2286 | 2327 | }); |
| 2287 | 2328 | } |
| 2288 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement support for vectors in zirBitwise", .{}); |
| 2329 | return sema.mod.fail(&block.base, src, "TODO implement support for vectors in zirBitwise", .{}); |
| 2289 | 2330 | } else if (lhs.ty.zigTypeTag() == .Vector or rhs.ty.zigTypeTag() == .Vector) { |
| 2290 | | return sema.mod.fail(&block.base, inst.base.src, "mixed scalar and vector operands to binary expression: '{}' and '{}'", .{ |
| 2331 | return sema.mod.fail(&block.base, src, "mixed scalar and vector operands to binary expression: '{}' and '{}'", .{ |
| 2291 | 2332 | lhs.ty, |
| 2292 | 2333 | rhs.ty, |
| 2293 | 2334 | }); |
| ... | ... | @@ -2296,22 +2337,22 @@ fn zirBitwise(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 2296 | 2337 | const is_int = scalar_tag == .Int or scalar_tag == .ComptimeInt; |
| 2297 | 2338 | |
| 2298 | 2339 | if (!is_int) { |
| 2299 | | return sema.mod.fail(&block.base, inst.base.src, "invalid operands to binary bitwise expression: '{s}' and '{s}'", .{ @tagName(lhs.ty.zigTypeTag()), @tagName(rhs.ty.zigTypeTag()) }); |
| 2340 | return sema.mod.fail(&block.base, src, "invalid operands to binary bitwise expression: '{s}' and '{s}'", .{ @tagName(lhs.ty.zigTypeTag()), @tagName(rhs.ty.zigTypeTag()) }); |
| 2300 | 2341 | } |
| 2301 | 2342 | |
| 2302 | 2343 | if (casted_lhs.value()) |lhs_val| { |
| 2303 | 2344 | if (casted_rhs.value()) |rhs_val| { |
| 2304 | 2345 | if (lhs_val.isUndef() or rhs_val.isUndef()) { |
| 2305 | | return sema.mod.constInst(scope, inst.base.src, .{ |
| 2346 | return sema.mod.constInst(sema.arena, src, .{ |
| 2306 | 2347 | .ty = resolved_type, |
| 2307 | 2348 | .val = Value.initTag(.undef), |
| 2308 | 2349 | }); |
| 2309 | 2350 | } |
| 2310 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement comptime bitwise operations", .{}); |
| 2351 | return sema.mod.fail(&block.base, src, "TODO implement comptime bitwise operations", .{}); |
| 2311 | 2352 | } |
| 2312 | 2353 | } |
| 2313 | 2354 | |
| 2314 | | try sema.requireRuntimeBlock(block, inst.base.src); |
| 2355 | try sema.requireRuntimeBlock(block, src); |
| 2315 | 2356 | const ir_tag = switch (inst.base.tag) { |
| 2316 | 2357 | .bit_and => Inst.Tag.bit_and, |
| 2317 | 2358 | .bit_or => Inst.Tag.bit_or, |
| ... | ... | @@ -2319,39 +2360,42 @@ fn zirBitwise(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 2319 | 2360 | else => unreachable, |
| 2320 | 2361 | }; |
| 2321 | 2362 | |
| 2322 | | return mod.addBinOp(b, inst.base.src, scalar_type, ir_tag, casted_lhs, casted_rhs); |
| 2363 | return block.addBinOp(src, scalar_type, ir_tag, casted_lhs, casted_rhs); |
| 2323 | 2364 | } |
| 2324 | 2365 | |
| 2325 | 2366 | fn zirBitNot(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2326 | 2367 | const tracy = trace(@src()); |
| 2327 | 2368 | defer tracy.end(); |
| 2328 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement zirBitNot", .{}); |
| 2369 | return sema.mod.fail(&block.base, sema.src, "TODO implement zirBitNot", .{}); |
| 2329 | 2370 | } |
| 2330 | 2371 | |
| 2331 | 2372 | fn zirArrayCat(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2332 | 2373 | const tracy = trace(@src()); |
| 2333 | 2374 | defer tracy.end(); |
| 2334 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement zirArrayCat", .{}); |
| 2375 | return sema.mod.fail(&block.base, sema.src, "TODO implement zirArrayCat", .{}); |
| 2335 | 2376 | } |
| 2336 | 2377 | |
| 2337 | 2378 | fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2338 | 2379 | const tracy = trace(@src()); |
| 2339 | 2380 | defer tracy.end(); |
| 2340 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement zirArrayMul", .{}); |
| 2381 | return sema.mod.fail(&block.base, sema.src, "TODO implement zirArrayMul", .{}); |
| 2341 | 2382 | } |
| 2342 | 2383 | |
| 2343 | 2384 | fn zirArithmetic(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2344 | 2385 | const tracy = trace(@src()); |
| 2345 | 2386 | defer tracy.end(); |
| 2346 | 2387 | |
| 2388 | if (true) @panic("TODO rework with zir-memory-layout in mind"); |
| 2389 | |
| 2347 | 2390 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 2348 | | const lhs = sema.resolveInst(bin_inst.lhs); |
| 2349 | | const rhs = sema.resolveInst(bin_inst.rhs); |
| 2391 | const src: LazySrcLoc = .todo; |
| 2392 | const lhs = try sema.resolveInst(bin_inst.lhs); |
| 2393 | const rhs = try sema.resolveInst(bin_inst.rhs); |
| 2350 | 2394 | |
| 2351 | 2395 | const instructions = &[_]*Inst{ lhs, rhs }; |
| 2352 | 2396 | const resolved_type = try sema.resolvePeerTypes(block, instructions); |
| 2353 | | const casted_lhs = try sema.coerce(scope, resolved_type, lhs); |
| 2354 | | const casted_rhs = try sema.coerce(scope, resolved_type, rhs); |
| 2397 | const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs.src); |
| 2398 | const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs.src); |
| 2355 | 2399 | |
| 2356 | 2400 | const scalar_type = if (resolved_type.zigTypeTag() == .Vector) |
| 2357 | 2401 | resolved_type.elemType() |
| ... | ... | @@ -2362,14 +2406,14 @@ fn zirArithmetic(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr |
| 2362 | 2406 | |
| 2363 | 2407 | if (lhs.ty.zigTypeTag() == .Vector and rhs.ty.zigTypeTag() == .Vector) { |
| 2364 | 2408 | if (lhs.ty.arrayLen() != rhs.ty.arrayLen()) { |
| 2365 | | return sema.mod.fail(&block.base, inst.base.src, "vector length mismatch: {d} and {d}", .{ |
| 2409 | return sema.mod.fail(&block.base, src, "vector length mismatch: {d} and {d}", .{ |
| 2366 | 2410 | lhs.ty.arrayLen(), |
| 2367 | 2411 | rhs.ty.arrayLen(), |
| 2368 | 2412 | }); |
| 2369 | 2413 | } |
| 2370 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement support for vectors in zirBinOp", .{}); |
| 2414 | return sema.mod.fail(&block.base, src, "TODO implement support for vectors in zirBinOp", .{}); |
| 2371 | 2415 | } else if (lhs.ty.zigTypeTag() == .Vector or rhs.ty.zigTypeTag() == .Vector) { |
| 2372 | | return sema.mod.fail(&block.base, inst.base.src, "mixed scalar and vector operands to binary expression: '{}' and '{}'", .{ |
| 2416 | return sema.mod.fail(&block.base, src, "mixed scalar and vector operands to binary expression: '{}' and '{}'", .{ |
| 2373 | 2417 | lhs.ty, |
| 2374 | 2418 | rhs.ty, |
| 2375 | 2419 | }); |
| ... | ... | @@ -2379,22 +2423,22 @@ fn zirArithmetic(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr |
| 2379 | 2423 | const is_float = scalar_tag == .Float or scalar_tag == .ComptimeFloat; |
| 2380 | 2424 | |
| 2381 | 2425 | if (!is_int and !(is_float and floatOpAllowed(inst.base.tag))) { |
| 2382 | | return sema.mod.fail(&block.base, inst.base.src, "invalid operands to binary expression: '{s}' and '{s}'", .{ @tagName(lhs.ty.zigTypeTag()), @tagName(rhs.ty.zigTypeTag()) }); |
| 2426 | return sema.mod.fail(&block.base, src, "invalid operands to binary expression: '{s}' and '{s}'", .{ @tagName(lhs.ty.zigTypeTag()), @tagName(rhs.ty.zigTypeTag()) }); |
| 2383 | 2427 | } |
| 2384 | 2428 | |
| 2385 | 2429 | if (casted_lhs.value()) |lhs_val| { |
| 2386 | 2430 | if (casted_rhs.value()) |rhs_val| { |
| 2387 | 2431 | if (lhs_val.isUndef() or rhs_val.isUndef()) { |
| 2388 | | return sema.mod.constInst(scope, inst.base.src, .{ |
| 2432 | return sema.mod.constInst(sema.arena, src, .{ |
| 2389 | 2433 | .ty = resolved_type, |
| 2390 | 2434 | .val = Value.initTag(.undef), |
| 2391 | 2435 | }); |
| 2392 | 2436 | } |
| 2393 | | return analyzeInstComptimeOp(mod, scope, scalar_type, inst, lhs_val, rhs_val); |
| 2437 | return sema.analyzeInstComptimeOp(block, scalar_type, inst, lhs_val, rhs_val); |
| 2394 | 2438 | } |
| 2395 | 2439 | } |
| 2396 | 2440 | |
| 2397 | | try sema.requireRuntimeBlock(block, inst.base.src); |
| 2441 | try sema.requireRuntimeBlock(block, src); |
| 2398 | 2442 | const ir_tag: Inst.Tag = switch (inst.base.tag) { |
| 2399 | 2443 | .add => .add, |
| 2400 | 2444 | .addwrap => .addwrap, |
| ... | ... | @@ -2402,18 +2446,27 @@ fn zirArithmetic(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr |
| 2402 | 2446 | .subwrap => .subwrap, |
| 2403 | 2447 | .mul => .mul, |
| 2404 | 2448 | .mulwrap => .mulwrap, |
| 2405 | | else => return sema.mod.fail(&block.base, inst.base.src, "TODO implement arithmetic for operand '{s}''", .{@tagName(inst.base.tag)}), |
| 2449 | else => return sema.mod.fail(&block.base, src, "TODO implement arithmetic for operand '{s}''", .{@tagName(inst.base.tag)}), |
| 2406 | 2450 | }; |
| 2407 | 2451 | |
| 2408 | | return mod.addBinOp(b, inst.base.src, scalar_type, ir_tag, casted_lhs, casted_rhs); |
| 2452 | return block.addBinOp(src, scalar_type, ir_tag, casted_lhs, casted_rhs); |
| 2409 | 2453 | } |
| 2410 | 2454 | |
| 2411 | 2455 | /// Analyzes operands that are known at comptime |
| 2412 | | fn analyzeInstComptimeOp(sema: *Sema, block: *Scope.Block, res_type: Type, inst: zir.Inst.Index, lhs_val: Value, rhs_val: Value) InnerError!*Inst { |
| 2456 | fn analyzeInstComptimeOp( |
| 2457 | sema: *Sema, |
| 2458 | block: *Scope.Block, |
| 2459 | res_type: Type, |
| 2460 | inst: zir.Inst.Index, |
| 2461 | lhs_val: Value, |
| 2462 | rhs_val: Value, |
| 2463 | ) InnerError!*Inst { |
| 2464 | if (true) @panic("TODO rework analyzeInstComptimeOp for zir-memory-layout"); |
| 2465 | |
| 2413 | 2466 | // incase rhs is 0, simply return lhs without doing any calculations |
| 2414 | 2467 | // TODO Once division is implemented we should throw an error when dividing by 0. |
| 2415 | 2468 | if (rhs_val.compareWithZero(.eq)) { |
| 2416 | | return sema.mod.constInst(scope, inst.base.src, .{ |
| 2469 | return sema.mod.constInst(sema.arena, inst.base.src, .{ |
| 2417 | 2470 | .ty = res_type, |
| 2418 | 2471 | .val = lhs_val, |
| 2419 | 2472 | }); |
| ... | ... | @@ -2425,14 +2478,14 @@ fn analyzeInstComptimeOp(sema: *Sema, block: *Scope.Block, res_type: Type, inst: |
| 2425 | 2478 | const val = if (is_int) |
| 2426 | 2479 | try Module.intAdd(sema.arena, lhs_val, rhs_val) |
| 2427 | 2480 | else |
| 2428 | | try mod.floatAdd(scope, res_type, inst.base.src, lhs_val, rhs_val); |
| 2481 | try Module.floatAdd(sema.arena, res_type, inst.base.src, lhs_val, rhs_val); |
| 2429 | 2482 | break :blk val; |
| 2430 | 2483 | }, |
| 2431 | 2484 | .sub => blk: { |
| 2432 | 2485 | const val = if (is_int) |
| 2433 | 2486 | try Module.intSub(sema.arena, lhs_val, rhs_val) |
| 2434 | 2487 | else |
| 2435 | | try mod.floatSub(scope, res_type, inst.base.src, lhs_val, rhs_val); |
| 2488 | try Module.floatSub(sema.arena, res_type, inst.base.src, lhs_val, rhs_val); |
| 2436 | 2489 | break :blk val; |
| 2437 | 2490 | }, |
| 2438 | 2491 | else => return sema.mod.fail(&block.base, inst.base.src, "TODO Implement arithmetic operand '{s}'", .{@tagName(inst.base.tag)}), |
| ... | ... | @@ -2440,27 +2493,27 @@ fn analyzeInstComptimeOp(sema: *Sema, block: *Scope.Block, res_type: Type, inst: |
| 2440 | 2493 | |
| 2441 | 2494 | log.debug("{s}({}, {}) result: {}", .{ @tagName(inst.base.tag), lhs_val, rhs_val, value }); |
| 2442 | 2495 | |
| 2443 | | return sema.mod.constInst(scope, inst.base.src, .{ |
| 2496 | return sema.mod.constInst(sema.arena, inst.base.src, .{ |
| 2444 | 2497 | .ty = res_type, |
| 2445 | 2498 | .val = value, |
| 2446 | 2499 | }); |
| 2447 | 2500 | } |
| 2448 | 2501 | |
| 2449 | | fn zirDerefNode(sema: *Sema, block: *Scope.Block, deref: zir.Inst.Index) InnerError!*Inst { |
| 2502 | fn zirDerefNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2450 | 2503 | const tracy = trace(@src()); |
| 2451 | 2504 | defer tracy.end(); |
| 2452 | 2505 | |
| 2453 | 2506 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; |
| 2454 | 2507 | const src = inst_data.src(); |
| 2455 | 2508 | const ptr_src: LazySrcLoc = .{ .node_offset_deref_ptr = inst_data.src_node }; |
| 2456 | | const ptr = sema.resolveInst(block, inst_data.operand); |
| 2509 | const ptr = try sema.resolveInst(inst_data.operand); |
| 2457 | 2510 | return sema.analyzeDeref(block, src, ptr, ptr_src); |
| 2458 | 2511 | } |
| 2459 | 2512 | |
| 2460 | 2513 | fn zirAsm( |
| 2461 | 2514 | sema: *Sema, |
| 2462 | 2515 | block: *Scope.Block, |
| 2463 | | assembly: zir.Inst.Index, |
| 2516 | inst: zir.Inst.Index, |
| 2464 | 2517 | is_volatile: bool, |
| 2465 | 2518 | ) InnerError!*Inst { |
| 2466 | 2519 | const tracy = trace(@src()); |
| ... | ... | @@ -2475,23 +2528,24 @@ fn zirAsm( |
| 2475 | 2528 | const asm_source = try sema.resolveConstString(block, asm_source_src, extra.data.asm_source); |
| 2476 | 2529 | |
| 2477 | 2530 | var extra_i = extra.end; |
| 2478 | | const output = if (extra.data.output != 0) blk: { |
| 2531 | const Output = struct { name: []const u8, inst: *Inst }; |
| 2532 | const output: ?Output = if (extra.data.output != 0) blk: { |
| 2479 | 2533 | const name = sema.code.nullTerminatedString(sema.code.extra[extra_i]); |
| 2480 | 2534 | extra_i += 1; |
| 2481 | | break :blk .{ |
| 2535 | break :blk Output{ |
| 2482 | 2536 | .name = name, |
| 2483 | | .inst = try sema.resolveInst(block, extra.data.output), |
| 2537 | .inst = try sema.resolveInst(extra.data.output), |
| 2484 | 2538 | }; |
| 2485 | 2539 | } else null; |
| 2486 | 2540 | |
| 2487 | | const args = try sema.arena.alloc(*Inst, extra.data.args.len); |
| 2541 | const args = try sema.arena.alloc(*Inst, extra.data.args_len); |
| 2488 | 2542 | const inputs = try sema.arena.alloc([]const u8, extra.data.args_len); |
| 2489 | 2543 | const clobbers = try sema.arena.alloc([]const u8, extra.data.clobbers_len); |
| 2490 | 2544 | |
| 2491 | 2545 | for (args) |*arg| { |
| 2492 | | const uncasted = sema.resolveInst(block, sema.code.extra[extra_i]); |
| 2546 | const uncasted = try sema.resolveInst(sema.code.extra[extra_i]); |
| 2493 | 2547 | extra_i += 1; |
| 2494 | | arg.* = try sema.coerce(block, Type.initTag(.usize), uncasted); |
| 2548 | arg.* = try sema.coerce(block, Type.initTag(.usize), uncasted, uncasted.src); |
| 2495 | 2549 | } |
| 2496 | 2550 | for (inputs) |*name| { |
| 2497 | 2551 | name.* = sema.code.nullTerminatedString(sema.code.extra[extra_i]); |
| ... | ... | @@ -2503,8 +2557,8 @@ fn zirAsm( |
| 2503 | 2557 | } |
| 2504 | 2558 | |
| 2505 | 2559 | try sema.requireRuntimeBlock(block, src); |
| 2506 | | const inst = try sema.arena.create(Inst.Assembly); |
| 2507 | | inst.* = .{ |
| 2560 | const asm_tzir = try sema.arena.create(Inst.Assembly); |
| 2561 | asm_tzir.* = .{ |
| 2508 | 2562 | .base = .{ |
| 2509 | 2563 | .tag = .assembly, |
| 2510 | 2564 | .ty = return_type, |
| ... | ... | @@ -2518,8 +2572,8 @@ fn zirAsm( |
| 2518 | 2572 | .clobbers = clobbers, |
| 2519 | 2573 | .args = args, |
| 2520 | 2574 | }; |
| 2521 | | try block.instructions.append(mod.gpa, &inst.base); |
| 2522 | | return &inst.base; |
| 2575 | try block.instructions.append(sema.gpa, &asm_tzir.base); |
| 2576 | return &asm_tzir.base; |
| 2523 | 2577 | } |
| 2524 | 2578 | |
| 2525 | 2579 | fn zirCmp( |
| ... | ... | @@ -2531,9 +2585,10 @@ fn zirCmp( |
| 2531 | 2585 | const tracy = trace(@src()); |
| 2532 | 2586 | defer tracy.end(); |
| 2533 | 2587 | |
| 2588 | const src: LazySrcLoc = .todo; |
| 2534 | 2589 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 2535 | | const lhs = sema.resolveInst(bin_inst.lhs); |
| 2536 | | const rhs = sema.resolveInst(bin_inst.rhs); |
| 2590 | const lhs = try sema.resolveInst(bin_inst.lhs); |
| 2591 | const rhs = try sema.resolveInst(bin_inst.rhs); |
| 2537 | 2592 | |
| 2538 | 2593 | const is_equality_cmp = switch (op) { |
| 2539 | 2594 | .eq, .neq => true, |
| ... | ... | @@ -2543,50 +2598,50 @@ fn zirCmp( |
| 2543 | 2598 | const rhs_ty_tag = rhs.ty.zigTypeTag(); |
| 2544 | 2599 | if (is_equality_cmp and lhs_ty_tag == .Null and rhs_ty_tag == .Null) { |
| 2545 | 2600 | // null == null, null != null |
| 2546 | | return mod.constBool(sema.arena, inst.base.src, op == .eq); |
| 2601 | return sema.mod.constBool(sema.arena, src, op == .eq); |
| 2547 | 2602 | } else if (is_equality_cmp and |
| 2548 | 2603 | ((lhs_ty_tag == .Null and rhs_ty_tag == .Optional) or |
| 2549 | 2604 | rhs_ty_tag == .Null and lhs_ty_tag == .Optional)) |
| 2550 | 2605 | { |
| 2551 | 2606 | // comparing null with optionals |
| 2552 | 2607 | const opt_operand = if (lhs_ty_tag == .Optional) lhs else rhs; |
| 2553 | | return sema.analyzeIsNull(block, inst.base.src, opt_operand, op == .neq); |
| 2608 | return sema.analyzeIsNull(block, src, opt_operand, op == .neq); |
| 2554 | 2609 | } else if (is_equality_cmp and |
| 2555 | 2610 | ((lhs_ty_tag == .Null and rhs.ty.isCPtr()) or (rhs_ty_tag == .Null and lhs.ty.isCPtr()))) |
| 2556 | 2611 | { |
| 2557 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement C pointer cmp", .{}); |
| 2612 | return sema.mod.fail(&block.base, src, "TODO implement C pointer cmp", .{}); |
| 2558 | 2613 | } else if (lhs_ty_tag == .Null or rhs_ty_tag == .Null) { |
| 2559 | 2614 | const non_null_type = if (lhs_ty_tag == .Null) rhs.ty else lhs.ty; |
| 2560 | | return sema.mod.fail(&block.base, inst.base.src, "comparison of '{}' with null", .{non_null_type}); |
| 2615 | return sema.mod.fail(&block.base, src, "comparison of '{}' with null", .{non_null_type}); |
| 2561 | 2616 | } else if (is_equality_cmp and |
| 2562 | 2617 | ((lhs_ty_tag == .EnumLiteral and rhs_ty_tag == .Union) or |
| 2563 | 2618 | (rhs_ty_tag == .EnumLiteral and lhs_ty_tag == .Union))) |
| 2564 | 2619 | { |
| 2565 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement equality comparison between a union's tag value and an enum literal", .{}); |
| 2620 | return sema.mod.fail(&block.base, src, "TODO implement equality comparison between a union's tag value and an enum literal", .{}); |
| 2566 | 2621 | } else if (lhs_ty_tag == .ErrorSet and rhs_ty_tag == .ErrorSet) { |
| 2567 | 2622 | if (!is_equality_cmp) { |
| 2568 | | return sema.mod.fail(&block.base, inst.base.src, "{s} operator not allowed for errors", .{@tagName(op)}); |
| 2623 | return sema.mod.fail(&block.base, src, "{s} operator not allowed for errors", .{@tagName(op)}); |
| 2569 | 2624 | } |
| 2570 | 2625 | if (rhs.value()) |rval| { |
| 2571 | 2626 | if (lhs.value()) |lval| { |
| 2572 | 2627 | // TODO optimisation oppurtunity: evaluate if std.mem.eql is faster with the names, or calling to Module.getErrorValue to get the values and then compare them is faster |
| 2573 | | return mod.constBool(sema.arena, inst.base.src, std.mem.eql(u8, lval.castTag(.@"error").?.data.name, rval.castTag(.@"error").?.data.name) == (op == .eq)); |
| 2628 | return sema.mod.constBool(sema.arena, src, std.mem.eql(u8, lval.castTag(.@"error").?.data.name, rval.castTag(.@"error").?.data.name) == (op == .eq)); |
| 2574 | 2629 | } |
| 2575 | 2630 | } |
| 2576 | | try sema.requireRuntimeBlock(block, inst.base.src); |
| 2577 | | return mod.addBinOp(b, inst.base.src, Type.initTag(.bool), if (op == .eq) .cmp_eq else .cmp_neq, lhs, rhs); |
| 2631 | try sema.requireRuntimeBlock(block, src); |
| 2632 | return block.addBinOp(src, Type.initTag(.bool), if (op == .eq) .cmp_eq else .cmp_neq, lhs, rhs); |
| 2578 | 2633 | } else if (lhs.ty.isNumeric() and rhs.ty.isNumeric()) { |
| 2579 | 2634 | // This operation allows any combination of integer and float types, regardless of the |
| 2580 | 2635 | // signed-ness, comptime-ness, and bit-width. So peer type resolution is incorrect for |
| 2581 | 2636 | // numeric types. |
| 2582 | | return mod.cmpNumeric(scope, inst.base.src, lhs, rhs, op); |
| 2637 | return sema.cmpNumeric(block, src, lhs, rhs, op); |
| 2583 | 2638 | } else if (lhs_ty_tag == .Type and rhs_ty_tag == .Type) { |
| 2584 | 2639 | if (!is_equality_cmp) { |
| 2585 | | return sema.mod.fail(&block.base, inst.base.src, "{s} operator not allowed for types", .{@tagName(op)}); |
| 2640 | return sema.mod.fail(&block.base, src, "{s} operator not allowed for types", .{@tagName(op)}); |
| 2586 | 2641 | } |
| 2587 | | return mod.constBool(sema.arena, inst.base.src, lhs.value().?.eql(rhs.value().?) == (op == .eq)); |
| 2642 | return sema.mod.constBool(sema.arena, src, lhs.value().?.eql(rhs.value().?) == (op == .eq)); |
| 2588 | 2643 | } |
| 2589 | | return sema.mod.fail(&block.base, inst.base.src, "TODO implement more cmp analysis", .{}); |
| 2644 | return sema.mod.fail(&block.base, src, "TODO implement more cmp analysis", .{}); |
| 2590 | 2645 | } |
| 2591 | 2646 | |
| 2592 | 2647 | fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -2594,7 +2649,7 @@ fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError! |
| 2594 | 2649 | defer tracy.end(); |
| 2595 | 2650 | |
| 2596 | 2651 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 2597 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 2652 | const operand = try sema.resolveInst(inst_data.operand); |
| 2598 | 2653 | return sema.mod.constType(sema.arena, inst_data.src(), operand.ty); |
| 2599 | 2654 | } |
| 2600 | 2655 | |
| ... | ... | @@ -2606,18 +2661,14 @@ fn zirTypeofPeer(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr |
| 2606 | 2661 | const src = inst_data.src(); |
| 2607 | 2662 | const extra = sema.code.extraData(zir.Inst.MultiOp, inst_data.payload_index); |
| 2608 | 2663 | |
| 2609 | | const inst_list = try mod.gpa.alloc(*ir.Inst, extra.data.operands_len); |
| 2610 | | defer mod.gpa.free(inst_list); |
| 2611 | | |
| 2612 | | const src_list = try mod.gpa.alloc(LazySrcLoc, extra.data.operands_len); |
| 2613 | | defer mod.gpa.free(src_list); |
| 2664 | const inst_list = try sema.gpa.alloc(*ir.Inst, extra.data.operands_len); |
| 2665 | defer sema.gpa.free(inst_list); |
| 2614 | 2666 | |
| 2615 | 2667 | for (sema.code.extra[extra.end..][0..extra.data.operands_len]) |arg_ref, i| { |
| 2616 | | inst_list[i] = sema.resolveInst(block, arg_ref); |
| 2617 | | src_list[i] = .{ .node_offset_builtin_call_argn = inst_data.src_node }; |
| 2668 | inst_list[i] = try sema.resolveInst(arg_ref); |
| 2618 | 2669 | } |
| 2619 | 2670 | |
| 2620 | | const result_type = try sema.resolvePeerTypes(block, inst_list, src_list); |
| 2671 | const result_type = try sema.resolvePeerTypes(block, inst_list); |
| 2621 | 2672 | return sema.mod.constType(sema.arena, src, result_type); |
| 2622 | 2673 | } |
| 2623 | 2674 | |
| ... | ... | @@ -2627,12 +2678,12 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 2627 | 2678 | |
| 2628 | 2679 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 2629 | 2680 | const src = inst_data.src(); |
| 2630 | | const uncasted_operand = sema.resolveInst(block, inst_data.operand); |
| 2681 | const uncasted_operand = try sema.resolveInst(inst_data.operand); |
| 2631 | 2682 | |
| 2632 | 2683 | const bool_type = Type.initTag(.bool); |
| 2633 | | const operand = try sema.coerce(scope, bool_type, uncasted_operand); |
| 2634 | | if (try mod.resolveDefinedValue(scope, operand)) |val| { |
| 2635 | | return mod.constBool(sema.arena, src, !val.toBool()); |
| 2684 | const operand = try sema.coerce(block, bool_type, uncasted_operand, uncasted_operand.src); |
| 2685 | if (try sema.resolveDefinedValue(block, src, operand)) |val| { |
| 2686 | return sema.mod.constBool(sema.arena, src, !val.toBool()); |
| 2636 | 2687 | } |
| 2637 | 2688 | try sema.requireRuntimeBlock(block, src); |
| 2638 | 2689 | return block.addUnOp(src, bool_type, .not, operand); |
| ... | ... | @@ -2647,25 +2698,26 @@ fn zirBoolOp( |
| 2647 | 2698 | const tracy = trace(@src()); |
| 2648 | 2699 | defer tracy.end(); |
| 2649 | 2700 | |
| 2701 | const src: LazySrcLoc = .unneeded; |
| 2650 | 2702 | const bool_type = Type.initTag(.bool); |
| 2651 | 2703 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 2652 | | const uncasted_lhs = sema.resolveInst(bin_inst.lhs); |
| 2653 | | const lhs = try sema.coerce(scope, bool_type, uncasted_lhs); |
| 2654 | | const uncasted_rhs = sema.resolveInst(bin_inst.rhs); |
| 2655 | | const rhs = try sema.coerce(scope, bool_type, uncasted_rhs); |
| 2704 | const uncasted_lhs = try sema.resolveInst(bin_inst.lhs); |
| 2705 | const lhs = try sema.coerce(block, bool_type, uncasted_lhs, uncasted_lhs.src); |
| 2706 | const uncasted_rhs = try sema.resolveInst(bin_inst.rhs); |
| 2707 | const rhs = try sema.coerce(block, bool_type, uncasted_rhs, uncasted_rhs.src); |
| 2656 | 2708 | |
| 2657 | 2709 | if (lhs.value()) |lhs_val| { |
| 2658 | 2710 | if (rhs.value()) |rhs_val| { |
| 2659 | 2711 | if (is_bool_or) { |
| 2660 | | return mod.constBool(sema.arena, inst.base.src, lhs_val.toBool() or rhs_val.toBool()); |
| 2712 | return sema.mod.constBool(sema.arena, src, lhs_val.toBool() or rhs_val.toBool()); |
| 2661 | 2713 | } else { |
| 2662 | | return mod.constBool(sema.arena, inst.base.src, lhs_val.toBool() and rhs_val.toBool()); |
| 2714 | return sema.mod.constBool(sema.arena, src, lhs_val.toBool() and rhs_val.toBool()); |
| 2663 | 2715 | } |
| 2664 | 2716 | } |
| 2665 | 2717 | } |
| 2666 | | try sema.requireRuntimeBlock(block, inst.base.src); |
| 2718 | try sema.requireRuntimeBlock(block, src); |
| 2667 | 2719 | const tag: ir.Inst.Tag = if (is_bool_or) .bool_or else .bool_and; |
| 2668 | | return mod.addBinOp(b, inst.base.src, bool_type, tag, lhs, rhs); |
| 2720 | return block.addBinOp(src, bool_type, tag, lhs, rhs); |
| 2669 | 2721 | } |
| 2670 | 2722 | |
| 2671 | 2723 | fn zirIsNull( |
| ... | ... | @@ -2679,7 +2731,7 @@ fn zirIsNull( |
| 2679 | 2731 | |
| 2680 | 2732 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 2681 | 2733 | const src = inst_data.src(); |
| 2682 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 2734 | const operand = try sema.resolveInst(inst_data.operand); |
| 2683 | 2735 | return sema.analyzeIsNull(block, src, operand, invert_logic); |
| 2684 | 2736 | } |
| 2685 | 2737 | |
| ... | ... | @@ -2694,7 +2746,7 @@ fn zirIsNullPtr( |
| 2694 | 2746 | |
| 2695 | 2747 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 2696 | 2748 | const src = inst_data.src(); |
| 2697 | | const ptr = sema.resolveInst(block, inst_data.operand); |
| 2749 | const ptr = try sema.resolveInst(inst_data.operand); |
| 2698 | 2750 | const loaded = try sema.analyzeDeref(block, src, ptr, src); |
| 2699 | 2751 | return sema.analyzeIsNull(block, src, loaded, invert_logic); |
| 2700 | 2752 | } |
| ... | ... | @@ -2704,8 +2756,8 @@ fn zirIsErr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!* |
| 2704 | 2756 | defer tracy.end(); |
| 2705 | 2757 | |
| 2706 | 2758 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 2707 | | const operand = sema.resolveInst(block, inst_data.operand); |
| 2708 | | return mod.analyzeIsErr(scope, inst_data.src(), operand); |
| 2759 | const operand = try sema.resolveInst(inst_data.operand); |
| 2760 | return sema.analyzeIsErr(block, inst_data.src(), operand); |
| 2709 | 2761 | } |
| 2710 | 2762 | |
| 2711 | 2763 | fn zirIsErrPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -2714,83 +2766,111 @@ fn zirIsErrPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro |
| 2714 | 2766 | |
| 2715 | 2767 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 2716 | 2768 | const src = inst_data.src(); |
| 2717 | | const ptr = sema.resolveInst(block, inst_data.operand); |
| 2769 | const ptr = try sema.resolveInst(inst_data.operand); |
| 2718 | 2770 | const loaded = try sema.analyzeDeref(block, src, ptr, src); |
| 2719 | | return mod.analyzeIsErr(scope, src, loaded); |
| 2771 | return sema.analyzeIsErr(block, src, loaded); |
| 2720 | 2772 | } |
| 2721 | 2773 | |
| 2722 | 2774 | fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2723 | 2775 | const tracy = trace(@src()); |
| 2724 | 2776 | defer tracy.end(); |
| 2725 | 2777 | |
| 2726 | | const uncasted_cond = sema.resolveInst(block, inst.positionals.condition); |
| 2727 | | const cond = try sema.coerce(scope, Type.initTag(.bool), uncasted_cond); |
| 2778 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 2779 | const src = inst_data.src(); |
| 2780 | const cond_src: LazySrcLoc = .{ .node_offset_if_cond = inst_data.src_node }; |
| 2781 | const extra = sema.code.extraData(zir.Inst.CondBr, inst_data.payload_index); |
| 2782 | |
| 2783 | const then_body = sema.code.extra[extra.end..][0..extra.data.then_body_len]; |
| 2784 | const else_body = sema.code.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| 2728 | 2785 | |
| 2729 | | if (try mod.resolveDefinedValue(scope, cond)) |cond_val| { |
| 2730 | | const body = if (cond_val.toBool()) &inst.positionals.then_body else &inst.positionals.else_body; |
| 2731 | | try sema.analyzeBody(parent_block, body.*); |
| 2732 | | return mod.constNoReturn(scope, inst.base.src); |
| 2786 | const uncasted_cond = try sema.resolveInst(extra.data.condition); |
| 2787 | const cond = try sema.coerce(parent_block, Type.initTag(.bool), uncasted_cond, cond_src); |
| 2788 | |
| 2789 | if (try sema.resolveDefinedValue(parent_block, src, cond)) |cond_val| { |
| 2790 | const body = if (cond_val.toBool()) then_body else else_body; |
| 2791 | try sema.analyzeBody(parent_block, body); |
| 2792 | return sema.mod.constNoReturn(sema.arena, src); |
| 2733 | 2793 | } |
| 2734 | 2794 | |
| 2735 | 2795 | var true_block: Scope.Block = .{ |
| 2736 | 2796 | .parent = parent_block, |
| 2737 | | .inst_table = parent_block.inst_table, |
| 2738 | | .func = parent_block.func, |
| 2739 | | .owner_decl = parent_block.owner_decl, |
| 2797 | .sema = sema, |
| 2740 | 2798 | .src_decl = parent_block.src_decl, |
| 2741 | 2799 | .instructions = .{}, |
| 2742 | | .arena = sema.arena, |
| 2743 | 2800 | .inlining = parent_block.inlining, |
| 2744 | 2801 | .is_comptime = parent_block.is_comptime, |
| 2745 | | .branch_quota = parent_block.branch_quota, |
| 2746 | 2802 | }; |
| 2747 | | defer true_block.instructions.deinit(mod.gpa); |
| 2748 | | try sema.analyzeBody(&true_block, inst.positionals.then_body); |
| 2803 | defer true_block.instructions.deinit(sema.gpa); |
| 2804 | try sema.analyzeBody(&true_block, then_body); |
| 2749 | 2805 | |
| 2750 | 2806 | var false_block: Scope.Block = .{ |
| 2751 | 2807 | .parent = parent_block, |
| 2752 | | .inst_table = parent_block.inst_table, |
| 2753 | | .func = parent_block.func, |
| 2754 | | .owner_decl = parent_block.owner_decl, |
| 2808 | .sema = sema, |
| 2755 | 2809 | .src_decl = parent_block.src_decl, |
| 2756 | 2810 | .instructions = .{}, |
| 2757 | | .arena = sema.arena, |
| 2758 | 2811 | .inlining = parent_block.inlining, |
| 2759 | 2812 | .is_comptime = parent_block.is_comptime, |
| 2760 | | .branch_quota = parent_block.branch_quota, |
| 2761 | 2813 | }; |
| 2762 | | defer false_block.instructions.deinit(mod.gpa); |
| 2763 | | try sema.analyzeBody(&false_block, inst.positionals.else_body); |
| 2814 | defer false_block.instructions.deinit(sema.gpa); |
| 2815 | try sema.analyzeBody(&false_block, else_body); |
| 2764 | 2816 | |
| 2765 | | const then_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, true_block.instructions.items) }; |
| 2766 | | const else_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, false_block.instructions.items) }; |
| 2767 | | return mod.addCondBr(parent_block, inst.base.src, cond, then_body, else_body); |
| 2817 | const tzir_then_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, true_block.instructions.items) }; |
| 2818 | const tzir_else_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, false_block.instructions.items) }; |
| 2819 | return parent_block.addCondBr(src, cond, tzir_then_body, tzir_else_body); |
| 2768 | 2820 | } |
| 2769 | 2821 | |
| 2770 | 2822 | fn zirUnreachable( |
| 2771 | 2823 | sema: *Sema, |
| 2772 | 2824 | block: *Scope.Block, |
| 2773 | | zir_index: zir.Inst.Index, |
| 2825 | inst: zir.Inst.Index, |
| 2774 | 2826 | safety_check: bool, |
| 2775 | 2827 | ) InnerError!*Inst { |
| 2776 | 2828 | const tracy = trace(@src()); |
| 2777 | 2829 | defer tracy.end(); |
| 2778 | 2830 | |
| 2779 | | try sema.requireRuntimeBlock(block, zir_index.base.src); |
| 2831 | const src_node = sema.code.instructions.items(.data)[inst].node; |
| 2832 | const src: LazySrcLoc = .{ .node_offset = src_node }; |
| 2833 | try sema.requireRuntimeBlock(block, src); |
| 2780 | 2834 | // TODO Add compile error for @optimizeFor occurring too late in a scope. |
| 2781 | 2835 | if (safety_check and block.wantSafety()) { |
| 2782 | | return mod.safetyPanic(b, zir_index.base.src, .unreach); |
| 2836 | return sema.safetyPanic(block, src, .unreach); |
| 2783 | 2837 | } else { |
| 2784 | | return block.addNoOp(zir_index.base.src, Type.initTag(.noreturn), .unreach); |
| 2838 | return block.addNoOp(src, Type.initTag(.noreturn), .unreach); |
| 2785 | 2839 | } |
| 2786 | 2840 | } |
| 2787 | 2841 | |
| 2788 | | fn zirRetTok(sema: *Sema, block: *Scope.Block, zir_inst: zir.Inst.Index) InnerError!*Inst { |
| 2789 | | @compileError("TODO"); |
| 2842 | fn zirRetTok(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2843 | const tracy = trace(@src()); |
| 2844 | defer tracy.end(); |
| 2845 | |
| 2846 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 2847 | const operand = try sema.resolveInst(inst_data.operand); |
| 2848 | const src = inst_data.src(); |
| 2849 | |
| 2850 | return sema.analyzeRet(block, operand, src); |
| 2851 | } |
| 2852 | |
| 2853 | fn zirRetNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 2854 | const tracy = trace(@src()); |
| 2855 | defer tracy.end(); |
| 2856 | |
| 2857 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; |
| 2858 | const operand = try sema.resolveInst(inst_data.operand); |
| 2859 | const src = inst_data.src(); |
| 2860 | |
| 2861 | return sema.analyzeRet(block, operand, src); |
| 2790 | 2862 | } |
| 2791 | 2863 | |
| 2792 | | fn zirRetNode(sema: *Sema, block: *Scope.Block, zir_inst: zir.Inst.Index) InnerError!*Inst { |
| 2793 | | @compileError("TODO"); |
| 2864 | fn analyzeRet(sema: *Sema, block: *Scope.Block, operand: *Inst, src: LazySrcLoc) InnerError!*Inst { |
| 2865 | if (block.inlining) |inlining| { |
| 2866 | // We are inlining a function call; rewrite the `ret` as a `break`. |
| 2867 | try inlining.merges.results.append(sema.gpa, operand); |
| 2868 | const br = try block.addBr(src, inlining.merges.block_inst, operand); |
| 2869 | return &br.base; |
| 2870 | } |
| 2871 | |
| 2872 | try sema.requireFunctionBlock(block, src); |
| 2873 | return block.addUnOp(src, Type.initTag(.noreturn), .ret, operand); |
| 2794 | 2874 | } |
| 2795 | 2875 | |
| 2796 | 2876 | fn floatOpAllowed(tag: zir.Inst.Tag) bool { |
| ... | ... | @@ -2826,6 +2906,7 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 2826 | 2906 | const tracy = trace(@src()); |
| 2827 | 2907 | defer tracy.end(); |
| 2828 | 2908 | |
| 2909 | const src: LazySrcLoc = .unneeded; |
| 2829 | 2910 | const inst_data = sema.code.instructions.items(.data)[inst].ptr_type; |
| 2830 | 2911 | const extra = sema.code.extraData(zir.Inst.PtrType, inst_data.payload_index); |
| 2831 | 2912 | |
| ... | ... | @@ -2855,13 +2936,13 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 2855 | 2936 | break :blk try sema.resolveAlreadyCoercedInt(block, .unneeded, ref, u16); |
| 2856 | 2937 | } else 0; |
| 2857 | 2938 | |
| 2858 | | if (bit_end != 0 and bit_offset >= bit_end * 8) |
| 2859 | | return sema.mod.fail(&block.base, inst.base.src, "bit offset starts after end of host integer", .{}); |
| 2939 | if (bit_end != 0 and bit_start >= bit_end * 8) |
| 2940 | return sema.mod.fail(&block.base, src, "bit offset starts after end of host integer", .{}); |
| 2860 | 2941 | |
| 2861 | | const elem_type = try sema.resolveType(block, extra.data.elem_type); |
| 2942 | const elem_type = try sema.resolveType(block, .unneeded, extra.data.elem_type); |
| 2862 | 2943 | |
| 2863 | | const ty = try mod.ptrType( |
| 2864 | | scope, |
| 2944 | const ty = try sema.mod.ptrType( |
| 2945 | sema.arena, |
| 2865 | 2946 | elem_type, |
| 2866 | 2947 | sentinel, |
| 2867 | 2948 | abi_align, |
| ... | ... | @@ -2872,7 +2953,7 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 2872 | 2953 | inst_data.flags.is_volatile, |
| 2873 | 2954 | inst_data.size, |
| 2874 | 2955 | ); |
| 2875 | | return sema.mod.constType(sema.arena, .unneeded, ty); |
| 2956 | return sema.mod.constType(sema.arena, src, ty); |
| 2876 | 2957 | } |
| 2877 | 2958 | |
| 2878 | 2959 | fn zirAwait(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| ... | ... | @@ -2892,7 +2973,7 @@ fn requireFunctionBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void |
| 2892 | 2973 | } |
| 2893 | 2974 | |
| 2894 | 2975 | fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void { |
| 2895 | | try sema.requireFunctionBlock(scope, src); |
| 2976 | try sema.requireFunctionBlock(block, src); |
| 2896 | 2977 | if (block.is_comptime) { |
| 2897 | 2978 | return sema.mod.fail(&block.base, src, "unable to resolve comptime value", .{}); |
| 2898 | 2979 | } |
| ... | ... | @@ -2900,7 +2981,7 @@ fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void |
| 2900 | 2981 | |
| 2901 | 2982 | fn validateVarType(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) !void { |
| 2902 | 2983 | if (!ty.isValidVarType(false)) { |
| 2903 | | return mod.fail(&block.base, src, "variable of type '{}' must be const or comptime", .{ty}); |
| 2984 | return sema.mod.fail(&block.base, src, "variable of type '{}' must be const or comptime", .{ty}); |
| 2904 | 2985 | } |
| 2905 | 2986 | } |
| 2906 | 2987 | |
| ... | ... | @@ -2939,20 +3020,16 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id: |
| 2939 | 3020 | |
| 2940 | 3021 | var fail_block: Scope.Block = .{ |
| 2941 | 3022 | .parent = parent_block, |
| 2942 | | .inst_map = parent_block.inst_map, |
| 2943 | | .func = parent_block.func, |
| 2944 | | .owner_decl = parent_block.owner_decl, |
| 3023 | .sema = sema, |
| 2945 | 3024 | .src_decl = parent_block.src_decl, |
| 2946 | 3025 | .instructions = .{}, |
| 2947 | | .arena = sema.arena, |
| 2948 | 3026 | .inlining = parent_block.inlining, |
| 2949 | 3027 | .is_comptime = parent_block.is_comptime, |
| 2950 | | .branch_quota = parent_block.branch_quota, |
| 2951 | 3028 | }; |
| 2952 | 3029 | |
| 2953 | | defer fail_block.instructions.deinit(mod.gpa); |
| 3030 | defer fail_block.instructions.deinit(sema.gpa); |
| 2954 | 3031 | |
| 2955 | | _ = try mod.safetyPanic(&fail_block, ok.src, panic_id); |
| 3032 | _ = try sema.safetyPanic(&fail_block, ok.src, panic_id); |
| 2956 | 3033 | |
| 2957 | 3034 | const fail_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, fail_block.instructions.items) }; |
| 2958 | 3035 | |
| ... | ... | @@ -2969,13 +3046,13 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id: |
| 2969 | 3046 | }; |
| 2970 | 3047 | block_inst.body.instructions[0] = &condbr.base; |
| 2971 | 3048 | |
| 2972 | | try parent_block.instructions.append(mod.gpa, &block_inst.base); |
| 3049 | try parent_block.instructions.append(sema.gpa, &block_inst.base); |
| 2973 | 3050 | } |
| 2974 | 3051 | |
| 2975 | 3052 | fn safetyPanic(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, panic_id: PanicId) !*Inst { |
| 2976 | 3053 | // TODO Once we have a panic function to call, call it here instead of breakpoint. |
| 2977 | | _ = try mod.addNoOp(block, src, Type.initTag(.void), .breakpoint); |
| 2978 | | return mod.addNoOp(block, src, Type.initTag(.noreturn), .unreach); |
| 3054 | _ = try block.addNoOp(src, Type.initTag(.void), .breakpoint); |
| 3055 | return block.addNoOp(src, Type.initTag(.noreturn), .unreach); |
| 2979 | 3056 | } |
| 2980 | 3057 | |
| 2981 | 3058 | fn emitBackwardBranch(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void { |
| ... | ... | @@ -3002,16 +3079,16 @@ fn namedFieldPtr( |
| 3002 | 3079 | switch (elem_ty.zigTypeTag()) { |
| 3003 | 3080 | .Array => { |
| 3004 | 3081 | if (mem.eql(u8, field_name, "len")) { |
| 3005 | | return mod.constInst(scope, src, .{ |
| 3082 | return sema.mod.constInst(sema.arena, src, .{ |
| 3006 | 3083 | .ty = Type.initTag(.single_const_pointer_to_comptime_int), |
| 3007 | 3084 | .val = try Value.Tag.ref_val.create( |
| 3008 | | scope.arena(), |
| 3009 | | try Value.Tag.int_u64.create(scope.arena(), elem_ty.arrayLen()), |
| 3085 | sema.arena, |
| 3086 | try Value.Tag.int_u64.create(sema.arena, elem_ty.arrayLen()), |
| 3010 | 3087 | ), |
| 3011 | 3088 | }); |
| 3012 | 3089 | } else { |
| 3013 | | return mod.fail( |
| 3014 | | scope, |
| 3090 | return sema.mod.fail( |
| 3091 | &block.base, |
| 3015 | 3092 | field_name_src, |
| 3016 | 3093 | "no member named '{s}' in '{}'", |
| 3017 | 3094 | .{ field_name, elem_ty }, |
| ... | ... | @@ -3023,16 +3100,16 @@ fn namedFieldPtr( |
| 3023 | 3100 | switch (ptr_child.zigTypeTag()) { |
| 3024 | 3101 | .Array => { |
| 3025 | 3102 | if (mem.eql(u8, field_name, "len")) { |
| 3026 | | return mod.constInst(scope, src, .{ |
| 3103 | return sema.mod.constInst(sema.arena, src, .{ |
| 3027 | 3104 | .ty = Type.initTag(.single_const_pointer_to_comptime_int), |
| 3028 | 3105 | .val = try Value.Tag.ref_val.create( |
| 3029 | | scope.arena(), |
| 3030 | | try Value.Tag.int_u64.create(scope.arena(), ptr_child.arrayLen()), |
| 3106 | sema.arena, |
| 3107 | try Value.Tag.int_u64.create(sema.arena, ptr_child.arrayLen()), |
| 3031 | 3108 | ), |
| 3032 | 3109 | }); |
| 3033 | 3110 | } else { |
| 3034 | | return mod.fail( |
| 3035 | | scope, |
| 3111 | return sema.mod.fail( |
| 3112 | &block.base, |
| 3036 | 3113 | field_name_src, |
| 3037 | 3114 | "no member named '{s}' in '{}'", |
| 3038 | 3115 | .{ field_name, elem_ty }, |
| ... | ... | @@ -3043,10 +3120,10 @@ fn namedFieldPtr( |
| 3043 | 3120 | } |
| 3044 | 3121 | }, |
| 3045 | 3122 | .Type => { |
| 3046 | | _ = try sema.resolveConstValue(scope, object_ptr.src, object_ptr); |
| 3123 | _ = try sema.resolveConstValue(block, object_ptr.src, object_ptr); |
| 3047 | 3124 | const result = try sema.analyzeDeref(block, src, object_ptr, object_ptr.src); |
| 3048 | 3125 | const val = result.value().?; |
| 3049 | | const child_type = try val.toType(scope.arena()); |
| 3126 | const child_type = try val.toType(sema.arena); |
| 3050 | 3127 | switch (child_type.zigTypeTag()) { |
| 3051 | 3128 | .ErrorSet => { |
| 3052 | 3129 | var name: []const u8 = undefined; |
| ... | ... | @@ -3054,18 +3131,18 @@ fn namedFieldPtr( |
| 3054 | 3131 | if (val.castTag(.error_set)) |payload| |
| 3055 | 3132 | name = (payload.data.fields.getEntry(field_name) orelse return sema.mod.fail(&block.base, src, "no error named '{s}' in '{}'", .{ field_name, child_type })).key |
| 3056 | 3133 | else |
| 3057 | | name = (try mod.getErrorValue(field_name)).key; |
| 3134 | name = (try sema.mod.getErrorValue(field_name)).key; |
| 3058 | 3135 | |
| 3059 | 3136 | const result_type = if (child_type.tag() == .anyerror) |
| 3060 | | try Type.Tag.error_set_single.create(scope.arena(), name) |
| 3137 | try Type.Tag.error_set_single.create(sema.arena, name) |
| 3061 | 3138 | else |
| 3062 | 3139 | child_type; |
| 3063 | 3140 | |
| 3064 | | return mod.constInst(scope, src, .{ |
| 3065 | | .ty = try mod.simplePtrType(scope.arena(), result_type, false, .One), |
| 3141 | return sema.mod.constInst(sema.arena, src, .{ |
| 3142 | .ty = try sema.mod.simplePtrType(sema.arena, result_type, false, .One), |
| 3066 | 3143 | .val = try Value.Tag.ref_val.create( |
| 3067 | | scope.arena(), |
| 3068 | | try Value.Tag.@"error".create(scope.arena(), .{ |
| 3144 | sema.arena, |
| 3145 | try Value.Tag.@"error".create(sema.arena, .{ |
| 3069 | 3146 | .name = name, |
| 3070 | 3147 | }), |
| 3071 | 3148 | ), |
| ... | ... | @@ -3073,12 +3150,12 @@ fn namedFieldPtr( |
| 3073 | 3150 | }, |
| 3074 | 3151 | .Struct => { |
| 3075 | 3152 | const container_scope = child_type.getContainerScope(); |
| 3076 | | if (mod.lookupDeclName(&container_scope.base, field_name)) |decl| { |
| 3153 | if (sema.mod.lookupDeclName(&container_scope.base, field_name)) |decl| { |
| 3077 | 3154 | // TODO if !decl.is_pub and inDifferentFiles() "{} is private" |
| 3078 | 3155 | return sema.analyzeDeclRef(block, src, decl); |
| 3079 | 3156 | } |
| 3080 | 3157 | |
| 3081 | | if (container_scope.file_scope == mod.root_scope) { |
| 3158 | if (container_scope.file_scope == sema.mod.root_scope) { |
| 3082 | 3159 | return sema.mod.fail(&block.base, src, "root source file has no member called '{s}'", .{field_name}); |
| 3083 | 3160 | } else { |
| 3084 | 3161 | return sema.mod.fail(&block.base, src, "container '{}' has no member called '{s}'", .{ child_type, field_name }); |
| ... | ... | @@ -3117,11 +3194,11 @@ fn elemPtr( |
| 3117 | 3194 | const index_u64 = index_val.toUnsignedInt(); |
| 3118 | 3195 | // @intCast here because it would have been impossible to construct a value that |
| 3119 | 3196 | // required a larger index. |
| 3120 | | const elem_ptr = try array_ptr_val.elemPtr(scope.arena(), @intCast(usize, index_u64)); |
| 3197 | const elem_ptr = try array_ptr_val.elemPtr(sema.arena, @intCast(usize, index_u64)); |
| 3121 | 3198 | const pointee_type = elem_ty.elemType().elemType(); |
| 3122 | 3199 | |
| 3123 | | return mod.constInst(scope, src, .{ |
| 3124 | | .ty = try Type.Tag.single_const_pointer.create(scope.arena(), pointee_type), |
| 3200 | return sema.mod.constInst(sema.arena, src, .{ |
| 3201 | .ty = try Type.Tag.single_const_pointer.create(sema.arena, pointee_type), |
| 3125 | 3202 | .val = elem_ptr, |
| 3126 | 3203 | }); |
| 3127 | 3204 | } |
| ... | ... | @@ -3131,9 +3208,15 @@ fn elemPtr( |
| 3131 | 3208 | return sema.mod.fail(&block.base, src, "TODO implement more analyze elemptr", .{}); |
| 3132 | 3209 | } |
| 3133 | 3210 | |
| 3134 | | fn coerce(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) InnerError!*Inst { |
| 3211 | fn coerce( |
| 3212 | sema: *Sema, |
| 3213 | block: *Scope.Block, |
| 3214 | dest_type: Type, |
| 3215 | inst: *Inst, |
| 3216 | inst_src: LazySrcLoc, |
| 3217 | ) InnerError!*Inst { |
| 3135 | 3218 | if (dest_type.tag() == .var_args_param) { |
| 3136 | | return sema.coerceVarArgParam(scope, inst); |
| 3219 | return sema.coerceVarArgParam(block, inst); |
| 3137 | 3220 | } |
| 3138 | 3221 | // If the types are the same, we can return the operand. |
| 3139 | 3222 | if (dest_type.eql(inst.ty)) |
| ... | ... | @@ -3141,20 +3224,20 @@ fn coerce(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) InnerE |
| 3141 | 3224 | |
| 3142 | 3225 | const in_memory_result = coerceInMemoryAllowed(dest_type, inst.ty); |
| 3143 | 3226 | if (in_memory_result == .ok) { |
| 3144 | | return sema.bitcast(scope, dest_type, inst); |
| 3227 | return sema.bitcast(block, dest_type, inst); |
| 3145 | 3228 | } |
| 3146 | 3229 | |
| 3147 | 3230 | // undefined to anything |
| 3148 | 3231 | if (inst.value()) |val| { |
| 3149 | 3232 | if (val.isUndef() or inst.ty.zigTypeTag() == .Undefined) { |
| 3150 | | return mod.constInst(scope.arena(), inst.src, .{ .ty = dest_type, .val = val }); |
| 3233 | return sema.mod.constInst(sema.arena, inst_src, .{ .ty = dest_type, .val = val }); |
| 3151 | 3234 | } |
| 3152 | 3235 | } |
| 3153 | 3236 | assert(inst.ty.zigTypeTag() != .Undefined); |
| 3154 | 3237 | |
| 3155 | 3238 | // null to ?T |
| 3156 | 3239 | if (dest_type.zigTypeTag() == .Optional and inst.ty.zigTypeTag() == .Null) { |
| 3157 | | return mod.constInst(scope.arena(), inst.src, .{ .ty = dest_type, .val = Value.initTag(.null_value) }); |
| 3240 | return sema.mod.constInst(sema.arena, inst_src, .{ .ty = dest_type, .val = Value.initTag(.null_value) }); |
| 3158 | 3241 | } |
| 3159 | 3242 | |
| 3160 | 3243 | // T to ?T |
| ... | ... | @@ -3162,15 +3245,15 @@ fn coerce(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) InnerE |
| 3162 | 3245 | var buf: Type.Payload.ElemType = undefined; |
| 3163 | 3246 | const child_type = dest_type.optionalChild(&buf); |
| 3164 | 3247 | if (child_type.eql(inst.ty)) { |
| 3165 | | return mod.wrapOptional(scope, dest_type, inst); |
| 3166 | | } else if (try sema.coerceNum(scope, child_type, inst)) |some| { |
| 3167 | | return mod.wrapOptional(scope, dest_type, some); |
| 3248 | return sema.wrapOptional(block, dest_type, inst); |
| 3249 | } else if (try sema.coerceNum(block, child_type, inst)) |some| { |
| 3250 | return sema.wrapOptional(block, dest_type, some); |
| 3168 | 3251 | } |
| 3169 | 3252 | } |
| 3170 | 3253 | |
| 3171 | 3254 | // T to E!T or E to E!T |
| 3172 | 3255 | if (dest_type.tag() == .error_union) { |
| 3173 | | return try mod.wrapErrorUnion(scope, dest_type, inst); |
| 3256 | return try sema.wrapErrorUnion(block, dest_type, inst); |
| 3174 | 3257 | } |
| 3175 | 3258 | |
| 3176 | 3259 | // Coercions where the source is a single pointer to an array. |
| ... | ... | @@ -3191,11 +3274,11 @@ fn coerce(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) InnerE |
| 3191 | 3274 | switch (dest_type.ptrSize()) { |
| 3192 | 3275 | .Slice => { |
| 3193 | 3276 | // *[N]T to []T |
| 3194 | | return sema.coerceArrayPtrToSlice(scope, dest_type, inst); |
| 3277 | return sema.coerceArrayPtrToSlice(block, dest_type, inst); |
| 3195 | 3278 | }, |
| 3196 | 3279 | .C => { |
| 3197 | 3280 | // *[N]T to [*c]T |
| 3198 | | return sema.coerceArrayPtrToMany(scope, dest_type, inst); |
| 3281 | return sema.coerceArrayPtrToMany(block, dest_type, inst); |
| 3199 | 3282 | }, |
| 3200 | 3283 | .Many => { |
| 3201 | 3284 | // *[N]T to [*]T |
| ... | ... | @@ -3203,12 +3286,12 @@ fn coerce(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) InnerE |
| 3203 | 3286 | const src_sentinel = array_type.sentinel(); |
| 3204 | 3287 | const dst_sentinel = dest_type.sentinel(); |
| 3205 | 3288 | if (src_sentinel == null and dst_sentinel == null) |
| 3206 | | return sema.coerceArrayPtrToMany(scope, dest_type, inst); |
| 3289 | return sema.coerceArrayPtrToMany(block, dest_type, inst); |
| 3207 | 3290 | |
| 3208 | 3291 | if (src_sentinel) |src_s| { |
| 3209 | 3292 | if (dst_sentinel) |dst_s| { |
| 3210 | 3293 | if (src_s.eql(dst_s)) { |
| 3211 | | return sema.coerceArrayPtrToMany(scope, dest_type, inst); |
| 3294 | return sema.coerceArrayPtrToMany(block, dest_type, inst); |
| 3212 | 3295 | } |
| 3213 | 3296 | } |
| 3214 | 3297 | } |
| ... | ... | @@ -3218,21 +3301,23 @@ fn coerce(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) InnerE |
| 3218 | 3301 | } |
| 3219 | 3302 | |
| 3220 | 3303 | // comptime known number to other number |
| 3221 | | if (try sema.coerceNum(scope, dest_type, inst)) |some| |
| 3304 | if (try sema.coerceNum(block, dest_type, inst)) |some| |
| 3222 | 3305 | return some; |
| 3223 | 3306 | |
| 3307 | const target = sema.mod.getTarget(); |
| 3308 | |
| 3224 | 3309 | // integer widening |
| 3225 | 3310 | if (inst.ty.zigTypeTag() == .Int and dest_type.zigTypeTag() == .Int) { |
| 3226 | 3311 | assert(inst.value() == null); // handled above |
| 3227 | 3312 | |
| 3228 | | const src_info = inst.ty.intInfo(mod.getTarget()); |
| 3229 | | const dst_info = dest_type.intInfo(mod.getTarget()); |
| 3313 | const src_info = inst.ty.intInfo(target); |
| 3314 | const dst_info = dest_type.intInfo(target); |
| 3230 | 3315 | if ((src_info.signedness == dst_info.signedness and dst_info.bits >= src_info.bits) or |
| 3231 | 3316 | // small enough unsigned ints can get casted to large enough signed ints |
| 3232 | 3317 | (src_info.signedness == .signed and dst_info.signedness == .unsigned and dst_info.bits > src_info.bits)) |
| 3233 | 3318 | { |
| 3234 | | try sema.requireRuntimeBlock(block, inst.src); |
| 3235 | | return mod.addUnOp(b, inst.src, dest_type, .intcast, inst); |
| 3319 | try sema.requireRuntimeBlock(block, inst_src); |
| 3320 | return block.addUnOp(inst_src, dest_type, .intcast, inst); |
| 3236 | 3321 | } |
| 3237 | 3322 | } |
| 3238 | 3323 | |
| ... | ... | @@ -3240,15 +3325,15 @@ fn coerce(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) InnerE |
| 3240 | 3325 | if (inst.ty.zigTypeTag() == .Float and dest_type.zigTypeTag() == .Float) { |
| 3241 | 3326 | assert(inst.value() == null); // handled above |
| 3242 | 3327 | |
| 3243 | | const src_bits = inst.ty.floatBits(mod.getTarget()); |
| 3244 | | const dst_bits = dest_type.floatBits(mod.getTarget()); |
| 3328 | const src_bits = inst.ty.floatBits(target); |
| 3329 | const dst_bits = dest_type.floatBits(target); |
| 3245 | 3330 | if (dst_bits >= src_bits) { |
| 3246 | | try sema.requireRuntimeBlock(block, inst.src); |
| 3247 | | return mod.addUnOp(b, inst.src, dest_type, .floatcast, inst); |
| 3331 | try sema.requireRuntimeBlock(block, inst_src); |
| 3332 | return block.addUnOp(inst_src, dest_type, .floatcast, inst); |
| 3248 | 3333 | } |
| 3249 | 3334 | } |
| 3250 | 3335 | |
| 3251 | | return sema.mod.fail(&block.base, inst.src, "expected {}, found {}", .{ dest_type, inst.ty }); |
| 3336 | return sema.mod.fail(&block.base, inst_src, "expected {}, found {}", .{ dest_type, inst.ty }); |
| 3252 | 3337 | } |
| 3253 | 3338 | |
| 3254 | 3339 | const InMemoryCoercionResult = enum { |
| ... | ... | @@ -3270,6 +3355,8 @@ fn coerceNum(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) Inn |
| 3270 | 3355 | const src_zig_tag = inst.ty.zigTypeTag(); |
| 3271 | 3356 | const dst_zig_tag = dest_type.zigTypeTag(); |
| 3272 | 3357 | |
| 3358 | const target = sema.mod.getTarget(); |
| 3359 | |
| 3273 | 3360 | if (dst_zig_tag == .ComptimeInt or dst_zig_tag == .Int) { |
| 3274 | 3361 | if (src_zig_tag == .Float or src_zig_tag == .ComptimeFloat) { |
| 3275 | 3362 | if (val.floatHasFraction()) { |
| ... | ... | @@ -3277,23 +3364,23 @@ fn coerceNum(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) Inn |
| 3277 | 3364 | } |
| 3278 | 3365 | return sema.mod.fail(&block.base, inst.src, "TODO float to int", .{}); |
| 3279 | 3366 | } else if (src_zig_tag == .Int or src_zig_tag == .ComptimeInt) { |
| 3280 | | if (!val.intFitsInType(dest_type, mod.getTarget())) { |
| 3367 | if (!val.intFitsInType(dest_type, target)) { |
| 3281 | 3368 | return sema.mod.fail(&block.base, inst.src, "type {} cannot represent integer value {}", .{ inst.ty, val }); |
| 3282 | 3369 | } |
| 3283 | | return mod.constInst(scope, inst.src, .{ .ty = dest_type, .val = val }); |
| 3370 | return sema.mod.constInst(sema.arena, inst.src, .{ .ty = dest_type, .val = val }); |
| 3284 | 3371 | } |
| 3285 | 3372 | } else if (dst_zig_tag == .ComptimeFloat or dst_zig_tag == .Float) { |
| 3286 | 3373 | if (src_zig_tag == .Float or src_zig_tag == .ComptimeFloat) { |
| 3287 | | const res = val.floatCast(scope.arena(), dest_type, mod.getTarget()) catch |err| switch (err) { |
| 3288 | | error.Overflow => return mod.fail( |
| 3289 | | scope, |
| 3374 | const res = val.floatCast(sema.arena, dest_type, target) catch |err| switch (err) { |
| 3375 | error.Overflow => return sema.mod.fail( |
| 3376 | &block.base, |
| 3290 | 3377 | inst.src, |
| 3291 | 3378 | "cast of value {} to type '{}' loses information", |
| 3292 | 3379 | .{ val, dest_type }, |
| 3293 | 3380 | ), |
| 3294 | 3381 | error.OutOfMemory => return error.OutOfMemory, |
| 3295 | 3382 | }; |
| 3296 | | return mod.constInst(scope, inst.src, .{ .ty = dest_type, .val = res }); |
| 3383 | return sema.mod.constInst(sema.arena, inst.src, .{ .ty = dest_type, .val = res }); |
| 3297 | 3384 | } else if (src_zig_tag == .Int or src_zig_tag == .ComptimeInt) { |
| 3298 | 3385 | return sema.mod.fail(&block.base, inst.src, "TODO int to float", .{}); |
| 3299 | 3386 | } |
| ... | ... | @@ -3310,12 +3397,18 @@ fn coerceVarArgParam(sema: *Sema, block: *Scope.Block, inst: *Inst) !*Inst { |
| 3310 | 3397 | return inst; |
| 3311 | 3398 | } |
| 3312 | 3399 | |
| 3313 | | fn storePtr(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ptr: *Inst, uncasted_value: *Inst) !*Inst { |
| 3400 | fn storePtr( |
| 3401 | sema: *Sema, |
| 3402 | block: *Scope.Block, |
| 3403 | src: LazySrcLoc, |
| 3404 | ptr: *Inst, |
| 3405 | uncasted_value: *Inst, |
| 3406 | ) !*Inst { |
| 3314 | 3407 | if (ptr.ty.isConstPtr()) |
| 3315 | 3408 | return sema.mod.fail(&block.base, src, "cannot assign to constant", .{}); |
| 3316 | 3409 | |
| 3317 | 3410 | const elem_ty = ptr.ty.elemType(); |
| 3318 | | const value = try sema.coerce(scope, elem_ty, uncasted_value); |
| 3411 | const value = try sema.coerce(block, elem_ty, uncasted_value, uncasted_value.src); |
| 3319 | 3412 | if (elem_ty.onePossibleValue() != null) |
| 3320 | 3413 | return sema.mod.constVoid(sema.arena, .unneeded); |
| 3321 | 3414 | |
| ... | ... | @@ -3323,23 +3416,23 @@ fn storePtr(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ptr: *Inst, uncas |
| 3323 | 3416 | // TODO handle if the element type requires comptime |
| 3324 | 3417 | |
| 3325 | 3418 | try sema.requireRuntimeBlock(block, src); |
| 3326 | | return mod.addBinOp(b, src, Type.initTag(.void), .store, ptr, value); |
| 3419 | return block.addBinOp(src, Type.initTag(.void), .store, ptr, value); |
| 3327 | 3420 | } |
| 3328 | 3421 | |
| 3329 | 3422 | fn bitcast(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) !*Inst { |
| 3330 | 3423 | if (inst.value()) |val| { |
| 3331 | 3424 | // Keep the comptime Value representation; take the new type. |
| 3332 | | return mod.constInst(scope, inst.src, .{ .ty = dest_type, .val = val }); |
| 3425 | return sema.mod.constInst(sema.arena, inst.src, .{ .ty = dest_type, .val = val }); |
| 3333 | 3426 | } |
| 3334 | 3427 | // TODO validate the type size and other compile errors |
| 3335 | 3428 | try sema.requireRuntimeBlock(block, inst.src); |
| 3336 | | return mod.addUnOp(b, inst.src, dest_type, .bitcast, inst); |
| 3429 | return block.addUnOp(inst.src, dest_type, .bitcast, inst); |
| 3337 | 3430 | } |
| 3338 | 3431 | |
| 3339 | 3432 | fn coerceArrayPtrToSlice(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) !*Inst { |
| 3340 | 3433 | if (inst.value()) |val| { |
| 3341 | 3434 | // The comptime Value representation is compatible with both types. |
| 3342 | | return mod.constInst(scope, inst.src, .{ .ty = dest_type, .val = val }); |
| 3435 | return sema.mod.constInst(sema.arena, inst.src, .{ .ty = dest_type, .val = val }); |
| 3343 | 3436 | } |
| 3344 | 3437 | return sema.mod.fail(&block.base, inst.src, "TODO implement coerceArrayPtrToSlice runtime instruction", .{}); |
| 3345 | 3438 | } |
| ... | ... | @@ -3347,7 +3440,7 @@ fn coerceArrayPtrToSlice(sema: *Sema, block: *Scope.Block, dest_type: Type, inst |
| 3347 | 3440 | fn coerceArrayPtrToMany(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) !*Inst { |
| 3348 | 3441 | if (inst.value()) |val| { |
| 3349 | 3442 | // The comptime Value representation is compatible with both types. |
| 3350 | | return mod.constInst(scope, inst.src, .{ .ty = dest_type, .val = val }); |
| 3443 | return sema.mod.constInst(sema.arena, inst.src, .{ .ty = dest_type, .val = val }); |
| 3351 | 3444 | } |
| 3352 | 3445 | return sema.mod.fail(&block.base, inst.src, "TODO implement coerceArrayPtrToMany runtime instruction", .{}); |
| 3353 | 3446 | } |
| ... | ... | @@ -3358,44 +3451,39 @@ fn analyzeDeclVal(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, decl: *Decl |
| 3358 | 3451 | } |
| 3359 | 3452 | |
| 3360 | 3453 | fn analyzeDeclRef(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, decl: *Decl) InnerError!*Inst { |
| 3361 | | const scope_decl = scope.ownerDecl().?; |
| 3362 | | try mod.declareDeclDependency(scope_decl, decl); |
| 3363 | | mod.ensureDeclAnalyzed(decl) catch |err| { |
| 3364 | | if (scope.cast(Scope.Block)) |block| { |
| 3365 | | if (block.func) |func| { |
| 3366 | | func.state = .dependency_failure; |
| 3367 | | } else { |
| 3368 | | block.owner_decl.analysis = .dependency_failure; |
| 3369 | | } |
| 3454 | try sema.mod.declareDeclDependency(sema.owner_decl, decl); |
| 3455 | sema.mod.ensureDeclAnalyzed(decl) catch |err| { |
| 3456 | if (sema.func) |func| { |
| 3457 | func.state = .dependency_failure; |
| 3370 | 3458 | } else { |
| 3371 | | scope_decl.analysis = .dependency_failure; |
| 3459 | sema.owner_decl.analysis = .dependency_failure; |
| 3372 | 3460 | } |
| 3373 | 3461 | return err; |
| 3374 | 3462 | }; |
| 3375 | 3463 | |
| 3376 | 3464 | const decl_tv = try decl.typedValue(); |
| 3377 | 3465 | if (decl_tv.val.tag() == .variable) { |
| 3378 | | return mod.analyzeVarRef(scope, src, decl_tv); |
| 3466 | return sema.analyzeVarRef(block, src, decl_tv); |
| 3379 | 3467 | } |
| 3380 | | return mod.constInst(scope.arena(), src, .{ |
| 3381 | | .ty = try mod.simplePtrType(scope.arena(), decl_tv.ty, false, .One), |
| 3382 | | .val = try Value.Tag.decl_ref.create(scope.arena(), decl), |
| 3468 | return sema.mod.constInst(sema.arena, src, .{ |
| 3469 | .ty = try sema.mod.simplePtrType(sema.arena, decl_tv.ty, false, .One), |
| 3470 | .val = try Value.Tag.decl_ref.create(sema.arena, decl), |
| 3383 | 3471 | }); |
| 3384 | 3472 | } |
| 3385 | 3473 | |
| 3386 | 3474 | fn analyzeVarRef(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, tv: TypedValue) InnerError!*Inst { |
| 3387 | 3475 | const variable = tv.val.castTag(.variable).?.data; |
| 3388 | 3476 | |
| 3389 | | const ty = try mod.simplePtrType(scope.arena(), tv.ty, variable.is_mutable, .One); |
| 3477 | const ty = try sema.mod.simplePtrType(sema.arena, tv.ty, variable.is_mutable, .One); |
| 3390 | 3478 | if (!variable.is_mutable and !variable.is_extern) { |
| 3391 | | return mod.constInst(scope.arena(), src, .{ |
| 3479 | return sema.mod.constInst(sema.arena, src, .{ |
| 3392 | 3480 | .ty = ty, |
| 3393 | | .val = try Value.Tag.ref_val.create(scope.arena(), variable.init), |
| 3481 | .val = try Value.Tag.ref_val.create(sema.arena, variable.init), |
| 3394 | 3482 | }); |
| 3395 | 3483 | } |
| 3396 | 3484 | |
| 3397 | 3485 | try sema.requireRuntimeBlock(block, src); |
| 3398 | | const inst = try b.arena.create(Inst.VarPtr); |
| 3486 | const inst = try sema.arena.create(Inst.VarPtr); |
| 3399 | 3487 | inst.* = .{ |
| 3400 | 3488 | .base = .{ |
| 3401 | 3489 | .tag = .varptr, |
| ... | ... | @@ -3404,7 +3492,7 @@ fn analyzeVarRef(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, tv: TypedVal |
| 3404 | 3492 | }, |
| 3405 | 3493 | .variable = variable, |
| 3406 | 3494 | }; |
| 3407 | | try b.instructions.append(mod.gpa, &inst.base); |
| 3495 | try block.instructions.append(sema.gpa, &inst.base); |
| 3408 | 3496 | return &inst.base; |
| 3409 | 3497 | } |
| 3410 | 3498 | |
| ... | ... | @@ -3414,12 +3502,12 @@ fn analyzeRef( |
| 3414 | 3502 | src: LazySrcLoc, |
| 3415 | 3503 | operand: *Inst, |
| 3416 | 3504 | ) InnerError!*Inst { |
| 3417 | | const ptr_type = try mod.simplePtrType(scope.arena(), operand.ty, false, .One); |
| 3505 | const ptr_type = try sema.mod.simplePtrType(sema.arena, operand.ty, false, .One); |
| 3418 | 3506 | |
| 3419 | 3507 | if (operand.value()) |val| { |
| 3420 | | return mod.constInst(scope.arena(), src, .{ |
| 3508 | return sema.mod.constInst(sema.arena, src, .{ |
| 3421 | 3509 | .ty = ptr_type, |
| 3422 | | .val = try Value.Tag.ref_val.create(scope.arena(), val), |
| 3510 | .val = try Value.Tag.ref_val.create(sema.arena, val), |
| 3423 | 3511 | }); |
| 3424 | 3512 | } |
| 3425 | 3513 | |
| ... | ... | @@ -3439,14 +3527,14 @@ fn analyzeDeref( |
| 3439 | 3527 | else => return sema.mod.fail(&block.base, ptr_src, "expected pointer, found '{}'", .{ptr.ty}), |
| 3440 | 3528 | }; |
| 3441 | 3529 | if (ptr.value()) |val| { |
| 3442 | | return mod.constInst(scope.arena(), src, .{ |
| 3530 | return sema.mod.constInst(sema.arena, src, .{ |
| 3443 | 3531 | .ty = elem_ty, |
| 3444 | | .val = try val.pointerDeref(scope.arena()), |
| 3532 | .val = try val.pointerDeref(sema.arena), |
| 3445 | 3533 | }); |
| 3446 | 3534 | } |
| 3447 | 3535 | |
| 3448 | 3536 | try sema.requireRuntimeBlock(block, src); |
| 3449 | | return mod.addUnOp(b, src, elem_ty, .load, ptr); |
| 3537 | return block.addUnOp(src, elem_ty, .load, ptr); |
| 3450 | 3538 | } |
| 3451 | 3539 | |
| 3452 | 3540 | fn analyzeIsNull( |
| ... | ... | @@ -3459,23 +3547,23 @@ fn analyzeIsNull( |
| 3459 | 3547 | if (operand.value()) |opt_val| { |
| 3460 | 3548 | const is_null = opt_val.isNull(); |
| 3461 | 3549 | const bool_value = if (invert_logic) !is_null else is_null; |
| 3462 | | return mod.constBool(sema.arena, src, bool_value); |
| 3550 | return sema.mod.constBool(sema.arena, src, bool_value); |
| 3463 | 3551 | } |
| 3464 | 3552 | try sema.requireRuntimeBlock(block, src); |
| 3465 | 3553 | const inst_tag: Inst.Tag = if (invert_logic) .is_non_null else .is_null; |
| 3466 | | return mod.addUnOp(b, src, Type.initTag(.bool), inst_tag, operand); |
| 3554 | return block.addUnOp(src, Type.initTag(.bool), inst_tag, operand); |
| 3467 | 3555 | } |
| 3468 | 3556 | |
| 3469 | 3557 | fn analyzeIsErr(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, operand: *Inst) InnerError!*Inst { |
| 3470 | 3558 | const ot = operand.ty.zigTypeTag(); |
| 3471 | | if (ot != .ErrorSet and ot != .ErrorUnion) return mod.constBool(sema.arena, src, false); |
| 3472 | | if (ot == .ErrorSet) return mod.constBool(sema.arena, src, true); |
| 3559 | if (ot != .ErrorSet and ot != .ErrorUnion) return sema.mod.constBool(sema.arena, src, false); |
| 3560 | if (ot == .ErrorSet) return sema.mod.constBool(sema.arena, src, true); |
| 3473 | 3561 | assert(ot == .ErrorUnion); |
| 3474 | 3562 | if (operand.value()) |err_union| { |
| 3475 | | return mod.constBool(sema.arena, src, err_union.getError() != null); |
| 3563 | return sema.mod.constBool(sema.arena, src, err_union.getError() != null); |
| 3476 | 3564 | } |
| 3477 | 3565 | try sema.requireRuntimeBlock(block, src); |
| 3478 | | return mod.addUnOp(b, src, Type.initTag(.bool), .is_err, operand); |
| 3566 | return block.addUnOp(src, Type.initTag(.bool), .is_err, operand); |
| 3479 | 3567 | } |
| 3480 | 3568 | |
| 3481 | 3569 | fn analyzeSlice( |
| ... | ... | @@ -3511,7 +3599,7 @@ fn analyzeSlice( |
| 3511 | 3599 | }; |
| 3512 | 3600 | |
| 3513 | 3601 | const slice_sentinel = if (sentinel_opt) |sentinel| blk: { |
| 3514 | | const casted = try sema.coerce(scope, elem_type, sentinel); |
| 3602 | const casted = try sema.coerce(block, elem_type, sentinel, sentinel.src); |
| 3515 | 3603 | break :blk try sema.resolveConstValue(block, sentinel_src, casted); |
| 3516 | 3604 | } else null; |
| 3517 | 3605 | |
| ... | ... | @@ -3531,13 +3619,13 @@ fn analyzeSlice( |
| 3531 | 3619 | array_type.sentinel() |
| 3532 | 3620 | else |
| 3533 | 3621 | slice_sentinel; |
| 3534 | | return_elem_type = try mod.arrayType(scope, len, array_sentinel, elem_type); |
| 3622 | return_elem_type = try sema.mod.arrayType(sema.arena, len, array_sentinel, elem_type); |
| 3535 | 3623 | return_ptr_size = .One; |
| 3536 | 3624 | } |
| 3537 | 3625 | } |
| 3538 | 3626 | } |
| 3539 | | const return_type = try mod.ptrType( |
| 3540 | | scope, |
| 3627 | const return_type = try sema.mod.ptrType( |
| 3628 | sema.arena, |
| 3541 | 3629 | return_elem_type, |
| 3542 | 3630 | if (end_opt == null) slice_sentinel else null, |
| 3543 | 3631 | 0, // TODO alignment |
| ... | ... | @@ -3553,24 +3641,24 @@ fn analyzeSlice( |
| 3553 | 3641 | } |
| 3554 | 3642 | |
| 3555 | 3643 | fn analyzeImport(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, target_string: []const u8) !*Scope.File { |
| 3556 | | const cur_pkg = scope.getFileScope().pkg; |
| 3644 | const cur_pkg = block.getFileScope().pkg; |
| 3557 | 3645 | const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse "."; |
| 3558 | 3646 | const found_pkg = cur_pkg.table.get(target_string); |
| 3559 | 3647 | |
| 3560 | 3648 | const resolved_path = if (found_pkg) |pkg| |
| 3561 | | try std.fs.path.resolve(mod.gpa, &[_][]const u8{ pkg.root_src_directory.path orelse ".", pkg.root_src_path }) |
| 3649 | try std.fs.path.resolve(sema.gpa, &[_][]const u8{ pkg.root_src_directory.path orelse ".", pkg.root_src_path }) |
| 3562 | 3650 | else |
| 3563 | | try std.fs.path.resolve(mod.gpa, &[_][]const u8{ cur_pkg_dir_path, target_string }); |
| 3564 | | errdefer mod.gpa.free(resolved_path); |
| 3651 | try std.fs.path.resolve(sema.gpa, &[_][]const u8{ cur_pkg_dir_path, target_string }); |
| 3652 | errdefer sema.gpa.free(resolved_path); |
| 3565 | 3653 | |
| 3566 | | if (mod.import_table.get(resolved_path)) |some| { |
| 3567 | | mod.gpa.free(resolved_path); |
| 3654 | if (sema.mod.import_table.get(resolved_path)) |some| { |
| 3655 | sema.gpa.free(resolved_path); |
| 3568 | 3656 | return some; |
| 3569 | 3657 | } |
| 3570 | 3658 | |
| 3571 | 3659 | if (found_pkg == null) { |
| 3572 | | const resolved_root_path = try std.fs.path.resolve(mod.gpa, &[_][]const u8{cur_pkg_dir_path}); |
| 3573 | | defer mod.gpa.free(resolved_root_path); |
| 3660 | const resolved_root_path = try std.fs.path.resolve(sema.gpa, &[_][]const u8{cur_pkg_dir_path}); |
| 3661 | defer sema.gpa.free(resolved_root_path); |
| 3574 | 3662 | |
| 3575 | 3663 | if (!mem.startsWith(u8, resolved_path, resolved_root_path)) { |
| 3576 | 3664 | return error.ImportOutsidePkgPath; |
| ... | ... | @@ -3578,10 +3666,10 @@ fn analyzeImport(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, target_strin |
| 3578 | 3666 | } |
| 3579 | 3667 | |
| 3580 | 3668 | // TODO Scope.Container arena for ty and sub_file_path |
| 3581 | | const file_scope = try mod.gpa.create(Scope.File); |
| 3582 | | errdefer mod.gpa.destroy(file_scope); |
| 3583 | | const struct_ty = try Type.Tag.empty_struct.create(mod.gpa, &file_scope.root_container); |
| 3584 | | errdefer mod.gpa.destroy(struct_ty.castTag(.empty_struct).?); |
| 3669 | const file_scope = try sema.gpa.create(Scope.File); |
| 3670 | errdefer sema.gpa.destroy(file_scope); |
| 3671 | const struct_ty = try Type.Tag.empty_struct.create(sema.gpa, &file_scope.root_container); |
| 3672 | errdefer sema.gpa.destroy(struct_ty.castTag(.empty_struct).?); |
| 3585 | 3673 | |
| 3586 | 3674 | file_scope.* = .{ |
| 3587 | 3675 | .sub_file_path = resolved_path, |
| ... | ... | @@ -3595,13 +3683,13 @@ fn analyzeImport(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, target_strin |
| 3595 | 3683 | .ty = struct_ty, |
| 3596 | 3684 | }, |
| 3597 | 3685 | }; |
| 3598 | | mod.analyzeContainer(&file_scope.root_container) catch |err| switch (err) { |
| 3686 | sema.mod.analyzeContainer(&file_scope.root_container) catch |err| switch (err) { |
| 3599 | 3687 | error.AnalysisFail => { |
| 3600 | | assert(mod.comp.totalErrorCount() != 0); |
| 3688 | assert(sema.mod.comp.totalErrorCount() != 0); |
| 3601 | 3689 | }, |
| 3602 | 3690 | else => |e| return e, |
| 3603 | 3691 | }; |
| 3604 | | try mod.import_table.put(mod.gpa, file_scope.sub_file_path, file_scope); |
| 3692 | try sema.mod.import_table.put(sema.gpa, file_scope.sub_file_path, file_scope); |
| 3605 | 3693 | return file_scope; |
| 3606 | 3694 | } |
| 3607 | 3695 | |
| ... | ... | @@ -3637,7 +3725,7 @@ fn cmpNumeric( |
| 3637 | 3725 | |
| 3638 | 3726 | if (lhs.value()) |lhs_val| { |
| 3639 | 3727 | if (rhs.value()) |rhs_val| { |
| 3640 | | return mod.constBool(sema.arena, src, Value.compare(lhs_val, op, rhs_val)); |
| 3728 | return sema.mod.constBool(sema.arena, src, Value.compare(lhs_val, op, rhs_val)); |
| 3641 | 3729 | } |
| 3642 | 3730 | } |
| 3643 | 3731 | |
| ... | ... | @@ -3658,6 +3746,7 @@ fn cmpNumeric( |
| 3658 | 3746 | .Float, .ComptimeFloat => true, |
| 3659 | 3747 | else => false, |
| 3660 | 3748 | }; |
| 3749 | const target = sema.mod.getTarget(); |
| 3661 | 3750 | if (lhs_is_float and rhs_is_float) { |
| 3662 | 3751 | // Implicit cast the smaller one to the larger one. |
| 3663 | 3752 | const dest_type = x: { |
| ... | ... | @@ -3666,15 +3755,15 @@ fn cmpNumeric( |
| 3666 | 3755 | } else if (rhs_ty_tag == .ComptimeFloat) { |
| 3667 | 3756 | break :x lhs.ty; |
| 3668 | 3757 | } |
| 3669 | | if (lhs.ty.floatBits(mod.getTarget()) >= rhs.ty.floatBits(mod.getTarget())) { |
| 3758 | if (lhs.ty.floatBits(target) >= rhs.ty.floatBits(target)) { |
| 3670 | 3759 | break :x lhs.ty; |
| 3671 | 3760 | } else { |
| 3672 | 3761 | break :x rhs.ty; |
| 3673 | 3762 | } |
| 3674 | 3763 | }; |
| 3675 | | const casted_lhs = try sema.coerce(scope, dest_type, lhs); |
| 3676 | | const casted_rhs = try sema.coerce(scope, dest_type, rhs); |
| 3677 | | return mod.addBinOp(b, src, dest_type, Inst.Tag.fromCmpOp(op), casted_lhs, casted_rhs); |
| 3764 | const casted_lhs = try sema.coerce(block, dest_type, lhs, lhs.src); |
| 3765 | const casted_rhs = try sema.coerce(block, dest_type, rhs, rhs.src); |
| 3766 | return block.addBinOp(src, dest_type, Inst.Tag.fromCmpOp(op), casted_lhs, casted_rhs); |
| 3678 | 3767 | } |
| 3679 | 3768 | // For mixed unsigned integer sizes, implicit cast both operands to the larger integer. |
| 3680 | 3769 | // For mixed signed and unsigned integers, implicit cast both operands to a signed |
| ... | ... | @@ -3697,16 +3786,16 @@ fn cmpNumeric( |
| 3697 | 3786 | var lhs_bits: usize = undefined; |
| 3698 | 3787 | if (lhs.value()) |lhs_val| { |
| 3699 | 3788 | if (lhs_val.isUndef()) |
| 3700 | | return mod.constUndef(scope, src, Type.initTag(.bool)); |
| 3789 | return sema.mod.constUndef(sema.arena, src, Type.initTag(.bool)); |
| 3701 | 3790 | const is_unsigned = if (lhs_is_float) x: { |
| 3702 | 3791 | var bigint_space: Value.BigIntSpace = undefined; |
| 3703 | | var bigint = try lhs_val.toBigInt(&bigint_space).toManaged(mod.gpa); |
| 3792 | var bigint = try lhs_val.toBigInt(&bigint_space).toManaged(sema.gpa); |
| 3704 | 3793 | defer bigint.deinit(); |
| 3705 | 3794 | const zcmp = lhs_val.orderAgainstZero(); |
| 3706 | 3795 | if (lhs_val.floatHasFraction()) { |
| 3707 | 3796 | switch (op) { |
| 3708 | | .eq => return mod.constBool(sema.arena, src, false), |
| 3709 | | .neq => return mod.constBool(sema.arena, src, true), |
| 3797 | .eq => return sema.mod.constBool(sema.arena, src, false), |
| 3798 | .neq => return sema.mod.constBool(sema.arena, src, true), |
| 3710 | 3799 | else => {}, |
| 3711 | 3800 | } |
| 3712 | 3801 | if (zcmp == .lt) { |
| ... | ... | @@ -3725,23 +3814,23 @@ fn cmpNumeric( |
| 3725 | 3814 | } else if (lhs_is_float) { |
| 3726 | 3815 | dest_float_type = lhs.ty; |
| 3727 | 3816 | } else { |
| 3728 | | const int_info = lhs.ty.intInfo(mod.getTarget()); |
| 3817 | const int_info = lhs.ty.intInfo(target); |
| 3729 | 3818 | lhs_bits = int_info.bits + @boolToInt(int_info.signedness == .unsigned and dest_int_is_signed); |
| 3730 | 3819 | } |
| 3731 | 3820 | |
| 3732 | 3821 | var rhs_bits: usize = undefined; |
| 3733 | 3822 | if (rhs.value()) |rhs_val| { |
| 3734 | 3823 | if (rhs_val.isUndef()) |
| 3735 | | return mod.constUndef(scope, src, Type.initTag(.bool)); |
| 3824 | return sema.mod.constUndef(sema.arena, src, Type.initTag(.bool)); |
| 3736 | 3825 | const is_unsigned = if (rhs_is_float) x: { |
| 3737 | 3826 | var bigint_space: Value.BigIntSpace = undefined; |
| 3738 | | var bigint = try rhs_val.toBigInt(&bigint_space).toManaged(mod.gpa); |
| 3827 | var bigint = try rhs_val.toBigInt(&bigint_space).toManaged(sema.gpa); |
| 3739 | 3828 | defer bigint.deinit(); |
| 3740 | 3829 | const zcmp = rhs_val.orderAgainstZero(); |
| 3741 | 3830 | if (rhs_val.floatHasFraction()) { |
| 3742 | 3831 | switch (op) { |
| 3743 | | .eq => return mod.constBool(sema.arena, src, false), |
| 3744 | | .neq => return mod.constBool(sema.arena, src, true), |
| 3832 | .eq => return sema.mod.constBool(sema.arena, src, false), |
| 3833 | .neq => return sema.mod.constBool(sema.arena, src, true), |
| 3745 | 3834 | else => {}, |
| 3746 | 3835 | } |
| 3747 | 3836 | if (zcmp == .lt) { |
| ... | ... | @@ -3760,7 +3849,7 @@ fn cmpNumeric( |
| 3760 | 3849 | } else if (rhs_is_float) { |
| 3761 | 3850 | dest_float_type = rhs.ty; |
| 3762 | 3851 | } else { |
| 3763 | | const int_info = rhs.ty.intInfo(mod.getTarget()); |
| 3852 | const int_info = rhs.ty.intInfo(target); |
| 3764 | 3853 | rhs_bits = int_info.bits + @boolToInt(int_info.signedness == .unsigned and dest_int_is_signed); |
| 3765 | 3854 | } |
| 3766 | 3855 | |
| ... | ... | @@ -3769,21 +3858,21 @@ fn cmpNumeric( |
| 3769 | 3858 | const casted_bits = std.math.cast(u16, max_bits) catch |err| switch (err) { |
| 3770 | 3859 | error.Overflow => return sema.mod.fail(&block.base, src, "{d} exceeds maximum integer bit count", .{max_bits}), |
| 3771 | 3860 | }; |
| 3772 | | break :blk try mod.makeIntType(scope, dest_int_is_signed, casted_bits); |
| 3861 | break :blk try Module.makeIntType(sema.arena, dest_int_is_signed, casted_bits); |
| 3773 | 3862 | }; |
| 3774 | | const casted_lhs = try sema.coerce(scope, dest_type, lhs); |
| 3775 | | const casted_rhs = try sema.coerce(scope, dest_type, rhs); |
| 3863 | const casted_lhs = try sema.coerce(block, dest_type, lhs, lhs.src); |
| 3864 | const casted_rhs = try sema.coerce(block, dest_type, rhs, rhs.src); |
| 3776 | 3865 | |
| 3777 | | return mod.addBinOp(b, src, Type.initTag(.bool), Inst.Tag.fromCmpOp(op), casted_lhs, casted_rhs); |
| 3866 | return block.addBinOp(src, Type.initTag(.bool), Inst.Tag.fromCmpOp(op), casted_lhs, casted_rhs); |
| 3778 | 3867 | } |
| 3779 | 3868 | |
| 3780 | 3869 | fn wrapOptional(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) !*Inst { |
| 3781 | 3870 | if (inst.value()) |val| { |
| 3782 | | return mod.constInst(scope.arena(), inst.src, .{ .ty = dest_type, .val = val }); |
| 3871 | return sema.mod.constInst(sema.arena, inst.src, .{ .ty = dest_type, .val = val }); |
| 3783 | 3872 | } |
| 3784 | 3873 | |
| 3785 | 3874 | try sema.requireRuntimeBlock(block, inst.src); |
| 3786 | | return mod.addUnOp(b, inst.src, dest_type, .wrap_optional, inst); |
| 3875 | return block.addUnOp(inst.src, dest_type, .wrap_optional, inst); |
| 3787 | 3876 | } |
| 3788 | 3877 | |
| 3789 | 3878 | fn wrapErrorUnion(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst) !*Inst { |
| ... | ... | @@ -3791,7 +3880,7 @@ fn wrapErrorUnion(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst |
| 3791 | 3880 | const err_union = dest_type.castTag(.error_union).?; |
| 3792 | 3881 | if (inst.value()) |val| { |
| 3793 | 3882 | const to_wrap = if (inst.ty.zigTypeTag() != .ErrorSet) blk: { |
| 3794 | | _ = try sema.coerce(scope, err_union.data.payload, inst); |
| 3883 | _ = try sema.coerce(block, err_union.data.payload, inst, inst.src); |
| 3795 | 3884 | break :blk val; |
| 3796 | 3885 | } else switch (err_union.data.error_set.tag()) { |
| 3797 | 3886 | .anyerror => val, |
| ... | ... | @@ -3810,11 +3899,11 @@ fn wrapErrorUnion(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst |
| 3810 | 3899 | else => unreachable, |
| 3811 | 3900 | }; |
| 3812 | 3901 | |
| 3813 | | return mod.constInst(scope.arena(), inst.src, .{ |
| 3902 | return sema.mod.constInst(sema.arena, inst.src, .{ |
| 3814 | 3903 | .ty = dest_type, |
| 3815 | 3904 | // creating a SubValue for the error_union payload |
| 3816 | 3905 | .val = try Value.Tag.error_union.create( |
| 3817 | | scope.arena(), |
| 3906 | sema.arena, |
| 3818 | 3907 | to_wrap, |
| 3819 | 3908 | ), |
| 3820 | 3909 | }); |
| ... | ... | @@ -3824,11 +3913,11 @@ fn wrapErrorUnion(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: *Inst |
| 3824 | 3913 | |
| 3825 | 3914 | // we are coercing from E to E!T |
| 3826 | 3915 | if (inst.ty.zigTypeTag() == .ErrorSet) { |
| 3827 | | var coerced = try sema.coerce(scope, err_union.data.error_set, inst); |
| 3828 | | return mod.addUnOp(b, inst.src, dest_type, .wrap_errunion_err, coerced); |
| 3916 | var coerced = try sema.coerce(block, err_union.data.error_set, inst, inst.src); |
| 3917 | return block.addUnOp(inst.src, dest_type, .wrap_errunion_err, coerced); |
| 3829 | 3918 | } else { |
| 3830 | | var coerced = try sema.coerce(scope, err_union.data.payload, inst); |
| 3831 | | return mod.addUnOp(b, inst.src, dest_type, .wrap_errunion_payload, coerced); |
| 3919 | var coerced = try sema.coerce(block, err_union.data.payload, inst, inst.src); |
| 3920 | return block.addUnOp(inst.src, dest_type, .wrap_errunion_payload, coerced); |
| 3832 | 3921 | } |
| 3833 | 3922 | } |
| 3834 | 3923 | |
| ... | ... | @@ -3839,6 +3928,8 @@ fn resolvePeerTypes(sema: *Sema, block: *Scope.Block, instructions: []*Inst) !Ty |
| 3839 | 3928 | if (instructions.len == 1) |
| 3840 | 3929 | return instructions[0].ty; |
| 3841 | 3930 | |
| 3931 | const target = sema.mod.getTarget(); |
| 3932 | |
| 3842 | 3933 | var chosen = instructions[0]; |
| 3843 | 3934 | for (instructions[1..]) |candidate| { |
| 3844 | 3935 | if (candidate.ty.eql(chosen.ty)) |
| ... | ... | @@ -3859,13 +3950,13 @@ fn resolvePeerTypes(sema: *Sema, block: *Scope.Block, instructions: []*Inst) !Ty |
| 3859 | 3950 | candidate.ty.isInt() and |
| 3860 | 3951 | chosen.ty.isSignedInt() == candidate.ty.isSignedInt()) |
| 3861 | 3952 | { |
| 3862 | | if (chosen.ty.intInfo(mod.getTarget()).bits < candidate.ty.intInfo(mod.getTarget()).bits) { |
| 3953 | if (chosen.ty.intInfo(target).bits < candidate.ty.intInfo(target).bits) { |
| 3863 | 3954 | chosen = candidate; |
| 3864 | 3955 | } |
| 3865 | 3956 | continue; |
| 3866 | 3957 | } |
| 3867 | 3958 | if (chosen.ty.isFloat() and candidate.ty.isFloat()) { |
| 3868 | | if (chosen.ty.floatBits(mod.getTarget()) < candidate.ty.floatBits(mod.getTarget())) { |
| 3959 | if (chosen.ty.floatBits(target) < candidate.ty.floatBits(target)) { |
| 3869 | 3960 | chosen = candidate; |
| 3870 | 3961 | } |
| 3871 | 3962 | continue; |