Skip to content

Commit

Permalink
Fix indexing arrays of structs larger than 0x80 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
CloakerSmoker committed Dec 28, 2023
1 parent 4968f48 commit cd32372
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
Binary file modified build/freebsd_compiler.elf
Binary file not shown.
Binary file modified build/linux_compiler.elf
Binary file not shown.
Binary file modified build/windows_compiler.exe
Binary file not shown.
4 changes: 4 additions & 0 deletions src/compiler/CodeGen.rlx
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,10 @@ define void EmitIMul_R64_I8(CompilerState* Compiler, i8 DestinationRegister, i8
EmitBasicInstruction(Compiler, 0x6B, MODE_RTOR, 8, DestinationRegister, DestinationRegister)
PushByte(Compiler, Value)
}
define void EmitIMul_R64_I32(CompilerState* Compiler, i8 DestinationRegister, i32 Value) {
EmitBasicInstruction(Compiler, 0x69, MODE_RTOR, 8, DestinationRegister, DestinationRegister)
PushI32(Compiler, Value)
}


define void EmitCXX(CompilerState* Compiler, i8 Size) {
Expand Down
22 changes: 14 additions & 8 deletions src/compiler/Compiler.rlx
Original file line number Diff line number Diff line change
Expand Up @@ -2671,13 +2671,13 @@ define void CompileArrayAccess(CompilerState* Compiler, i8 ResultRegister, ASTNo

Type PointerType := Pointer->ValueType

i8 IndexScale := 8
i32 IndexScale := 8

TypeInfo* PointerBase := GetBaseType(PointerType)
i16 PointerDepth := GetPointerDepth(PointerType)

if (PointerDepth = 1) {
IndexScale := (PointerBase->Size) As i8
IndexScale := PointerBase->Size

if (PointerBase->Is(TYPE_KIND_STRUCTURE)) {
GetAddress := true
Expand All @@ -2698,11 +2698,11 @@ define void CompileArrayAccess(CompilerState* Compiler, i8 ResultRegister, ASTNo
EmitLea_R64_R64_DISPX(Compiler, ResultRegister, PointerRegister, IndexAsDisplacement)

if (IsSet) {
EmitMove_RIX_RX(Compiler, ResultRegister, ValueRegister, IndexScale)
EmitMove_RIX_RX(Compiler, ResultRegister, ValueRegister, IndexScale As i8)
}
}
else {
EmitMove_RX_RIX_DISPX(Compiler, ResultRegister, PointerRegister, IndexAsDisplacement, DisplacementSize, IndexScale)
EmitMove_RX_RIX_DISPX(Compiler, ResultRegister, PointerRegister, IndexAsDisplacement, DisplacementSize, IndexScale As i8)
}
}
else {
Expand All @@ -2717,19 +2717,25 @@ define void CompileArrayAccess(CompilerState* Compiler, i8 ResultRegister, ASTNo

if (IsSet || GetAddress) {
if (IndexScale > 8) {
EmitIMul_R64_I8(Compiler, IndexRegister, IndexScale)
if (IndexScale < 0x80) {
EmitIMul_R64_I8(Compiler, IndexRegister, IndexScale As i8)
}
else {
EmitIMul_R64_I32(Compiler, IndexRegister, IndexScale)
}

EmitAdd_RX_RX(Compiler, PointerRegister, IndexRegister, 8)
}
else {
EmitLea_R64_SIB(Compiler, ResultRegister, IndexScale, IndexRegister, PointerRegister)
EmitLea_R64_SIB(Compiler, ResultRegister, IndexScale As i8, IndexRegister, PointerRegister)
}

if (IsSet) {
EmitMove_RIX_RX(Compiler, ResultRegister, ValueRegister, IndexScale)
EmitMove_RIX_RX(Compiler, ResultRegister, ValueRegister, IndexScale As i8)
}
}
else {
EmitMove_RX_SIB(Compiler, ResultRegister, IndexScale, IndexRegister, PointerRegister)
EmitMove_RX_SIB(Compiler, ResultRegister, IndexScale As i8, IndexRegister, PointerRegister)
}

PopRegisterStack(Compiler)
Expand Down

0 comments on commit cd32372

Please sign in to comment.