authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-15 13:36:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-20 12:19:16-07:00
log12c10139e3e0166e91d2dbb1801c2054ca12d413
treef084c36fecb08bd36eb0733263647b12ac1fbb05
parent0da66339096c21d9ca524ff7a0c11a5707b60041

Sema: finish reworking for AIR memory layout except switch


2 files changed, 332 insertions(+), 285 deletions(-)

src/Sema.zig+331-284
...@@ -276,101 +276,101 @@ pub fn analyzeBody(...@@ -276,101 +276,101 @@ pub fn analyzeBody(
276 //.switch_capture_multi_ref => try sema.zirSwitchCapture(block, inst, true, true),276 //.switch_capture_multi_ref => try sema.zirSwitchCapture(block, inst, true, true),
277 //.switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),277 //.switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),
278 //.switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),278 //.switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),
279 //.type_info => try sema.zirTypeInfo(block, inst),279 .type_info => try sema.zirTypeInfo(block, inst),
280 //.size_of => try sema.zirSizeOf(block, inst),280 .size_of => try sema.zirSizeOf(block, inst),
281 //.bit_size_of => try sema.zirBitSizeOf(block, inst),281 .bit_size_of => try sema.zirBitSizeOf(block, inst),
282 //.typeof => try sema.zirTypeof(block, inst),282 .typeof => try sema.zirTypeof(block, inst),
283 //.typeof_elem => try sema.zirTypeofElem(block, inst),283 .typeof_elem => try sema.zirTypeofElem(block, inst),
284 //.log2_int_type => try sema.zirLog2IntType(block, inst),284 .log2_int_type => try sema.zirLog2IntType(block, inst),
285 //.typeof_log2_int_type => try sema.zirTypeofLog2IntType(block, inst),285 .typeof_log2_int_type => try sema.zirTypeofLog2IntType(block, inst),
286 //.xor => try sema.zirBitwise(block, inst, .xor),286 .xor => try sema.zirBitwise(block, inst, .xor),
287 //.struct_init_empty => try sema.zirStructInitEmpty(block, inst),287 .struct_init_empty => try sema.zirStructInitEmpty(block, inst),
288 //.struct_init => try sema.zirStructInit(block, inst, false),288 .struct_init => try sema.zirStructInit(block, inst, false),
289 //.struct_init_ref => try sema.zirStructInit(block, inst, true),289 .struct_init_ref => try sema.zirStructInit(block, inst, true),
290 //.struct_init_anon => try sema.zirStructInitAnon(block, inst, false),290 .struct_init_anon => try sema.zirStructInitAnon(block, inst, false),
291 //.struct_init_anon_ref => try sema.zirStructInitAnon(block, inst, true),291 .struct_init_anon_ref => try sema.zirStructInitAnon(block, inst, true),
292 //.array_init => try sema.zirArrayInit(block, inst, false),292 .array_init => try sema.zirArrayInit(block, inst, false),
293 //.array_init_ref => try sema.zirArrayInit(block, inst, true),293 .array_init_ref => try sema.zirArrayInit(block, inst, true),
294 //.array_init_anon => try sema.zirArrayInitAnon(block, inst, false),294 .array_init_anon => try sema.zirArrayInitAnon(block, inst, false),
295 //.array_init_anon_ref => try sema.zirArrayInitAnon(block, inst, true),295 .array_init_anon_ref => try sema.zirArrayInitAnon(block, inst, true),
296 //.union_init_ptr => try sema.zirUnionInitPtr(block, inst),296 .union_init_ptr => try sema.zirUnionInitPtr(block, inst),
297 //.field_type => try sema.zirFieldType(block, inst),297 .field_type => try sema.zirFieldType(block, inst),
298 //.field_type_ref => try sema.zirFieldTypeRef(block, inst),298 .field_type_ref => try sema.zirFieldTypeRef(block, inst),
299 //.ptr_to_int => try sema.zirPtrToInt(block, inst),299 .ptr_to_int => try sema.zirPtrToInt(block, inst),
300 //.align_of => try sema.zirAlignOf(block, inst),300 .align_of => try sema.zirAlignOf(block, inst),
301 //.bool_to_int => try sema.zirBoolToInt(block, inst),301 .bool_to_int => try sema.zirBoolToInt(block, inst),
302 //.embed_file => try sema.zirEmbedFile(block, inst),302 .embed_file => try sema.zirEmbedFile(block, inst),
303 //.error_name => try sema.zirErrorName(block, inst),303 .error_name => try sema.zirErrorName(block, inst),
304 //.tag_name => try sema.zirTagName(block, inst),304 .tag_name => try sema.zirTagName(block, inst),
305 //.reify => try sema.zirReify(block, inst),305 .reify => try sema.zirReify(block, inst),
306 //.type_name => try sema.zirTypeName(block, inst),306 .type_name => try sema.zirTypeName(block, inst),
307 //.frame_type => try sema.zirFrameType(block, inst),307 .frame_type => try sema.zirFrameType(block, inst),
308 //.frame_size => try sema.zirFrameSize(block, inst),308 .frame_size => try sema.zirFrameSize(block, inst),
309 //.float_to_int => try sema.zirFloatToInt(block, inst),309 .float_to_int => try sema.zirFloatToInt(block, inst),
310 //.int_to_float => try sema.zirIntToFloat(block, inst),310 .int_to_float => try sema.zirIntToFloat(block, inst),
311 //.int_to_ptr => try sema.zirIntToPtr(block, inst),311 .int_to_ptr => try sema.zirIntToPtr(block, inst),
312 //.float_cast => try sema.zirFloatCast(block, inst),312 .float_cast => try sema.zirFloatCast(block, inst),
313 //.int_cast => try sema.zirIntCast(block, inst),313 .int_cast => try sema.zirIntCast(block, inst),
314 //.err_set_cast => try sema.zirErrSetCast(block, inst),314 .err_set_cast => try sema.zirErrSetCast(block, inst),
315 //.ptr_cast => try sema.zirPtrCast(block, inst),315 .ptr_cast => try sema.zirPtrCast(block, inst),
316 //.truncate => try sema.zirTruncate(block, inst),316 .truncate => try sema.zirTruncate(block, inst),
317 //.align_cast => try sema.zirAlignCast(block, inst),317 .align_cast => try sema.zirAlignCast(block, inst),
318 //.has_decl => try sema.zirHasDecl(block, inst),318 .has_decl => try sema.zirHasDecl(block, inst),
319 //.has_field => try sema.zirHasField(block, inst),319 .has_field => try sema.zirHasField(block, inst),
320 //.clz => try sema.zirClz(block, inst),320 .clz => try sema.zirClz(block, inst),
321 //.ctz => try sema.zirCtz(block, inst),321 .ctz => try sema.zirCtz(block, inst),
322 //.pop_count => try sema.zirPopCount(block, inst),322 .pop_count => try sema.zirPopCount(block, inst),
323 //.byte_swap => try sema.zirByteSwap(block, inst),323 .byte_swap => try sema.zirByteSwap(block, inst),
324 //.bit_reverse => try sema.zirBitReverse(block, inst),324 .bit_reverse => try sema.zirBitReverse(block, inst),
325 //.div_exact => try sema.zirDivExact(block, inst),325 .div_exact => try sema.zirDivExact(block, inst),
326 //.div_floor => try sema.zirDivFloor(block, inst),326 .div_floor => try sema.zirDivFloor(block, inst),
327 //.div_trunc => try sema.zirDivTrunc(block, inst),327 .div_trunc => try sema.zirDivTrunc(block, inst),
328 //.mod => try sema.zirMod(block, inst),328 .mod => try sema.zirMod(block, inst),
329 //.rem => try sema.zirRem(block, inst),329 .rem => try sema.zirRem(block, inst),
330 //.shl_exact => try sema.zirShlExact(block, inst),330 .shl_exact => try sema.zirShlExact(block, inst),
331 //.shr_exact => try sema.zirShrExact(block, inst),331 .shr_exact => try sema.zirShrExact(block, inst),
332 //.bit_offset_of => try sema.zirBitOffsetOf(block, inst),332 .bit_offset_of => try sema.zirBitOffsetOf(block, inst),
333 //.offset_of => try sema.zirOffsetOf(block, inst),333 .offset_of => try sema.zirOffsetOf(block, inst),
334 //.cmpxchg_strong => try sema.zirCmpxchg(block, inst),334 .cmpxchg_strong => try sema.zirCmpxchg(block, inst),
335 //.cmpxchg_weak => try sema.zirCmpxchg(block, inst),335 .cmpxchg_weak => try sema.zirCmpxchg(block, inst),
336 //.splat => try sema.zirSplat(block, inst),336 .splat => try sema.zirSplat(block, inst),
337 //.reduce => try sema.zirReduce(block, inst),337 .reduce => try sema.zirReduce(block, inst),
338 //.shuffle => try sema.zirShuffle(block, inst),338 .shuffle => try sema.zirShuffle(block, inst),
339 //.atomic_load => try sema.zirAtomicLoad(block, inst),339 .atomic_load => try sema.zirAtomicLoad(block, inst),
340 //.atomic_rmw => try sema.zirAtomicRmw(block, inst),340 .atomic_rmw => try sema.zirAtomicRmw(block, inst),
341 //.atomic_store => try sema.zirAtomicStore(block, inst),341 .atomic_store => try sema.zirAtomicStore(block, inst),
342 //.mul_add => try sema.zirMulAdd(block, inst),342 .mul_add => try sema.zirMulAdd(block, inst),
343 //.builtin_call => try sema.zirBuiltinCall(block, inst),343 .builtin_call => try sema.zirBuiltinCall(block, inst),
344 //.field_ptr_type => try sema.zirFieldPtrType(block, inst),344 .field_ptr_type => try sema.zirFieldPtrType(block, inst),
345 //.field_parent_ptr => try sema.zirFieldParentPtr(block, inst),345 .field_parent_ptr => try sema.zirFieldParentPtr(block, inst),
346 //.memcpy => try sema.zirMemcpy(block, inst),346 .memcpy => try sema.zirMemcpy(block, inst),
347 //.memset => try sema.zirMemset(block, inst),347 .memset => try sema.zirMemset(block, inst),
348 //.builtin_async_call => try sema.zirBuiltinAsyncCall(block, inst),348 .builtin_async_call => try sema.zirBuiltinAsyncCall(block, inst),
349 //.@"resume" => try sema.zirResume(block, inst),349 .@"resume" => try sema.zirResume(block, inst),
350 //.@"await" => try sema.zirAwait(block, inst, false),350 .@"await" => try sema.zirAwait(block, inst, false),
351 //.await_nosuspend => try sema.zirAwait(block, inst, true),351 .await_nosuspend => try sema.zirAwait(block, inst, true),
352 //.extended => try sema.zirExtended(block, inst),352 .extended => try sema.zirExtended(block, inst),
353353
354 //.sqrt => try sema.zirUnaryMath(block, inst),354 .sqrt => try sema.zirUnaryMath(block, inst),
355 //.sin => try sema.zirUnaryMath(block, inst),355 .sin => try sema.zirUnaryMath(block, inst),
356 //.cos => try sema.zirUnaryMath(block, inst),356 .cos => try sema.zirUnaryMath(block, inst),
357 //.exp => try sema.zirUnaryMath(block, inst),357 .exp => try sema.zirUnaryMath(block, inst),
358 //.exp2 => try sema.zirUnaryMath(block, inst),358 .exp2 => try sema.zirUnaryMath(block, inst),
359 //.log => try sema.zirUnaryMath(block, inst),359 .log => try sema.zirUnaryMath(block, inst),
360 //.log2 => try sema.zirUnaryMath(block, inst),360 .log2 => try sema.zirUnaryMath(block, inst),
361 //.log10 => try sema.zirUnaryMath(block, inst),361 .log10 => try sema.zirUnaryMath(block, inst),
362 //.fabs => try sema.zirUnaryMath(block, inst),362 .fabs => try sema.zirUnaryMath(block, inst),
363 //.floor => try sema.zirUnaryMath(block, inst),363 .floor => try sema.zirUnaryMath(block, inst),
364 //.ceil => try sema.zirUnaryMath(block, inst),364 .ceil => try sema.zirUnaryMath(block, inst),
365 //.trunc => try sema.zirUnaryMath(block, inst),365 .trunc => try sema.zirUnaryMath(block, inst),
366 //.round => try sema.zirUnaryMath(block, inst),366 .round => try sema.zirUnaryMath(block, inst),
367367
368 //.opaque_decl => try sema.zirOpaqueDecl(block, inst, .parent),368 .opaque_decl => try sema.zirOpaqueDecl(block, inst, .parent),
369 //.opaque_decl_anon => try sema.zirOpaqueDecl(block, inst, .anon),369 .opaque_decl_anon => try sema.zirOpaqueDecl(block, inst, .anon),
370 //.opaque_decl_func => try sema.zirOpaqueDecl(block, inst, .func),370 .opaque_decl_func => try sema.zirOpaqueDecl(block, inst, .func),
371 //.error_set_decl => try sema.zirErrorSetDecl(block, inst, .parent),371 .error_set_decl => try sema.zirErrorSetDecl(block, inst, .parent),
372 //.error_set_decl_anon => try sema.zirErrorSetDecl(block, inst, .anon),372 .error_set_decl_anon => try sema.zirErrorSetDecl(block, inst, .anon),
373 //.error_set_decl_func => try sema.zirErrorSetDecl(block, inst, .func),373 .error_set_decl_func => try sema.zirErrorSetDecl(block, inst, .func),
374374
375 .add => try sema.zirArithmetic(block, inst),375 .add => try sema.zirArithmetic(block, inst),
376 .addwrap => try sema.zirArithmetic(block, inst),376 .addwrap => try sema.zirArithmetic(block, inst),
...@@ -396,106 +396,106 @@ pub fn analyzeBody(...@@ -396,106 +396,106 @@ pub fn analyzeBody(
396 .panic => return sema.zirPanic(block, inst),396 .panic => return sema.zirPanic(block, inst),
397 // zig fmt: on397 // zig fmt: on
398398
399 //// Instructions that we know can *never* be noreturn based solely on399 // Instructions that we know can *never* be noreturn based solely on
400 //// their tag. We avoid needlessly checking if they are noreturn and400 // their tag. We avoid needlessly checking if they are noreturn and
401 //// continue the loop.401 // continue the loop.
402 //// We also know that they cannot be referenced later, so we avoid402 // We also know that they cannot be referenced later, so we avoid
403 //// putting them into the map.403 // putting them into the map.
404 //.breakpoint => {404 .breakpoint => {
405 // try sema.zirBreakpoint(block, inst);405 try sema.zirBreakpoint(block, inst);
406 // i += 1;406 i += 1;
407 // continue;407 continue;
408 //},408 },
409 //.fence => {409 .fence => {
410 // try sema.zirFence(block, inst);410 try sema.zirFence(block, inst);
411 // i += 1;411 i += 1;
412 // continue;412 continue;
413 //},413 },
414 //.dbg_stmt => {414 .dbg_stmt => {
415 // try sema.zirDbgStmt(block, inst);415 try sema.zirDbgStmt(block, inst);
416 // i += 1;416 i += 1;
417 // continue;417 continue;
418 //},418 },
419 //.ensure_err_payload_void => {419 .ensure_err_payload_void => {
420 // try sema.zirEnsureErrPayloadVoid(block, inst);420 try sema.zirEnsureErrPayloadVoid(block, inst);
421 // i += 1;421 i += 1;
422 // continue;422 continue;
423 //},423 },
424 //.ensure_result_non_error => {424 .ensure_result_non_error => {
425 // try sema.zirEnsureResultNonError(block, inst);425 try sema.zirEnsureResultNonError(block, inst);
426 // i += 1;426 i += 1;
427 // continue;427 continue;
428 //},428 },
429 //.ensure_result_used => {429 .ensure_result_used => {
430 // try sema.zirEnsureResultUsed(block, inst);430 try sema.zirEnsureResultUsed(block, inst);
431 // i += 1;431 i += 1;
432 // continue;432 continue;
433 //},433 },
434 //.set_eval_branch_quota => {434 .set_eval_branch_quota => {
435 // try sema.zirSetEvalBranchQuota(block, inst);435 try sema.zirSetEvalBranchQuota(block, inst);
436 // i += 1;436 i += 1;
437 // continue;437 continue;
438 //},438 },
439 //.store => {439 .store => {
440 // try sema.zirStore(block, inst);440 try sema.zirStore(block, inst);
441 // i += 1;441 i += 1;
442 // continue;442 continue;
443 //},443 },
444 //.store_node => {444 .store_node => {
445 // try sema.zirStoreNode(block, inst);445 try sema.zirStoreNode(block, inst);
446 // i += 1;446 i += 1;
447 // continue;447 continue;
448 //},448 },
449 //.store_to_block_ptr => {449 .store_to_block_ptr => {
450 // try sema.zirStoreToBlockPtr(block, inst);450 try sema.zirStoreToBlockPtr(block, inst);
451 // i += 1;451 i += 1;
452 // continue;452 continue;
453 //},453 },
454 //.store_to_inferred_ptr => {454 .store_to_inferred_ptr => {
455 // try sema.zirStoreToInferredPtr(block, inst);455 try sema.zirStoreToInferredPtr(block, inst);
456 // i += 1;456 i += 1;
457 // continue;457 continue;
458 //},458 },
459 //.resolve_inferred_alloc => {459 .resolve_inferred_alloc => {
460 // try sema.zirResolveInferredAlloc(block, inst);460 try sema.zirResolveInferredAlloc(block, inst);
461 // i += 1;461 i += 1;
462 // continue;462 continue;
463 //},463 },
464 //.validate_struct_init_ptr => {464 .validate_struct_init_ptr => {
465 // try sema.zirValidateStructInitPtr(block, inst);465 try sema.zirValidateStructInitPtr(block, inst);
466 // i += 1;466 i += 1;
467 // continue;467 continue;
468 //},468 },
469 //.validate_array_init_ptr => {469 .validate_array_init_ptr => {
470 // try sema.zirValidateArrayInitPtr(block, inst);470 try sema.zirValidateArrayInitPtr(block, inst);
471 // i += 1;471 i += 1;
472 // continue;472 continue;
473 //},473 },
474 //.@"export" => {474 .@"export" => {
475 // try sema.zirExport(block, inst);475 try sema.zirExport(block, inst);
476 // i += 1;476 i += 1;
477 // continue;477 continue;
478 //},478 },
479 //.set_align_stack => {479 .set_align_stack => {
480 // try sema.zirSetAlignStack(block, inst);480 try sema.zirSetAlignStack(block, inst);
481 // i += 1;481 i += 1;
482 // continue;482 continue;
483 //},483 },
484 //.set_cold => {484 .set_cold => {
485 // try sema.zirSetCold(block, inst);485 try sema.zirSetCold(block, inst);
486 // i += 1;486 i += 1;
487 // continue;487 continue;
488 //},488 },
489 //.set_float_mode => {489 .set_float_mode => {
490 // try sema.zirSetFloatMode(block, inst);490 try sema.zirSetFloatMode(block, inst);
491 // i += 1;491 i += 1;
492 // continue;492 continue;
493 //},493 },
494 //.set_runtime_safety => {494 .set_runtime_safety => {
495 // try sema.zirSetRuntimeSafety(block, inst);495 try sema.zirSetRuntimeSafety(block, inst);
496 // i += 1;496 i += 1;
497 // continue;497 continue;
498 //},498 },
499499
500 // Special case instructions to handle comptime control flow.500 // Special case instructions to handle comptime control flow.
501 .repeat_inline => {501 .repeat_inline => {
...@@ -562,7 +562,7 @@ fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -562,7 +562,7 @@ fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
562 .frame_address => return sema.zirFrameAddress( block, extended),562 .frame_address => return sema.zirFrameAddress( block, extended),
563 .alloc => return sema.zirAllocExtended( block, extended),563 .alloc => return sema.zirAllocExtended( block, extended),
564 .builtin_extern => return sema.zirBuiltinExtern( block, extended),564 .builtin_extern => return sema.zirBuiltinExtern( block, extended),
565 .@"asm" => return sema.zirAsm( block, extended),565 .@"asm" => return sema.zirAsm( block, extended, inst),
566 .typeof_peer => return sema.zirTypeofPeer( block, extended),566 .typeof_peer => return sema.zirTypeofPeer( block, extended),
567 .compile_log => return sema.zirCompileLog( block, extended),567 .compile_log => return sema.zirCompileLog( block, extended),
568 .add_with_overflow => return sema.zirOverflowArithmetic(block, extended),568 .add_with_overflow => return sema.zirOverflowArithmetic(block, extended),
...@@ -1400,11 +1400,14 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde...@@ -1400,11 +1400,14 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
1400 const inst_data = sema.code.instructions.items(.data)[inst].un_node;1400 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1401 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };1401 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };
1402 const ptr = sema.resolveInst(inst_data.operand);1402 const ptr = sema.resolveInst(inst_data.operand);
1403 const ptr_val = ptr.castTag(.constant).?.val;1403 const ptr_inst = refToIndex(ptr).?;
1404 assert(sema.air_instructions.items(.tag)[ptr_inst] == .constant);
1405 const air_datas = sema.air_instructions.items(.data);
1406 const ptr_val = sema.air_values.items[air_datas[ptr_inst].ty_pl.payload];
1404 const inferred_alloc = ptr_val.castTag(.inferred_alloc).?;1407 const inferred_alloc = ptr_val.castTag(.inferred_alloc).?;
1405 const peer_inst_list = inferred_alloc.data.stored_inst_list.items;1408 const peer_inst_list = inferred_alloc.data.stored_inst_list.items;
1406 const final_elem_ty = try sema.resolvePeerTypes(block, ty_src, peer_inst_list);1409 const final_elem_ty = try sema.resolvePeerTypes(block, ty_src, peer_inst_list);
1407 const var_is_mut = switch (ptr.ty.tag()) {1410 const var_is_mut = switch (sema.typeOf(ptr).tag()) {
1408 .inferred_alloc_const => false,1411 .inferred_alloc_const => false,
1409 .inferred_alloc_mut => true,1412 .inferred_alloc_mut => true,
1410 else => unreachable,1413 else => unreachable,
...@@ -1415,8 +1418,10 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde...@@ -1415,8 +1418,10 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
1415 const final_ptr_ty = try Module.simplePtrType(sema.arena, final_elem_ty, true, .One);1418 const final_ptr_ty = try Module.simplePtrType(sema.arena, final_elem_ty, true, .One);
14161419
1417 // Change it to a normal alloc.1420 // Change it to a normal alloc.
1418 ptr.ty = final_ptr_ty;1421 sema.air_instructions.set(ptr_inst, .{
1419 ptr.tag = .alloc;1422 .tag = .alloc,
1423 .data = .{ .ty = final_ptr_ty },
1424 });
1420}1425}
14211426
1422fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {1427fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
...@@ -1434,7 +1439,7 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Ind...@@ -1434,7 +1439,7 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Ind
1434 const field_ptr_data = sema.code.instructions.items(.data)[instrs[0]].pl_node;1439 const field_ptr_data = sema.code.instructions.items(.data)[instrs[0]].pl_node;
1435 const field_ptr_extra = sema.code.extraData(Zir.Inst.Field, field_ptr_data.payload_index).data;1440 const field_ptr_extra = sema.code.extraData(Zir.Inst.Field, field_ptr_data.payload_index).data;
1436 const object_ptr = sema.resolveInst(field_ptr_extra.lhs);1441 const object_ptr = sema.resolveInst(field_ptr_extra.lhs);
1437 break :s object_ptr.ty.elemType().castTag(.@"struct").?.data;1442 break :s sema.typeOf(object_ptr).elemType().castTag(.@"struct").?.data;
1438 };1443 };
14391444
1440 // Maps field index to field_ptr index of where it was already initialized.1445 // Maps field index to field_ptr index of where it was already initialized.
...@@ -1564,7 +1569,7 @@ fn zirStoreToBlockPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co...@@ -1564,7 +1569,7 @@ fn zirStoreToBlockPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
1564 }1569 }
1565 const ptr = sema.resolveInst(bin_inst.lhs);1570 const ptr = sema.resolveInst(bin_inst.lhs);
1566 const value = sema.resolveInst(bin_inst.rhs);1571 const value = sema.resolveInst(bin_inst.rhs);
1567 const ptr_ty = try Module.simplePtrType(sema.arena, value.ty, true, .One);1572 const ptr_ty = try Module.simplePtrType(sema.arena, sema.typeOf(value), true, .One);
1568 // TODO detect when this store should be done at compile-time. For example,1573 // TODO detect when this store should be done at compile-time. For example,
1569 // if expressions should force it when the condition is compile-time known.1574 // if expressions should force it when the condition is compile-time known.
1570 const src: LazySrcLoc = .unneeded;1575 const src: LazySrcLoc = .unneeded;
...@@ -1581,12 +1586,16 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)...@@ -1581,12 +1586,16 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
1581 const bin_inst = sema.code.instructions.items(.data)[inst].bin;1586 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
1582 const ptr = sema.resolveInst(bin_inst.lhs);1587 const ptr = sema.resolveInst(bin_inst.lhs);
1583 const value = sema.resolveInst(bin_inst.rhs);1588 const value = sema.resolveInst(bin_inst.rhs);
1584 const inferred_alloc = ptr.castTag(.constant).?.val.castTag(.inferred_alloc).?;1589 const ptr_inst = refToIndex(ptr).?;
1590 assert(sema.air_instructions.items(.tag)[ptr_inst] == .constant);
1591 const air_datas = sema.air_instructions.items(.data);
1592 const ptr_val = sema.air_values.items[air_datas[ptr_inst].ty_pl.payload];
1593 const inferred_alloc = ptr_val.castTag(.inferred_alloc).?;
1585 // Add the stored instruction to the set we will use to resolve peer types1594 // Add the stored instruction to the set we will use to resolve peer types
1586 // for the inferred allocation.1595 // for the inferred allocation.
1587 try inferred_alloc.data.stored_inst_list.append(sema.arena, value);1596 try inferred_alloc.data.stored_inst_list.append(sema.arena, value);
1588 // Create a runtime bitcast instruction with exactly the type the pointer wants.1597 // Create a runtime bitcast instruction with exactly the type the pointer wants.
1589 const ptr_ty = try Module.simplePtrType(sema.arena, value.ty, true, .One);1598 const ptr_ty = try Module.simplePtrType(sema.arena, sema.typeOf(value), true, .One);
1590 try sema.requireRuntimeBlock(block, src);1599 try sema.requireRuntimeBlock(block, src);
1591 const bitcasted_ptr = try block.addTyOp(.bitcast, ptr_ty, ptr);1600 const bitcasted_ptr = try block.addTyOp(.bitcast, ptr_ty, ptr);
1592 return sema.storePtr(block, src, bitcasted_ptr, value);1601 return sema.storePtr(block, src, bitcasted_ptr, value);
...@@ -1767,10 +1776,11 @@ fn zirCompileLog(...@@ -1767,10 +1776,11 @@ fn zirCompileLog(
1767 if (i != 0) try writer.print(", ", .{});1776 if (i != 0) try writer.print(", ", .{});
17681777
1769 const arg = sema.resolveInst(arg_ref);1778 const arg = sema.resolveInst(arg_ref);
1779 const arg_ty = sema.typeOf(arg);
1770 if (try sema.resolvePossiblyUndefinedValue(block, src, arg)) |val| {1780 if (try sema.resolvePossiblyUndefinedValue(block, src, arg)) |val| {
1771 try writer.print("@as({}, {})", .{ arg.ty, val });1781 try writer.print("@as({}, {})", .{ arg_ty, val });
1772 } else {1782 } else {
1773 try writer.print("@as({}, [runtime value])", .{arg.ty});1783 try writer.print("@as({}, [runtime value])", .{arg_ty});
1774 }1784 }
1775 }1785 }
1776 try writer.print("\n", .{});1786 try writer.print("\n", .{});
...@@ -2157,7 +2167,13 @@ fn zirDbgStmt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -2157,7 +2167,13 @@ fn zirDbgStmt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
2157 if (block.is_comptime) return;2167 if (block.is_comptime) return;
21582168
2159 const inst_data = sema.code.instructions.items(.data)[inst].dbg_stmt;2169 const inst_data = sema.code.instructions.items(.data)[inst].dbg_stmt;
2160 _ = try block.addDbgStmt(.unneeded, inst_data.line, inst_data.column);2170 _ = try block.addInst(.{
2171 .tag = .dbg_stmt,
2172 .data = .{ .dbg_stmt = .{
2173 .line = inst_data.line,
2174 .column = inst_data.column,
2175 } },
2176 });
2161}2177}
21622178
2163fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {2179fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -3263,9 +3279,10 @@ fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -3263,9 +3279,10 @@ fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
32633279
3264 const inst_data = sema.code.instructions.items(.data)[inst].un_node;3280 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
3265 const ptr = sema.resolveInst(inst_data.operand);3281 const ptr = sema.resolveInst(inst_data.operand);
3266 if (ptr.ty.zigTypeTag() != .Pointer) {3282 const ptr_ty = sema.typeOf(ptr);
3283 if (ptr_ty.zigTypeTag() != .Pointer) {
3267 const ptr_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };3284 const ptr_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
3268 return sema.mod.fail(&block.base, ptr_src, "expected pointer, found '{}'", .{ptr.ty});3285 return sema.mod.fail(&block.base, ptr_src, "expected pointer, found '{}'", .{ptr_ty});
3269 }3286 }
3270 // TODO handle known-pointer-address3287 // TODO handle known-pointer-address
3271 const src = inst_data.src();3288 const src = inst_data.src();
...@@ -3368,7 +3385,7 @@ fn zirIntCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -3368,7 +3385,7 @@ fn zirIntCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
3368 ),3385 ),
3369 }3386 }
33703387
3371 if (operand.value() != null) {3388 if (try sema.isComptimeKnown(block, operand_src, operand)) {
3372 return sema.coerce(block, dest_type, operand, operand_src);3389 return sema.coerce(block, dest_type, operand, operand_src);
3373 } else if (dest_is_comptime_int) {3390 } else if (dest_is_comptime_int) {
3374 return sema.mod.fail(&block.base, src, "unable to cast runtime value to 'comptime_int'", .{});3391 return sema.mod.fail(&block.base, src, "unable to cast runtime value to 'comptime_int'", .{});
...@@ -3426,7 +3443,7 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -3426,7 +3443,7 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
3426 ),3443 ),
3427 }3444 }
34283445
3429 if (operand.value() != null) {3446 if (try sema.isComptimeKnown(block, operand_src, operand)) {
3430 return sema.coerce(block, dest_type, operand, operand_src);3447 return sema.coerce(block, dest_type, operand, operand_src);
3431 } else if (dest_is_comptime_float) {3448 } else if (dest_is_comptime_float) {
3432 return sema.mod.fail(&block.base, src, "unable to cast runtime value to 'comptime_float'", .{});3449 return sema.mod.fail(&block.base, src, "unable to cast runtime value to 'comptime_float'", .{});
...@@ -4843,19 +4860,17 @@ fn zirAsm(...@@ -4843,19 +4860,17 @@ fn zirAsm(
4843 sema: *Sema,4860 sema: *Sema,
4844 block: *Scope.Block,4861 block: *Scope.Block,
4845 extended: Zir.Inst.Extended.InstData,4862 extended: Zir.Inst.Extended.InstData,
4863 inst: Zir.Inst.Index,
4846) CompileError!Air.Inst.Ref {4864) CompileError!Air.Inst.Ref {
4847 const tracy = trace(@src());4865 const tracy = trace(@src());
4848 defer tracy.end();4866 defer tracy.end();
48494867
4850 const extra = sema.code.extraData(Zir.Inst.Asm, extended.operand);4868 const extra = sema.code.extraData(Zir.Inst.Asm, extended.operand);
4851 const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };4869 const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };
4852 const asm_source_src: LazySrcLoc = .{ .node_offset_asm_source = extra.data.src_node };
4853 const ret_ty_src: LazySrcLoc = .{ .node_offset_asm_ret_ty = extra.data.src_node };4870 const ret_ty_src: LazySrcLoc = .{ .node_offset_asm_ret_ty = extra.data.src_node };
4854 const asm_source = try sema.resolveConstString(block, asm_source_src, extra.data.asm_source);
4855 const outputs_len = @truncate(u5, extended.small);4871 const outputs_len = @truncate(u5, extended.small);
4856 const inputs_len = @truncate(u5, extended.small >> 5);4872 const inputs_len = @truncate(u5, extended.small >> 5);
4857 const clobbers_len = @truncate(u5, extended.small >> 10);4873 const clobbers_len = @truncate(u5, extended.small >> 10);
4858 const is_volatile = @truncate(u1, extended.small >> 15) != 0;
48594874
4860 if (outputs_len > 1) {4875 if (outputs_len > 1) {
4861 return sema.mod.fail(&block.base, src, "TODO implement Sema for asm with more than 1 output", .{});4876 return sema.mod.fail(&block.base, src, "TODO implement Sema for asm with more than 1 output", .{});
...@@ -4883,7 +4898,7 @@ fn zirAsm(...@@ -4883,7 +4898,7 @@ fn zirAsm(
4883 };4898 };
4884 };4899 };
48854900
4886 const args = try sema.arena.alloc(Air.Inst.Index, inputs_len);4901 const args = try sema.arena.alloc(Air.Inst.Ref, inputs_len);
4887 const inputs = try sema.arena.alloc([]const u8, inputs_len);4902 const inputs = try sema.arena.alloc([]const u8, inputs_len);
48884903
4889 for (args) |*arg, arg_i| {4904 for (args) |*arg, arg_i| {
...@@ -4904,22 +4919,19 @@ fn zirAsm(...@@ -4904,22 +4919,19 @@ fn zirAsm(
4904 }4919 }
49054920
4906 try sema.requireRuntimeBlock(block, src);4921 try sema.requireRuntimeBlock(block, src);
4907 const asm_air = try sema.arena.create(Inst.Assembly);4922 const gpa = sema.gpa;
4908 asm_air.* = .{4923 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.Asm).Struct.fields.len + args.len);
4909 .base = .{4924 const asm_air = try block.addInst(.{
4910 .tag = .assembly,4925 .tag = .assembly,
4911 .ty = if (output) |o| o.ty else Type.initTag(.void),4926 .data = .{ .ty_pl = .{
4912 .src = src,4927 .ty = if (output) |o| try sema.addType(o.ty) else Air.Inst.Ref.void_type,
4913 },4928 .payload = sema.addExtraAssumeCapacity(Air.Asm{
4914 .asm_source = asm_source,4929 .zir_index = inst,
4915 .is_volatile = is_volatile,4930 }),
4916 .output_constraint = if (output) |o| o.constraint else null,4931 } },
4917 .inputs = inputs,4932 });
4918 .clobbers = clobbers,4933 sema.appendRefsAssumeCapacity(args);
4919 .args = args,4934 return asm_air;
4920 };
4921 try block.instructions.append(sema.gpa, &asm_air.base);
4922 return &asm_air.base;
4923}4935}
49244936
4925fn zirCmp(4937fn zirCmp(
...@@ -5153,7 +5165,7 @@ fn zirTypeofElem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile...@@ -5153,7 +5165,7 @@ fn zirTypeofElem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
5153 _ = block;5165 _ = block;
5154 const inst_data = sema.code.instructions.items(.data)[inst].un_node;5166 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5155 const operand_ptr = sema.resolveInst(inst_data.operand);5167 const operand_ptr = sema.resolveInst(inst_data.operand);
5156 const elem_ty = operand_ptr.ty.elemType();5168 const elem_ty = sema.typeOf(operand_ptr).elemType();
5157 return sema.addType(elem_ty);5169 return sema.addType(elem_ty);
5158}5170}
51595171
...@@ -5181,7 +5193,7 @@ fn zirTypeofPeer(...@@ -5181,7 +5193,7 @@ fn zirTypeofPeer(
5181 const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };5193 const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };
5182 const args = sema.code.refSlice(extra.end, extended.small);5194 const args = sema.code.refSlice(extra.end, extended.small);
51835195
5184 const inst_list = try sema.gpa.alloc(Air.Inst.Index, args.len);5196 const inst_list = try sema.gpa.alloc(Air.Inst.Ref, args.len);
5185 defer sema.gpa.free(inst_list);5197 defer sema.gpa.free(inst_list);
51865198
5187 for (args) |arg_ref, i| {5199 for (args) |arg_ref, i| {
...@@ -5623,7 +5635,7 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:...@@ -5623,7 +5635,7 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
5623 mem.set(Zir.Inst.Index, found_fields, 0);5635 mem.set(Zir.Inst.Index, found_fields, 0);
56245636
5625 // The init values to use for the struct instance.5637 // The init values to use for the struct instance.
5626 const field_inits = try gpa.alloc(Air.Inst.Index, struct_obj.fields.count());5638 const field_inits = try gpa.alloc(Air.Inst.Ref, struct_obj.fields.count());
5627 defer gpa.free(field_inits);5639 defer gpa.free(field_inits);
56285640
5629 var field_i: u32 = 0;5641 var field_i: u32 = 0;
...@@ -5692,7 +5704,7 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:...@@ -5692,7 +5704,7 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
5692 }5704 }
56935705
5694 const is_comptime = for (field_inits) |field_init| {5706 const is_comptime = for (field_inits) |field_init| {
5695 if (field_init.value() == null) {5707 if (!(try sema.isComptimeKnown(block, src, field_init))) {
5696 break false;5708 break false;
5697 }5709 }
5698 } else true;5710 } else true;
...@@ -5700,7 +5712,7 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:...@@ -5700,7 +5712,7 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
5700 if (is_comptime) {5712 if (is_comptime) {
5701 const values = try sema.arena.alloc(Value, field_inits.len);5713 const values = try sema.arena.alloc(Value, field_inits.len);
5702 for (field_inits) |field_init, i| {5714 for (field_inits) |field_init, i| {
5703 values[i] = field_init.value().?;5715 values[i] = (sema.resolvePossiblyUndefinedValue(block, src, field_init) catch unreachable).?;
5704 }5716 }
5705 return sema.addConstant(struct_ty, try Value.Tag.@"struct".create(sema.arena, values.ptr));5717 return sema.addConstant(struct_ty, try Value.Tag.@"struct".create(sema.arena, values.ptr));
5706 }5718 }
...@@ -6835,17 +6847,13 @@ fn coerce(...@@ -6835,17 +6847,13 @@ fn coerce(
6835 }6847 }
6836 assert(inst_ty.zigTypeTag() != .Undefined);6848 assert(inst_ty.zigTypeTag() != .Undefined);
68376849
6838 if (true) {
6839 @panic("TODO finish AIR memory layout rework");
6840 }
6841
6842 // T to E!T or E to E!T6850 // T to E!T or E to E!T
6843 if (dest_type.tag() == .error_union) {6851 if (dest_type.tag() == .error_union) {
6844 return try sema.wrapErrorUnion(block, dest_type, inst, inst_src);6852 return try sema.wrapErrorUnion(block, dest_type, inst, inst_src);
6845 }6853 }
68466854
6847 // comptime known number to other number6855 // comptime known number to other number
6848 if (try sema.coerceNum(block, dest_type, inst)) |some|6856 if (try sema.coerceNum(block, dest_type, inst, inst_src)) |some|
6849 return some;6857 return some;
68506858
6851 const target = mod.getTarget();6859 const target = mod.getTarget();
...@@ -6861,9 +6869,9 @@ fn coerce(...@@ -6861,9 +6869,9 @@ fn coerce(
6861 var buf: Type.Payload.ElemType = undefined;6869 var buf: Type.Payload.ElemType = undefined;
6862 const child_type = dest_type.optionalChild(&buf);6870 const child_type = dest_type.optionalChild(&buf);
6863 if (child_type.eql(inst_ty)) {6871 if (child_type.eql(inst_ty)) {
6864 return sema.wrapOptional(block, dest_type, inst);6872 return sema.wrapOptional(block, dest_type, inst, inst_src);
6865 } else if (try sema.coerceNum(block, child_type, inst)) |some| {6873 } else if (try sema.coerceNum(block, child_type, inst, inst_src)) |some| {
6866 return sema.wrapOptional(block, dest_type, some);6874 return sema.wrapOptional(block, dest_type, some, inst_src);
6867 }6875 }
6868 },6876 },
6869 .Pointer => {6877 .Pointer => {
...@@ -6885,11 +6893,11 @@ fn coerce(...@@ -6885,11 +6893,11 @@ fn coerce(
6885 switch (dest_type.ptrSize()) {6893 switch (dest_type.ptrSize()) {
6886 .Slice => {6894 .Slice => {
6887 // *[N]T to []T6895 // *[N]T to []T
6888 return sema.coerceArrayPtrToSlice(block, dest_type, inst);6896 return sema.coerceArrayPtrToSlice(block, dest_type, inst, inst_src);
6889 },6897 },
6890 .C => {6898 .C => {
6891 // *[N]T to [*c]T6899 // *[N]T to [*c]T
6892 return sema.coerceArrayPtrToMany(block, dest_type, inst);6900 return sema.coerceArrayPtrToMany(block, dest_type, inst, inst_src);
6893 },6901 },
6894 .Many => {6902 .Many => {
6895 // *[N]T to [*]T6903 // *[N]T to [*]T
...@@ -6897,12 +6905,12 @@ fn coerce(...@@ -6897,12 +6905,12 @@ fn coerce(
6897 const src_sentinel = array_type.sentinel();6905 const src_sentinel = array_type.sentinel();
6898 const dst_sentinel = dest_type.sentinel();6906 const dst_sentinel = dest_type.sentinel();
6899 if (src_sentinel == null and dst_sentinel == null)6907 if (src_sentinel == null and dst_sentinel == null)
6900 return sema.coerceArrayPtrToMany(block, dest_type, inst);6908 return sema.coerceArrayPtrToMany(block, dest_type, inst, inst_src);
69016909
6902 if (src_sentinel) |src_s| {6910 if (src_sentinel) |src_s| {
6903 if (dst_sentinel) |dst_s| {6911 if (dst_sentinel) |dst_s| {
6904 if (src_s.eql(dst_s)) {6912 if (src_s.eql(dst_s)) {
6905 return sema.coerceArrayPtrToMany(block, dest_type, inst);6913 return sema.coerceArrayPtrToMany(block, dest_type, inst, inst_src);
6906 }6914 }
6907 }6915 }
6908 }6916 }
...@@ -6914,7 +6922,7 @@ fn coerce(...@@ -6914,7 +6922,7 @@ fn coerce(
6914 .Int => {6922 .Int => {
6915 // integer widening6923 // integer widening
6916 if (inst_ty.zigTypeTag() == .Int) {6924 if (inst_ty.zigTypeTag() == .Int) {
6917 assert(inst.value() == null); // handled above6925 assert(!(try sema.isComptimeKnown(block, inst_src, inst))); // handled above
69186926
6919 const dst_info = dest_type.intInfo(target);6927 const dst_info = dest_type.intInfo(target);
6920 const src_info = inst_ty.intInfo(target);6928 const src_info = inst_ty.intInfo(target);
...@@ -6930,7 +6938,7 @@ fn coerce(...@@ -6930,7 +6938,7 @@ fn coerce(
6930 .Float => {6938 .Float => {
6931 // float widening6939 // float widening
6932 if (inst_ty.zigTypeTag() == .Float) {6940 if (inst_ty.zigTypeTag() == .Float) {
6933 assert(inst.value() == null); // handled above6941 assert(!(try sema.isComptimeKnown(block, inst_src, inst))); // handled above
69346942
6935 const src_bits = inst_ty.floatBits(target);6943 const src_bits = inst_ty.floatBits(target);
6936 const dst_bits = dest_type.floatBits(target);6944 const dst_bits = dest_type.floatBits(target);
...@@ -6991,9 +6999,16 @@ fn coerceInMemoryAllowed(dest_type: Type, src_type: Type) InMemoryCoercionResult...@@ -6991,9 +6999,16 @@ fn coerceInMemoryAllowed(dest_type: Type, src_type: Type) InMemoryCoercionResult
6991 return .no_match;6999 return .no_match;
6992}7000}
69937001
6994fn coerceNum(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.Ref) CompileError!?Air.Inst.Index {7002fn coerceNum(
6995 const val = inst.value() orelse return null;7003 sema: *Sema,
6996 const src_zig_tag = inst.ty.zigTypeTag();7004 block: *Scope.Block,
7005 dest_type: Type,
7006 inst: Air.Inst.Ref,
7007 inst_src: LazySrcLoc,
7008) CompileError!?Air.Inst.Ref {
7009 const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse return null;
7010 const inst_ty = sema.typeOf(inst);
7011 const src_zig_tag = inst_ty.zigTypeTag();
6997 const dst_zig_tag = dest_type.zigTypeTag();7012 const dst_zig_tag = dest_type.zigTypeTag();
69987013
6999 const target = sema.mod.getTarget();7014 const target = sema.mod.getTarget();
...@@ -7001,29 +7016,29 @@ fn coerceNum(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.R...@@ -7001,29 +7016,29 @@ fn coerceNum(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.R
7001 if (dst_zig_tag == .ComptimeInt or dst_zig_tag == .Int) {7016 if (dst_zig_tag == .ComptimeInt or dst_zig_tag == .Int) {
7002 if (src_zig_tag == .Float or src_zig_tag == .ComptimeFloat) {7017 if (src_zig_tag == .Float or src_zig_tag == .ComptimeFloat) {
7003 if (val.floatHasFraction()) {7018 if (val.floatHasFraction()) {
7004 return sema.mod.fail(&block.base, inst.src, "fractional component prevents float value {} from being casted to type '{}'", .{ val, inst.ty });7019 return sema.mod.fail(&block.base, inst_src, "fractional component prevents float value {} from being casted to type '{}'", .{ val, inst_ty });
7005 }7020 }
7006 return sema.mod.fail(&block.base, inst.src, "TODO float to int", .{});7021 return sema.mod.fail(&block.base, inst_src, "TODO float to int", .{});
7007 } else if (src_zig_tag == .Int or src_zig_tag == .ComptimeInt) {7022 } else if (src_zig_tag == .Int or src_zig_tag == .ComptimeInt) {
7008 if (!val.intFitsInType(dest_type, target)) {7023 if (!val.intFitsInType(dest_type, target)) {
7009 return sema.mod.fail(&block.base, inst.src, "type {} cannot represent integer value {}", .{ inst.ty, val });7024 return sema.mod.fail(&block.base, inst_src, "type {} cannot represent integer value {}", .{ inst_ty, val });
7010 }7025 }
7011 return sema.addConstant(dest_type, val);7026 return try sema.addConstant(dest_type, val);
7012 }7027 }
7013 } else if (dst_zig_tag == .ComptimeFloat or dst_zig_tag == .Float) {7028 } else if (dst_zig_tag == .ComptimeFloat or dst_zig_tag == .Float) {
7014 if (src_zig_tag == .Float or src_zig_tag == .ComptimeFloat) {7029 if (src_zig_tag == .Float or src_zig_tag == .ComptimeFloat) {
7015 const res = val.floatCast(sema.arena, dest_type, target) catch |err| switch (err) {7030 const res = val.floatCast(sema.arena, dest_type, target) catch |err| switch (err) {
7016 error.Overflow => return sema.mod.fail(7031 error.Overflow => return sema.mod.fail(
7017 &block.base,7032 &block.base,
7018 inst.src,7033 inst_src,
7019 "cast of value {} to type '{}' loses information",7034 "cast of value {} to type '{}' loses information",
7020 .{ val, dest_type },7035 .{ val, dest_type },
7021 ),7036 ),
7022 error.OutOfMemory => return error.OutOfMemory,7037 error.OutOfMemory => return error.OutOfMemory,
7023 };7038 };
7024 return sema.addConstant(dest_type, res);7039 return try sema.addConstant(dest_type, res);
7025 } else if (src_zig_tag == .Int or src_zig_tag == .ComptimeInt) {7040 } else if (src_zig_tag == .Int or src_zig_tag == .ComptimeInt) {
7026 return sema.mod.fail(&block.base, inst.src, "TODO int to float", .{});7041 return sema.mod.fail(&block.base, inst_src, "TODO int to float", .{});
7027 }7042 }
7028 }7043 }
7029 return null;7044 return null;
...@@ -7051,10 +7066,11 @@ fn storePtr(...@@ -7051,10 +7066,11 @@ fn storePtr(
7051 ptr: Air.Inst.Ref,7066 ptr: Air.Inst.Ref,
7052 uncasted_value: Air.Inst.Ref,7067 uncasted_value: Air.Inst.Ref,
7053) !void {7068) !void {
7054 if (ptr.ty.isConstPtr())7069 const ptr_ty = sema.typeOf(ptr);
7070 if (ptr_ty.isConstPtr())
7055 return sema.mod.fail(&block.base, src, "cannot assign to constant", .{});7071 return sema.mod.fail(&block.base, src, "cannot assign to constant", .{});
70567072
7057 const elem_ty = ptr.ty.elemType();7073 const elem_ty = ptr_ty.elemType();
7058 const value = try sema.coerce(block, elem_ty, uncasted_value, src);7074 const value = try sema.coerce(block, elem_ty, uncasted_value, src);
7059 if ((try sema.typeHasOnePossibleValue(block, src, elem_ty)) != null)7075 if ((try sema.typeHasOnePossibleValue(block, src, elem_ty)) != null)
7060 return;7076 return;
...@@ -7113,20 +7129,32 @@ fn bitcast(...@@ -7113,20 +7129,32 @@ fn bitcast(
7113 return block.addTyOp(.bitcast, dest_type, inst);7129 return block.addTyOp(.bitcast, dest_type, inst);
7114}7130}
71157131
7116fn coerceArrayPtrToSlice(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.Ref) CompileError!Air.Inst.Ref {7132fn coerceArrayPtrToSlice(
7117 if (inst.value()) |val| {7133 sema: *Sema,
7134 block: *Scope.Block,
7135 dest_type: Type,
7136 inst: Air.Inst.Ref,
7137 inst_src: LazySrcLoc,
7138) CompileError!Air.Inst.Ref {
7139 if (try sema.resolveDefinedValue(block, inst_src, inst)) |val| {
7118 // The comptime Value representation is compatible with both types.7140 // The comptime Value representation is compatible with both types.
7119 return sema.addConstant(dest_type, val);7141 return sema.addConstant(dest_type, val);
7120 }7142 }
7121 return sema.mod.fail(&block.base, inst.src, "TODO implement coerceArrayPtrToSlice runtime instruction", .{});7143 return sema.mod.fail(&block.base, inst_src, "TODO implement coerceArrayPtrToSlice runtime instruction", .{});
7122}7144}
71237145
7124fn coerceArrayPtrToMany(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.Ref) !Air.Inst.Ref {7146fn coerceArrayPtrToMany(
7125 if (inst.value()) |val| {7147 sema: *Sema,
7148 block: *Scope.Block,
7149 dest_type: Type,
7150 inst: Air.Inst.Ref,
7151 inst_src: LazySrcLoc,
7152) !Air.Inst.Ref {
7153 if (try sema.resolveDefinedValue(block, inst_src, inst)) |val| {
7126 // The comptime Value representation is compatible with both types.7154 // The comptime Value representation is compatible with both types.
7127 return sema.addConstant(dest_type, val);7155 return sema.addConstant(dest_type, val);
7128 }7156 }
7129 return sema.mod.fail(&block.base, inst.src, "TODO implement coerceArrayPtrToMany runtime instruction", .{});7157 return sema.mod.fail(&block.base, inst_src, "TODO implement coerceArrayPtrToMany runtime instruction", .{});
7130}7158}
71317159
7132fn analyzeDeclVal(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, decl: *Decl) CompileError!Air.Inst.Ref {7160fn analyzeDeclVal(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, decl: *Decl) CompileError!Air.Inst.Ref {
...@@ -7531,12 +7559,18 @@ fn cmpNumeric(...@@ -7531,12 +7559,18 @@ fn cmpNumeric(
7531 return block.addBinOp(Air.Inst.Tag.fromCmpOp(op), casted_lhs, casted_rhs);7559 return block.addBinOp(Air.Inst.Tag.fromCmpOp(op), casted_lhs, casted_rhs);
7532}7560}
75337561
7534fn wrapOptional(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.Ref) !Air.Inst.Index {7562fn wrapOptional(
7563 sema: *Sema,
7564 block: *Scope.Block,
7565 dest_type: Type,
7566 inst: Air.Inst.Ref,
7567 inst_src: LazySrcLoc,
7568) !Air.Inst.Ref {
7535 if (try sema.resolvePossiblyUndefinedValue(block, inst_src, inst)) |val| {7569 if (try sema.resolvePossiblyUndefinedValue(block, inst_src, inst)) |val| {
7536 return sema.addConstant(dest_type, val);7570 return sema.addConstant(dest_type, val);
7537 }7571 }
75387572
7539 try sema.requireRuntimeBlock(block, inst.src);7573 try sema.requireRuntimeBlock(block, inst_src);
7540 return block.addTyOp(.wrap_optional, dest_type, inst);7574 return block.addTyOp(.wrap_optional, dest_type, inst);
7541}7575}
75427576
...@@ -7546,11 +7580,12 @@ fn wrapErrorUnion(...@@ -7546,11 +7580,12 @@ fn wrapErrorUnion(
7546 dest_type: Type,7580 dest_type: Type,
7547 inst: Air.Inst.Ref,7581 inst: Air.Inst.Ref,
7548 inst_src: LazySrcLoc,7582 inst_src: LazySrcLoc,
7549) !Air.Inst.Index {7583) !Air.Inst.Ref {
7584 const inst_ty = sema.typeOf(inst);
7550 const err_union = dest_type.castTag(.error_union).?;7585 const err_union = dest_type.castTag(.error_union).?;
7551 if (try sema.resolvePossiblyUndefinedValue(block, inst_src, inst)) |val| {7586 if (try sema.resolvePossiblyUndefinedValue(block, inst_src, inst)) |val| {
7552 if (inst.ty.zigTypeTag() != .ErrorSet) {7587 if (inst_ty.zigTypeTag() != .ErrorSet) {
7553 _ = try sema.coerce(block, err_union.data.payload, inst, inst.src);7588 _ = try sema.coerce(block, err_union.data.payload, inst, inst_src);
7554 } else switch (err_union.data.error_set.tag()) {7589 } else switch (err_union.data.error_set.tag()) {
7555 .anyerror => {},7590 .anyerror => {},
7556 .error_set_single => {7591 .error_set_single => {
...@@ -7559,9 +7594,9 @@ fn wrapErrorUnion(...@@ -7559,9 +7594,9 @@ fn wrapErrorUnion(
7559 if (!mem.eql(u8, expected_name, n)) {7594 if (!mem.eql(u8, expected_name, n)) {
7560 return sema.mod.fail(7595 return sema.mod.fail(
7561 &block.base,7596 &block.base,
7562 inst.src,7597 inst_src,
7563 "expected type '{}', found type '{}'",7598 "expected type '{}', found type '{}'",
7564 .{ err_union.data.error_set, inst.ty },7599 .{ err_union.data.error_set, inst_ty },
7565 );7600 );
7566 }7601 }
7567 },7602 },
...@@ -7577,9 +7612,9 @@ fn wrapErrorUnion(...@@ -7577,9 +7612,9 @@ fn wrapErrorUnion(
7577 if (!found) {7612 if (!found) {
7578 return sema.mod.fail(7613 return sema.mod.fail(
7579 &block.base,7614 &block.base,
7580 inst.src,7615 inst_src,
7581 "expected type '{}', found type '{}'",7616 "expected type '{}', found type '{}'",
7582 .{ err_union.data.error_set, inst.ty },7617 .{ err_union.data.error_set, inst_ty },
7583 );7618 );
7584 }7619 }
7585 },7620 },
...@@ -7589,9 +7624,9 @@ fn wrapErrorUnion(...@@ -7589,9 +7624,9 @@ fn wrapErrorUnion(
7589 if (!map.contains(expected_name)) {7624 if (!map.contains(expected_name)) {
7590 return sema.mod.fail(7625 return sema.mod.fail(
7591 &block.base,7626 &block.base,
7592 inst.src,7627 inst_src,
7593 "expected type '{}', found type '{}'",7628 "expected type '{}', found type '{}'",
7594 .{ err_union.data.error_set, inst.ty },7629 .{ err_union.data.error_set, inst_ty },
7595 );7630 );
7596 }7631 }
7597 },7632 },
...@@ -7602,14 +7637,14 @@ fn wrapErrorUnion(...@@ -7602,14 +7637,14 @@ fn wrapErrorUnion(
7602 return sema.addConstant(dest_type, try Value.Tag.error_union.create(sema.arena, val));7637 return sema.addConstant(dest_type, try Value.Tag.error_union.create(sema.arena, val));
7603 }7638 }
76047639
7605 try sema.requireRuntimeBlock(block, inst.src);7640 try sema.requireRuntimeBlock(block, inst_src);
76067641
7607 // we are coercing from E to E!T7642 // we are coercing from E to E!T
7608 if (inst.ty.zigTypeTag() == .ErrorSet) {7643 if (inst_ty.zigTypeTag() == .ErrorSet) {
7609 var coerced = try sema.coerce(block, err_union.data.error_set, inst, inst.src);7644 var coerced = try sema.coerce(block, err_union.data.error_set, inst, inst_src);
7610 return block.addTyOp(.wrap_errunion_err, dest_type, coerced);7645 return block.addTyOp(.wrap_errunion_err, dest_type, coerced);
7611 } else {7646 } else {
7612 var coerced = try sema.coerce(block, err_union.data.payload, inst, inst.src);7647 var coerced = try sema.coerce(block, err_union.data.payload, inst, inst_src);
7613 return block.addTyOp(.wrap_errunion_payload, dest_type, coerced);7648 return block.addTyOp(.wrap_errunion_payload, dest_type, coerced);
7614 }7649 }
7615}7650}
...@@ -8081,7 +8116,10 @@ fn typeOf(sema: *Sema, inst: Air.Inst.Ref) Type {...@@ -8081,7 +8116,10 @@ fn typeOf(sema: *Sema, inst: Air.Inst.Ref) Type {
80818116
8082 .ptrtoint => return Type.initTag(.usize),8117 .ptrtoint => return Type.initTag(.usize),
80838118
8084 .call => @panic("TODO Sema.typeOf call"),8119 .call => {
8120 const callee_ty = sema.typeOf(air_datas[i].pl_op.operand);
8121 return callee_ty.fnReturnType();
8122 },
8085 }8123 }
8086}8124}
80878125
...@@ -8219,3 +8257,12 @@ fn getBreakBlock(sema: *Sema, inst_index: Air.Inst.Index) ?Air.Inst.Index {...@@ -8219,3 +8257,12 @@ fn getBreakBlock(sema: *Sema, inst_index: Air.Inst.Index) ?Air.Inst.Index {
8219 else => return null,8257 else => return null,
8220 }8258 }
8221}8259}
8260
8261fn isComptimeKnown(
8262 sema: *Sema,
8263 block: *Scope.Block,
8264 src: LazySrcLoc,
8265 inst: Air.Inst.Ref,
8266) !bool {
8267 return (try sema.resolvePossiblyUndefinedValue(block, src, inst)) != null;
8268}
src/value.zig+1-1
...@@ -1700,7 +1700,7 @@ pub const Value = extern union {...@@ -1700,7 +1700,7 @@ pub const Value = extern union {
1700 /// peer type resolution. This is stored in a separate list so that1700 /// peer type resolution. This is stored in a separate list so that
1701 /// the items are contiguous in memory and thus can be passed to1701 /// the items are contiguous in memory and thus can be passed to
1702 /// `Module.resolvePeerTypes`.1702 /// `Module.resolvePeerTypes`.
1703 stored_inst_list: std.ArrayListUnmanaged(Air.Inst.Index) = .{},1703 stored_inst_list: std.ArrayListUnmanaged(Air.Inst.Ref) = .{},
1704 },1704 },
1705 };1705 };
17061706