| ... | ... | @@ -285,6 +285,9 @@ const Function = struct { |
| 285 | 285 | memory: u64, |
| 286 | 286 | /// The value is one of the stack variables. |
| 287 | 287 | stack_offset: u64, |
| 288 | /// The value is the compare flag, with this operator |
| 289 | /// applied on top of it. |
| 290 | compare_flag: std.math.CompareOperator, |
| 288 | 291 | |
| 289 | 292 | fn isMemory(mcv: MCValue) bool { |
| 290 | 293 | return switch (mcv) { |
| ... | ... | @@ -292,6 +295,31 @@ const Function = struct { |
| 292 | 295 | else => false, |
| 293 | 296 | }; |
| 294 | 297 | } |
| 298 | |
| 299 | fn isImmediate(mcv: MCValue) bool { |
| 300 | return switch (mcv) { |
| 301 | .immediate => true, |
| 302 | else => false, |
| 303 | }; |
| 304 | } |
| 305 | |
| 306 | fn isMutable(mcv: MCValue) bool { |
| 307 | return switch (mcv) { |
| 308 | .none => unreachable, |
| 309 | .unreach => unreachable, |
| 310 | .dead => unreachable, |
| 311 | |
| 312 | .immediate, |
| 313 | .embedded_in_code, |
| 314 | .memory, |
| 315 | .compare_flag, |
| 316 | => false, |
| 317 | |
| 318 | .register, |
| 319 | .stack_offset, |
| 320 | => true, |
| 321 | }; |
| 322 | } |
| 295 | 323 | }; |
| 296 | 324 | |
| 297 | 325 | fn gen(self: *Function) !void { |
| ... | ... | @@ -362,20 +390,21 @@ const Function = struct { |
| 362 | 390 | switch (inst.tag) { |
| 363 | 391 | .add => return self.genAdd(inst.cast(ir.Inst.Add).?, arch), |
| 364 | 392 | .arg => return self.genArg(inst.cast(ir.Inst.Arg).?), |
| 393 | .assembly => return self.genAsm(inst.cast(ir.Inst.Assembly).?, arch), |
| 394 | .bitcast => return self.genBitCast(inst.cast(ir.Inst.BitCast).?), |
| 365 | 395 | .block => return self.genBlock(inst.cast(ir.Inst.Block).?, arch), |
| 366 | 396 | .breakpoint => return self.genBreakpoint(inst.src, arch), |
| 367 | 397 | .call => return self.genCall(inst.cast(ir.Inst.Call).?, arch), |
| 368 | | .unreach => return MCValue{ .unreach = {} }, |
| 398 | .cmp => return self.genCmp(inst.cast(ir.Inst.Cmp).?, arch), |
| 399 | .condbr => return self.genCondBr(inst.cast(ir.Inst.CondBr).?, arch), |
| 369 | 400 | .constant => unreachable, // excluded from function bodies |
| 370 | | .assembly => return self.genAsm(inst.cast(ir.Inst.Assembly).?, arch), |
| 401 | .isnonnull => return self.genIsNonNull(inst.cast(ir.Inst.IsNonNull).?, arch), |
| 402 | .isnull => return self.genIsNull(inst.cast(ir.Inst.IsNull).?, arch), |
| 371 | 403 | .ptrtoint => return self.genPtrToInt(inst.cast(ir.Inst.PtrToInt).?), |
| 372 | | .bitcast => return self.genBitCast(inst.cast(ir.Inst.BitCast).?), |
| 373 | 404 | .ret => return self.genRet(inst.cast(ir.Inst.Ret).?, arch), |
| 374 | 405 | .retvoid => return self.genRetVoid(inst.cast(ir.Inst.RetVoid).?, arch), |
| 375 | | .cmp => return self.genCmp(inst.cast(ir.Inst.Cmp).?, arch), |
| 376 | | .condbr => return self.genCondBr(inst.cast(ir.Inst.CondBr).?, arch), |
| 377 | | .isnull => return self.genIsNull(inst.cast(ir.Inst.IsNull).?, arch), |
| 378 | | .isnonnull => return self.genIsNonNull(inst.cast(ir.Inst.IsNonNull).?, arch), |
| 406 | .sub => return self.genSub(inst.cast(ir.Inst.Sub).?, arch), |
| 407 | .unreach => return MCValue{ .unreach = {} }, |
| 379 | 408 | } |
| 380 | 409 | } |
| 381 | 410 | |
| ... | ... | @@ -385,96 +414,136 @@ const Function = struct { |
| 385 | 414 | return MCValue.dead; |
| 386 | 415 | switch (arch) { |
| 387 | 416 | .x86_64 => { |
| 388 | | // Biggest encoding of ADD is 8 bytes. |
| 389 | | try self.code.ensureCapacity(self.code.items.len + 8); |
| 417 | return try self.genX8664BinMath(&inst.base, inst.args.lhs, inst.args.rhs, 0, 0x00); |
| 418 | }, |
| 419 | else => return self.fail(inst.base.src, "TODO implement add for {}", .{self.target.cpu.arch}), |
| 420 | } |
| 421 | } |
| 390 | 422 | |
| 391 | | // In x86, ADD has 2 operands, destination and source. |
| 392 | | // Either one, but not both, can be a memory operand. |
| 393 | | // Source operand can be an immediate, 8 bits or 32 bits. |
| 394 | | // So, if either one of the operands dies with this instruction, we can use it |
| 395 | | // as the result MCValue. |
| 396 | | var dst_mcv: MCValue = undefined; |
| 397 | | var src_mcv: MCValue = undefined; |
| 398 | | if (inst.base.operandDies(0)) { |
| 399 | | // LHS dies; use it as the destination. |
| 400 | | dst_mcv = try self.resolveInst(inst.args.lhs); |
| 401 | | // Both operands cannot be memory. |
| 402 | | if (dst_mcv.isMemory()) { |
| 403 | | src_mcv = try self.resolveInstImmOrReg(inst.args.rhs); |
| 404 | | } else { |
| 405 | | src_mcv = try self.resolveInst(inst.args.rhs); |
| 406 | | } |
| 407 | | } else if (inst.base.operandDies(1)) { |
| 408 | | // RHS dies; use it as the destination. |
| 409 | | dst_mcv = try self.resolveInst(inst.args.rhs); |
| 410 | | // Both operands cannot be memory. |
| 411 | | if (dst_mcv.isMemory()) { |
| 412 | | src_mcv = try self.resolveInstImmOrReg(inst.args.lhs); |
| 413 | | } else { |
| 414 | | src_mcv = try self.resolveInst(inst.args.lhs); |
| 415 | | } |
| 416 | | } else { |
| 417 | | const lhs = try self.resolveInst(inst.args.lhs); |
| 418 | | const rhs = try self.resolveInst(inst.args.rhs); |
| 419 | | if (lhs.isMemory()) { |
| 420 | | dst_mcv = try self.copyToNewRegister(inst.base.src, lhs); |
| 421 | | src_mcv = rhs; |
| 422 | | } else { |
| 423 | | dst_mcv = try self.copyToNewRegister(inst.base.src, rhs); |
| 424 | | src_mcv = lhs; |
| 425 | | } |
| 426 | | } |
| 427 | | // x86 ADD supports only signed 32-bit immediates at most. If the immediate |
| 428 | | // value is larger than this, we put it in a register. |
| 429 | | // A potential opportunity for future optimization here would be keeping track |
| 430 | | // of the fact that the instruction is available both as an immediate |
| 431 | | // and as a register. |
| 432 | | switch (src_mcv) { |
| 433 | | .immediate => |imm| { |
| 434 | | if (imm > std.math.maxInt(u31)) { |
| 435 | | src_mcv = try self.copyToNewRegister(inst.base.src, src_mcv); |
| 436 | | } |
| 437 | | }, |
| 438 | | else => {}, |
| 423 | fn genSub(self: *Function, inst: *ir.Inst.Sub, comptime arch: std.Target.Cpu.Arch) !MCValue { |
| 424 | // No side effects, so if it's unreferenced, do nothing. |
| 425 | if (inst.base.isUnused()) |
| 426 | return MCValue.dead; |
| 427 | switch (arch) { |
| 428 | .x86_64 => { |
| 429 | return try self.genX8664BinMath(&inst.base, inst.args.lhs, inst.args.rhs, 5, 0x28); |
| 430 | }, |
| 431 | else => return self.fail(inst.base.src, "TODO implement sub for {}", .{self.target.cpu.arch}), |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /// ADD, SUB |
| 436 | fn genX8664BinMath(self: *Function, inst: *ir.Inst, op_lhs: *ir.Inst, op_rhs: *ir.Inst, opx: u8, mr: u8) !MCValue { |
| 437 | try self.code.ensureCapacity(self.code.items.len + 8); |
| 438 | |
| 439 | const lhs = try self.resolveInst(op_lhs); |
| 440 | const rhs = try self.resolveInst(op_rhs); |
| 441 | |
| 442 | // There are 2 operands, destination and source. |
| 443 | // Either one, but not both, can be a memory operand. |
| 444 | // Source operand can be an immediate, 8 bits or 32 bits. |
| 445 | // So, if either one of the operands dies with this instruction, we can use it |
| 446 | // as the result MCValue. |
| 447 | var dst_mcv: MCValue = undefined; |
| 448 | var src_mcv: MCValue = undefined; |
| 449 | var src_inst: *ir.Inst = undefined; |
| 450 | if (inst.operandDies(0) and lhs.isMutable()) { |
| 451 | // LHS dies; use it as the destination. |
| 452 | // Both operands cannot be memory. |
| 453 | src_inst = op_rhs; |
| 454 | if (lhs.isMemory() and rhs.isMemory()) { |
| 455 | dst_mcv = try self.copyToNewRegister(op_lhs); |
| 456 | src_mcv = rhs; |
| 457 | } else { |
| 458 | dst_mcv = lhs; |
| 459 | src_mcv = rhs; |
| 460 | } |
| 461 | } else if (inst.operandDies(1) and rhs.isMutable()) { |
| 462 | // RHS dies; use it as the destination. |
| 463 | // Both operands cannot be memory. |
| 464 | src_inst = op_lhs; |
| 465 | if (lhs.isMemory() and rhs.isMemory()) { |
| 466 | dst_mcv = try self.copyToNewRegister(op_rhs); |
| 467 | src_mcv = lhs; |
| 468 | } else { |
| 469 | dst_mcv = rhs; |
| 470 | src_mcv = lhs; |
| 471 | } |
| 472 | } else { |
| 473 | if (lhs.isMemory()) { |
| 474 | dst_mcv = try self.copyToNewRegister(op_lhs); |
| 475 | src_mcv = rhs; |
| 476 | src_inst = op_rhs; |
| 477 | } else { |
| 478 | dst_mcv = try self.copyToNewRegister(op_rhs); |
| 479 | src_mcv = lhs; |
| 480 | src_inst = op_lhs; |
| 481 | } |
| 482 | } |
| 483 | // This instruction supports only signed 32-bit immediates at most. If the immediate |
| 484 | // value is larger than this, we put it in a register. |
| 485 | // A potential opportunity for future optimization here would be keeping track |
| 486 | // of the fact that the instruction is available both as an immediate |
| 487 | // and as a register. |
| 488 | switch (src_mcv) { |
| 489 | .immediate => |imm| { |
| 490 | if (imm > std.math.maxInt(u31)) { |
| 491 | src_mcv = try self.copyToNewRegister(src_inst); |
| 439 | 492 | } |
| 493 | }, |
| 494 | else => {}, |
| 495 | } |
| 496 | |
| 497 | try self.genX8664BinMathCode(inst.src, dst_mcv, src_mcv, opx, mr); |
| 440 | 498 | |
| 441 | | switch (dst_mcv) { |
| 499 | return dst_mcv; |
| 500 | } |
| 501 | |
| 502 | fn genX8664BinMathCode(self: *Function, src: usize, dst_mcv: MCValue, src_mcv: MCValue, opx: u8, mr: u8) !void { |
| 503 | switch (dst_mcv) { |
| 504 | .none => unreachable, |
| 505 | .dead, .unreach, .immediate => unreachable, |
| 506 | .compare_flag => unreachable, |
| 507 | .register => |dst_reg_usize| { |
| 508 | const dst_reg = @intToEnum(Reg(.x86_64), @intCast(u8, dst_reg_usize)); |
| 509 | switch (src_mcv) { |
| 442 | 510 | .none => unreachable, |
| 443 | | .dead, .unreach, .immediate => unreachable, |
| 444 | | .register => |dst_reg_usize| { |
| 445 | | const dst_reg = @intToEnum(Reg(arch), @intCast(@TagType(Reg(arch)), dst_reg_usize)); |
| 446 | | switch (src_mcv) { |
| 447 | | .none => unreachable, |
| 448 | | .dead, .unreach => unreachable, |
| 449 | | .register => |src_reg_usize| { |
| 450 | | const src_reg = @intToEnum(Reg(arch), @intCast(@TagType(Reg(arch)), src_reg_usize)); |
| 451 | | self.rex(.{ .b = dst_reg.isExtended(), .r = src_reg.isExtended(), .w = dst_reg.size() == 64 }); |
| 452 | | self.code.appendSliceAssumeCapacity(&[_]u8{ 0x1, 0xC0 | (@as(u8, src_reg.id() & 0b111) << 3) | @as(u8, dst_reg.id() & 0b111) }); |
| 453 | | }, |
| 454 | | .immediate => |imm| { |
| 455 | | const imm32 = @intCast(u31, imm); // We handle this case above. |
| 456 | | // 81 /0 id |
| 457 | | if (imm32 <= std.math.maxInt(u7)) { |
| 458 | | self.rex(.{ .b = dst_reg.isExtended(), .w = dst_reg.size() == 64 }); |
| 459 | | self.code.appendSliceAssumeCapacity(&[_]u8{ 0x83, 0xC0 | @as(u8, dst_reg.id() & 0b111), @intCast(u8, imm32)}); |
| 460 | | } else { |
| 461 | | self.rex(.{ .r = dst_reg.isExtended(), .w = dst_reg.size() == 64 }); |
| 462 | | self.code.appendSliceAssumeCapacity(&[_]u8{ 0x81, 0xC0 | @as(u8, dst_reg.id() & 0b111) }); |
| 463 | | std.mem.writeIntLittle(u32, self.code.addManyAsArrayAssumeCapacity(4), imm32); |
| 464 | | } |
| 465 | | }, |
| 466 | | .embedded_in_code, .memory, .stack_offset => { |
| 467 | | return self.fail(inst.base.src, "TODO implement x86 add source memory", .{}); |
| 468 | | }, |
| 511 | .dead, .unreach => unreachable, |
| 512 | .register => |src_reg_usize| { |
| 513 | const src_reg = @intToEnum(Reg(.x86_64), @intCast(u8, src_reg_usize)); |
| 514 | self.rex(.{ .b = dst_reg.isExtended(), .r = src_reg.isExtended(), .w = dst_reg.size() == 64 }); |
| 515 | self.code.appendSliceAssumeCapacity(&[_]u8{ mr + 0x1, 0xC0 | (@as(u8, src_reg.id() & 0b111) << 3) | @as(u8, dst_reg.id() & 0b111) }); |
| 516 | }, |
| 517 | .immediate => |imm| { |
| 518 | const imm32 = @intCast(u31, imm); // This case must be handled before calling genX8664BinMathCode. |
| 519 | // 81 /opx id |
| 520 | if (imm32 <= std.math.maxInt(u7)) { |
| 521 | self.rex(.{ .b = dst_reg.isExtended(), .w = dst_reg.size() == 64 }); |
| 522 | self.code.appendSliceAssumeCapacity(&[_]u8{ |
| 523 | 0x83, |
| 524 | 0xC0 | (opx << 3) | @truncate(u3, dst_reg.id()), |
| 525 | @intCast(u8, imm32), |
| 526 | }); |
| 527 | } else { |
| 528 | self.rex(.{ .r = dst_reg.isExtended(), .w = dst_reg.size() == 64 }); |
| 529 | self.code.appendSliceAssumeCapacity(&[_]u8{ |
| 530 | 0x81, |
| 531 | 0xC0 | (opx << 3) | @truncate(u3, dst_reg.id()), |
| 532 | }); |
| 533 | std.mem.writeIntLittle(u32, self.code.addManyAsArrayAssumeCapacity(4), imm32); |
| 469 | 534 | } |
| 470 | 535 | }, |
| 471 | 536 | .embedded_in_code, .memory, .stack_offset => { |
| 472 | | return self.fail(inst.base.src, "TODO implement x86 add destination memory", .{}); |
| 537 | return self.fail(src, "TODO implement x86 ADD/SUB/CMP source memory", .{}); |
| 538 | }, |
| 539 | .compare_flag => { |
| 540 | return self.fail(src, "TODO implement x86 ADD/SUB/CMP source compare flag", .{}); |
| 473 | 541 | }, |
| 474 | 542 | } |
| 475 | | return dst_mcv; |
| 476 | 543 | }, |
| 477 | | else => return self.fail(inst.base.src, "TODO implement add for {}", .{self.target.cpu.arch}), |
| 544 | .embedded_in_code, .memory, .stack_offset => { |
| 545 | return self.fail(src, "TODO implement x86 ADD/SUB/CMP destination memory", .{}); |
| 546 | }, |
| 478 | 547 | } |
| 479 | 548 | } |
| 480 | 549 | |
| ... | ... | @@ -550,7 +619,29 @@ const Function = struct { |
| 550 | 619 | } |
| 551 | 620 | |
| 552 | 621 | fn genCmp(self: *Function, inst: *ir.Inst.Cmp, comptime arch: std.Target.Cpu.Arch) !MCValue { |
| 622 | // No side effects, so if it's unreferenced, do nothing. |
| 623 | if (inst.base.isUnused()) |
| 624 | return MCValue.dead; |
| 553 | 625 | switch (arch) { |
| 626 | .x86_64 => { |
| 627 | try self.code.ensureCapacity(self.code.items.len + 8); |
| 628 | |
| 629 | const lhs = try self.resolveInst(inst.args.lhs); |
| 630 | const rhs = try self.resolveInst(inst.args.rhs); |
| 631 | |
| 632 | // There are 2 operands, destination and source. |
| 633 | // Either one, but not both, can be a memory operand. |
| 634 | // Source operand can be an immediate, 8 bits or 32 bits. |
| 635 | const dst_mcv = if (lhs.isImmediate() or (lhs.isMemory() and rhs.isMemory())) |
| 636 | try self.copyToNewRegister(inst.args.lhs) |
| 637 | else |
| 638 | lhs; |
| 639 | // This instruction supports only signed 32-bit immediates at most. |
| 640 | const src_mcv = try self.limitImmediateType(inst.args.rhs, i32); |
| 641 | |
| 642 | try self.genX8664BinMathCode(inst.base.src, dst_mcv, src_mcv, 7, 0x38); |
| 643 | return MCValue{.compare_flag = inst.args.op}; |
| 644 | }, |
| 554 | 645 | else => return self.fail(inst.base.src, "TODO implement cmp for {}", .{self.target.cpu.arch}), |
| 555 | 646 | } |
| 556 | 647 | } |
| ... | ... | @@ -668,6 +759,9 @@ const Function = struct { |
| 668 | 759 | .dead => unreachable, |
| 669 | 760 | .none => unreachable, |
| 670 | 761 | .unreach => unreachable, |
| 762 | .compare_flag => |op| { |
| 763 | return self.fail(src, "TODO set register with compare flag value", .{}); |
| 764 | }, |
| 671 | 765 | .immediate => |x| { |
| 672 | 766 | if (reg.size() != 64) { |
| 673 | 767 | return self.fail(src, "TODO decide whether to implement non-64-bit loads", .{}); |
| ... | ... | @@ -871,14 +965,35 @@ const Function = struct { |
| 871 | 965 | } |
| 872 | 966 | } |
| 873 | 967 | |
| 874 | | fn resolveInstImmOrReg(self: *Function, inst: *ir.Inst) !MCValue { |
| 875 | | return self.fail(inst.src, "TODO implement resolveInstImmOrReg", .{}); |
| 968 | fn copyToNewRegister(self: *Function, inst: *ir.Inst) !MCValue { |
| 969 | return self.fail(inst.src, "TODO implement copyToNewRegister", .{}); |
| 876 | 970 | } |
| 877 | 971 | |
| 878 | | fn copyToNewRegister(self: *Function, src: usize, mcv: MCValue) !MCValue { |
| 879 | | return self.fail(src, "TODO implement copyToNewRegister", .{}); |
| 972 | /// If the MCValue is an immediate, and it does not fit within this type, |
| 973 | /// we put it in a register. |
| 974 | /// A potential opportunity for future optimization here would be keeping track |
| 975 | /// of the fact that the instruction is available both as an immediate |
| 976 | /// and as a register. |
| 977 | fn limitImmediateType(self: *Function, inst: *ir.Inst, comptime T: type) !MCValue { |
| 978 | const mcv = try self.resolveInst(inst); |
| 979 | const ti = @typeInfo(T).Int; |
| 980 | switch (mcv) { |
| 981 | .immediate => |imm| { |
| 982 | // This immediate is unsigned. |
| 983 | const U = @Type(.{ .Int = .{ |
| 984 | .bits = ti.bits - @boolToInt(ti.is_signed), |
| 985 | .is_signed = false, |
| 986 | }}); |
| 987 | if (imm >= std.math.maxInt(U)) { |
| 988 | return self.copyToNewRegister(inst); |
| 989 | } |
| 990 | }, |
| 991 | else => {}, |
| 992 | } |
| 993 | return mcv; |
| 880 | 994 | } |
| 881 | 995 | |
| 996 | |
| 882 | 997 | fn genTypedValue(self: *Function, src: usize, typed_value: TypedValue) !MCValue { |
| 883 | 998 | const ptr_bits = self.target.cpu.arch.ptrBitWidth(); |
| 884 | 999 | const ptr_bytes: u64 = @divExact(ptr_bits, 8); |