comparison dyncall/dyncall_callvm_x86.c @ 84:67961454902b

- bigger cleanup in callvm code * changed init of most callvms to reuse code and for consistency * removed unused functions * general changes for consistency * added some missing cconv defines do mode calls - fixed potential buffer overrun on arm64 - fixed sparc and x64 mode setting (only one mode, but wasn't (pointlessly) resettable) - error code handling (dcGetError) changes, flag is now cleared (work still underway) - Changelog update
author cslag
date Wed, 06 Apr 2016 00:21:51 +0200
parents 3e629dc19168
children f5577f6bf97a
comparison
equal deleted inserted replaced
83:54930a037e8a 84:67961454902b
28 #include "dyncall_callvm_x86.h" 28 #include "dyncall_callvm_x86.h"
29 #include "dyncall_alloc.h" 29 #include "dyncall_alloc.h"
30 30
31 31
32 void dc_callvm_mode_x86(DCCallVM* in_self, DCint mode); 32 void dc_callvm_mode_x86(DCCallVM* in_self, DCint mode);
33
34 /* call vm allocator */
35
36 static DCCallVM* dc_callvm_new_x86(DCCallVM_vt* vt, DCsize size)
37 {
38 DCCallVM_x86* self = (DCCallVM_x86*) dcAllocMem( sizeof(DCCallVM_x86)+size );
39
40 dc_callvm_base_init(&self->mInterface, vt);
41
42 self->mIntRegs = 0;
43 dcVecInit(&self->mVecHead, size);
44 return (DCCallVM*) self;
45 }
46 33
47 /* call vm destructor */ 34 /* call vm destructor */
48 35
49 static void dc_callvm_free_x86(DCCallVM* in_self) 36 static void dc_callvm_free_x86(DCCallVM* in_self)
50 { 37 {
184 , (DCdoublevmfunc*) &dc_callvm_call_x86_plan9 171 , (DCdoublevmfunc*) &dc_callvm_call_x86_plan9
185 , (DCpointervmfunc*) &dc_callvm_call_x86_plan9 172 , (DCpointervmfunc*) &dc_callvm_call_x86_plan9
186 , NULL /* callStruct */ 173 , NULL /* callStruct */
187 }; 174 };
188 175
189 DCCallVM* dcNewCallVM_x86_plan9(DCsize size)
190 {
191 return dc_callvm_new_x86( &gVT_x86_plan9, size );
192 }
193
194 176
195 #else 177 #else
196 178
197 179
198 /* call 'cdecl' */ 180 /* call 'cdecl' */
229 , (DCdoublevmfunc*) &dc_callvm_call_x86_cdecl 211 , (DCdoublevmfunc*) &dc_callvm_call_x86_cdecl
230 , (DCpointervmfunc*) &dc_callvm_call_x86_cdecl 212 , (DCpointervmfunc*) &dc_callvm_call_x86_cdecl
231 , NULL /* callStruct */ 213 , NULL /* callStruct */
232 }; 214 };
233 215
234 DCCallVM* dcNewCallVM_x86_cdecl(DCsize size)
235 {
236 return dc_callvm_new_x86( &gVT_x86_cdecl, size );
237 }
238
239 216
240 217
241 /* --- stdcall -------------------------------------------------------------- */ 218 /* --- stdcall -------------------------------------------------------------- */
242 219
243 /* call win32/std */ 220 /* call win32/std */
276 , (DCdoublevmfunc*) &dc_callvm_call_x86_win32_std 253 , (DCdoublevmfunc*) &dc_callvm_call_x86_win32_std
277 , (DCpointervmfunc*) &dc_callvm_call_x86_win32_std 254 , (DCpointervmfunc*) &dc_callvm_call_x86_win32_std
278 , NULL /* callStruct */ 255 , NULL /* callStruct */
279 }; 256 };
280 257
281 /* win32/std callvm allocator */
282
283 DCCallVM* dcNewCallVM_x86_win32_std(DCsize size)
284 {
285 return dc_callvm_new_x86( &gVT_x86_win32_std, size );
286 }
287 258
288 /* --- fastcall common (ms/gnu) -------------------------------------------- */ 259 /* --- fastcall common (ms/gnu) -------------------------------------------- */
289 260
290 /* call win32 ms fast */ 261 /* call win32 ms fast */
291 262
387 , (DCdoublevmfunc*) &dc_callvm_call_x86_win32_fast 358 , (DCdoublevmfunc*) &dc_callvm_call_x86_win32_fast
388 , (DCpointervmfunc*) &dc_callvm_call_x86_win32_fast 359 , (DCpointervmfunc*) &dc_callvm_call_x86_win32_fast
389 , NULL /* callStruct */ 360 , NULL /* callStruct */
390 }; 361 };
391 362
392 DCCallVM* dcNewCallVM_x86_win32_fast_ms(DCsize size)
393 {
394 return dc_callvm_new_x86( &gVT_x86_win32_fast_ms, size );
395 }
396 363
397 /* --- gnu fastcall -------------------------------------------------------- */ 364 /* --- gnu fastcall -------------------------------------------------------- */
398 365
399 /* arg int - probably hold in ECX and EDX */ 366 /* arg int - probably hold in ECX and EDX */
400 367
485 , (DCdoublevmfunc*) &dc_callvm_call_x86_win32_fast 452 , (DCdoublevmfunc*) &dc_callvm_call_x86_win32_fast
486 , (DCpointervmfunc*) &dc_callvm_call_x86_win32_fast 453 , (DCpointervmfunc*) &dc_callvm_call_x86_win32_fast
487 , NULL /* callStruct */ 454 , NULL /* callStruct */
488 }; 455 };
489 456
490 DCCallVM* dcNewCallVM_x86_win32_fast_gnu(DCsize size)
491 {
492 return dc_callvm_new_x86( &gVT_x86_win32_fast_gnu, size );
493 }
494 457
495 /* --- this ms ------------------------------------------------------------- */ 458 /* --- this ms ------------------------------------------------------------- */
496 459
497 /* call win32/this/ms */ 460 /* call win32/this/ms */
498 461
603 , (DCpointervmfunc*) &dc_callvm_call_x86_sys_int80h_bsd 566 , (DCpointervmfunc*) &dc_callvm_call_x86_sys_int80h_bsd
604 , NULL /* callStruct */ 567 , NULL /* callStruct */
605 }; 568 };
606 569
607 570
608 /* win32/this/ms callvm allocator */
609
610 DCCallVM* dcNewCallVM_x86_win32_this_ms(DCsize size)
611 {
612 return dc_callvm_new_x86( &gVT_x86_win32_this_ms, size );
613 }
614
615 #endif 571 #endif
616 572
617 573
618 /* mode */ 574 /* mode */
619 575
620 void dc_callvm_mode_x86(DCCallVM* in_self, DCint mode) 576 void dc_callvm_mode_x86(DCCallVM* in_self, DCint mode)
621 { 577 {
622 DCCallVM_x86* self = (DCCallVM_x86*) in_self; 578 DCCallVM_x86* self = (DCCallVM_x86*)in_self;
623 DCCallVM_vt* vt; 579 DCCallVM_vt* vt;
580
624 switch(mode) { 581 switch(mode) {
582 case DC_CALL_C_DEFAULT:
625 case DC_CALL_C_ELLIPSIS: 583 case DC_CALL_C_ELLIPSIS:
626 case DC_CALL_C_ELLIPSIS_VARARGS: 584 case DC_CALL_C_ELLIPSIS_VARARGS:
627 case DC_CALL_C_DEFAULT: 585 /* Plan9 (and forks) have their own calling convention (and no support for foreign ones). */
628 #if defined(DC_PLAN9) /* Plan9 (and forks) have their own calling convention (and no support for foreign ones). */ 586 #if defined(DC_PLAN9)
629 case DC_CALL_C_X86_PLAN9: vt = &gVT_x86_plan9; break; 587 case DC_CALL_C_X86_PLAN9: vt = &gVT_x86_plan9; break;
630 #else 588 #else
631 case DC_CALL_C_X86_CDECL: vt = &gVT_x86_cdecl; break; 589 case DC_CALL_C_X86_CDECL: vt = &gVT_x86_cdecl; break;
632 case DC_CALL_C_X86_WIN32_STD: vt = &gVT_x86_win32_std; break; 590 case DC_CALL_C_X86_WIN32_STD: vt = &gVT_x86_win32_std; break;
633 case DC_CALL_C_X86_WIN32_FAST_MS: vt = &gVT_x86_win32_fast_ms; break; 591 case DC_CALL_C_X86_WIN32_FAST_MS: vt = &gVT_x86_win32_fast_ms; break;
637 case DC_CALL_SYS_DEFAULT: 595 case DC_CALL_SYS_DEFAULT:
638 # if defined DC_UNIX 596 # if defined DC_UNIX
639 # if defined DC__OS_Linux 597 # if defined DC__OS_Linux
640 vt = &gVT_x86_sys_int80h_linux; break; 598 vt = &gVT_x86_sys_int80h_linux; break;
641 # else 599 # else
642 vt = &gVT_x86_sys_int80h_bsd; break; 600 vt = &gVT_x86_sys_int80h_bsd; break;
643 # endif 601 # endif
644 # else 602 # else
645 self->mInterface.mError = DC_ERROR_UNSUPPORTED_MODE; return; 603 self->mInterface.mError = DC_ERROR_UNSUPPORTED_MODE; return;
646 # endif 604 # endif
647 case DC_CALL_SYS_X86_INT80H_LINUX: 605 case DC_CALL_SYS_X86_INT80H_LINUX: vt = &gVT_x86_sys_int80h_linux; break;
648 vt = &gVT_x86_sys_int80h_linux; break; 606 case DC_CALL_SYS_X86_INT80H_BSD: vt = &gVT_x86_sys_int80h_bsd; break;
649 case DC_CALL_SYS_X86_INT80H_BSD:
650 vt = &gVT_x86_sys_int80h_bsd; break;
651 #endif 607 #endif
652 default: 608 default:
653 self->mInterface.mError = DC_ERROR_UNSUPPORTED_MODE; return; 609 self->mInterface.mError = DC_ERROR_UNSUPPORTED_MODE;
610 return;
654 } 611 }
655 self->mInterface.mVTpointer = vt; 612 dc_callvm_base_init(&self->mInterface, vt);
656 } 613 }
657 614
658 /* new */ 615 /* Public API. */
659
660 DCCallVM* dcNewCallVM(DCsize size) 616 DCCallVM* dcNewCallVM(DCsize size)
661 { 617 {
662 #if defined(DC__OS_Plan9) 618 DCCallVM_x86* p = (DCCallVM_x86*)dcAllocMem(sizeof(DCCallVM_x86)+size);
663 return dcNewCallVM_x86_plan9(size); 619
664 #else 620 dc_callvm_mode_x86((DCCallVM*)p, DC_CALL_C_DEFAULT);
665 return dcNewCallVM_x86_cdecl(size); 621
666 #endif 622 dcVecInit(&p->mVecHead, size);
667 } 623 dc_callvm_reset_x86((DCCallVM*)p);
668 624
625 return (DCCallVM*)p;
626 }
627