Skip to content

Commit

Permalink
Fix issues with FP arguments for inlined dynamic calls
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Jan 27, 2024
1 parent 1d02377 commit 18d7b7d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
28 changes: 20 additions & 8 deletions programs/dyncalls/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,37 +80,49 @@ def emit_inline_assembly(header, asmdef, index, fargs):
has_output = (retval != "void")
inputs = []
output = []
argno = 0
areg = 0
freg = 0
asm_regs = ""
asm_in = []
asm_out = []
asm_clob = []

if has_output:
asm_regs += "register " + retval + " ra0 asm(\"a0\");\n"
asm_regs += "register " + retval + " ra0 __asm__(\"a0\");\n"
asm_out = ["\"=r\"(ra0)"]

asm_clob += []

for arg in fargs:
reg = "a" + str(areg)
asm_regs += "register " + arg + " " + reg + " asm(\"" + reg + "\") = arg" + str(areg) + ";\n"
if "float" in arg or "double" in arg:
# floating-point registers
reg = "fa" + str(freg)
freg += 1
else:
# integral registers
reg = "a" + str(areg)
areg += 1
asm_regs += "register " + arg + " " + reg + " __asm__(\"" + reg + "\") = arg" + str(argno) + ";\n"
# strings
if "*" in arg:
asm_in += ["\"r\"(" + reg + ")"]
if "void" in arg:
asm_in += ["\"m\"(*(char *)arg" + str(areg) + ")"]
asm_in += ["\"m\"(*(char *)arg" + str(argno) + ")"]
else:
asm_in += ["\"m\"(*arg" + str(areg) + ")"]
asm_in += ["\"m\"(*arg" + str(argno) + ")"]
# floats
elif "float" in arg or "double" in arg:
asm_in += ["\"f\"(" + reg + ")"]
# integrals
else:
asm_in += ["\"r\"(" + reg + ")"]
fargs[areg ] = arg + " arg" + str(areg)
areg += 1
fargs[argno] = arg + " arg" + str(argno)
argno += 1

asm_in += []

header += "static inline " + retval + " i" + asmdef + " (" + ','.join(fargs) + ') {\n'
header += "static inline __attribute__((always_inline, optimize(\"O2\"))) " + retval + " i" + asmdef + " (" + ','.join(fargs) + ') {\n'
header += asm_regs
header += '__asm__ volatile(\".insn i 0b1011011, 0, x0, x0, ' + str(index) + "\"" \
+ " : " + ",".join(asm_out) + " : " + ",".join(asm_in) + " : " + ",".join(asm_clob) + ");\n"
Expand Down
20 changes: 20 additions & 0 deletions tests/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ TEST_CASE("Verify dynamic calls with arguments", "[Basic]")
extern "C" void test_args() {
sys_test_3i3f(123, 456, 789, 10.0f, 100.0f, 1000.0f);
}
extern "C" void test_inlined_args() {
isys_test_3i3f(123, 456, 789, 10.0f, 100.0f, 1000.0f);
}
int main() {
})M");
Expand Down Expand Up @@ -149,8 +152,25 @@ TEST_CASE("Verify dynamic calls with arguments", "[Basic]")
REQUIRE(strings_called == 1);
REQUIRE(args_called == 0);

// Clear argument registers
for (int i = 0; i < 8; i++) {
script.machine().cpu.reg(10+i) = 0;
script.machine().cpu.registers().getfl(10+i).load_u64(0);
}

script.call("test_args");

REQUIRE(strings_called == 1);
REQUIRE(args_called == 1);

// Clear argument registers
for (int i = 0; i < 8; i++) {
script.machine().cpu.reg(10+i) = 0;
script.machine().cpu.registers().getfl(10+i).load_u64(0);
}

script.call("test_inlined_args");

REQUIRE(strings_called == 1);
REQUIRE(args_called == 2);
}

0 comments on commit 18d7b7d

Please sign in to comment.