@@ -236,67 +236,58 @@ qword_t ExternalFunctions::call(size_t id, const Arguments& args) {
236236
237237 // This is ATT (AT&T syntax) assembly code for x86_64 Linux
238238 qword_t result = 0 ;
239+ const void * fn_ptr = func.functionPtr ;
240+ const void * args_ptr = args.getBuffer ();
239241 __asm__ volatile (
240- " movq %[args_tag], %%r10\n "
241-
242- // Save original stack and align
243242 " pushq %%rbp\n "
244243 " movq %%rsp, %%rbp\n "
245244 " andq $-16, %%rsp\n "
246245
247- // Arg 1: RDI
248- " addq $8 , %%r10 \n "
249- " movq (%%r10), %%rdi \n "
250- " addq $8, %%r10 \n "
251- " cmpq $0, (%%r10 )\n "
246+ // Use R11 as our base pointer to avoid clobbering R10 if the compiler used it
247+ " movq %[args_tag] , %%r11 \n "
248+
249+ // Arg 1: Type is at 0(r11), Value is at 8(r11)
250+ " cmpq $0, 0 (%%r11 )\n "
252251 " je 1f\n "
252+ " movq 8(%%r11), %%rdi\n "
253253
254- // Arg 2: RSI
255- " addq $8, %%r10\n "
256- " movq (%%r10), %%rsi\n "
257- " addq $8, %%r10\n "
258- " cmpq $0, (%%r10)\n "
254+ // Arg 2: Type is at 16(r11), Value is at 24(r11)
255+ " cmpq $0, 16(%%r11)\n "
259256 " je 1f\n "
257+ " movq 24(%%r11), %%rsi\n "
260258
261- // Arg 3: RDX
262- " addq $8, %%r10\n "
263- " movq (%%r10), %%rdx\n "
264- " addq $8, %%r10\n "
265- " cmpq $0, (%%r10)\n "
259+ // Arg 3: Type is at 32(r11), Value is at 40(r11)
260+ " cmpq $0, 32(%%r11)\n "
266261 " je 1f\n "
262+ " movq 40(%%r11), %%rdx\n "
267263
268- // Arg 4: RCX
269- " addq $8, %%r10\n "
270- " movq (%%r10), %%rcx\n "
271- " addq $8, %%r10\n "
272- " cmpq $0, (%%r10)\n "
264+ // Arg 4: Type is at 48(r11), Value is at 56(r11)
265+ " cmpq $0, 48(%%r11)\n "
273266 " je 1f\n "
267+ " movq 56(%%r11), %%rcx\n "
274268
275- // Arg 5: R8
276- " addq $8, %%r10\n "
277- " movq (%%r10), %%r8\n "
278- " addq $8, %%r10\n "
279- " cmpq $0, (%%r10)\n "
269+ // Arg 5: Type is at 64(r11), Value is at 72(r11)
270+ " cmpq $0, 64(%%r11)\n "
280271 " je 1f\n "
272+ " movq 72(%%r11), %%r8\n "
281273
282- // Arg 6: R9
283- " addq $8, %%r10 \n "
284- " movq (%%r10), %%r9 \n "
285- " addq $8 , %%r10 \n "
274+ // Arg 6: Type is at 80(r11), Value is at 88(r11)
275+ " cmpq $0, 80(%%r11) \n "
276+ " je 1f \n "
277+ " movq 88(%%r11) , %%r9 \n "
286278
287- " 1:\n " // Local label for 'end of args'
288- " xorl %%eax, %%eax\n " // Shorter version of mov $0, %rax
279+ " 1:\n "
280+ " xorl %%eax, %%eax\n "
289281 " call *%[fn_tag]\n "
290282 " movq %%rax, %[result_tag]\n "
291283
292- // Restore stack
293284 " movq %%rbp, %%rsp\n "
294285 " popq %%rbp\n "
295286
296287 : [result_tag] " =r" (result)
297- : [fn_tag] " r" (func. functionPtr ),
298- [args_tag] " r" (args. getBuffer () )
299- : " rax" , " rdi" , " rsi" , " rdx" , " rcx" , " r8" , " r9" , " r10 " , " memory" , " cc"
288+ : [fn_tag] " r" (fn_ptr ),
289+ [args_tag] " r" (args_ptr )
290+ : " rax" , " rdi" , " rsi" , " rdx" , " rcx" , " r8" , " r9" , " r11 " , " memory" , " cc"
300291 );
301292
302293 if (func.returnType == ret_type::Bool){
0 commit comments