-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
1,269 additions
and
1,147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/compiler-pico-c/src/arch/x86/backend/compilers/compileCastInstruction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { IRCastInstruction } from 'frontend/ir/instructions'; | ||
import { CBackendError, CBackendErrorCode } from 'backend/errors/CBackendError'; | ||
|
||
import { genInstruction } from 'arch/x86/asm-utils'; | ||
import { isPrimitiveLikeType } from 'frontend/analyze'; | ||
|
||
import { X86CompilerInstructionFnAttrs } from '../../constants/types'; | ||
import { X86CompileInstructionOutput } from './shared'; | ||
|
||
type CastInstructionCompilerAttrs = | ||
X86CompilerInstructionFnAttrs<IRCastInstruction>; | ||
|
||
export function compileCastInstruction({ | ||
instruction, | ||
context, | ||
}: CastInstructionCompilerAttrs) { | ||
const { allocator } = context; | ||
const { inputVar, outputVar } = instruction; | ||
const { regs, x87regs, stackFrame, lifetime } = allocator; | ||
|
||
const output = new X86CompileInstructionOutput(); | ||
|
||
if ( | ||
!isPrimitiveLikeType(inputVar.type) || | ||
!isPrimitiveLikeType(outputVar.type) | ||
) { | ||
throw new CBackendError(CBackendErrorCode.CANNOT_CAST_VARIABLES); | ||
} | ||
|
||
if (inputVar.type.isIntegral() && outputVar.type.isFloating()) { | ||
// int -> float | ||
const pushResult = x87regs.tryResolveIRArgAsReg({ | ||
arg: inputVar, | ||
castedType: outputVar.type, | ||
allowCast: true, | ||
}); | ||
|
||
output.appendGroup(pushResult.asm); | ||
x87regs.tracker.setOwnership({ | ||
...pushResult.entry, | ||
varName: outputVar.name, | ||
}); | ||
} else if (inputVar.type.isFloating() && outputVar.type.isIntegral()) { | ||
// int -> float | ||
const spillVar = stackFrame.allocSpillVariable( | ||
outputVar.type.getByteSize(), | ||
); | ||
|
||
const spillMemAddr = stackFrame.getLocalVarStackRelAddress(spillVar.name, { | ||
withSize: true, | ||
}); | ||
|
||
const pushResult = x87regs.tryResolveIRArgAsReg({ | ||
arg: inputVar, | ||
stackTop: true, | ||
}); | ||
|
||
const reg = regs.requestReg({ | ||
size: outputVar.type.getByteSize(), | ||
}); | ||
|
||
output.appendGroup( | ||
x87regs.storeStackRegAtAddress({ | ||
reg: pushResult.entry.reg, | ||
address: spillMemAddr, | ||
integral: true, | ||
pop: !lifetime.isVariableLaterUsed( | ||
allocator.iterator.offset, | ||
spillVar.name, | ||
), | ||
}).asm, | ||
); | ||
|
||
output.appendInstructions( | ||
...reg.asm, | ||
genInstruction('mov', reg.value, spillMemAddr), | ||
); | ||
|
||
output.appendGroup(pushResult.asm); | ||
regs.ownership.setOwnership(outputVar.name, { | ||
reg: reg.value, | ||
}); | ||
} else if (outputVar.type.getByteSize() - inputVar.type.getByteSize() === 1) { | ||
const extendedReg = regs.requestReg({ | ||
size: outputVar.type.getByteSize(), | ||
}); | ||
|
||
const rValue = regs.tryResolveIrArg({ | ||
arg: inputVar, | ||
}); | ||
|
||
output.appendInstructions( | ||
...extendedReg.asm, | ||
...rValue.asm, | ||
genInstruction('movzx', extendedReg.value, rValue.value), | ||
); | ||
|
||
regs.ownership.setOwnership(outputVar.name, { | ||
reg: extendedReg.value, | ||
}); | ||
} else { | ||
regs.ownership.aliasOwnership(inputVar.name, outputVar.name); | ||
} | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.