Convert two .c files to LF line endings

... matching the other 157 .c files in the tree
master
Larry Doolittle 2017-09-24 16:48:08 -07:00
parent 1443039416
commit 44d4da62bb
2 changed files with 317 additions and 317 deletions

View File

@ -1,41 +1,41 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
asm ( asm (
".text;" ".text;"
".globl _us;.globl _ss;.globl _uc;.globl _sc;" ".globl _us;.globl _ss;.globl _uc;.globl _sc;"
"_us:;_ss:;_uc:;_sc:;" "_us:;_ss:;_uc:;_sc:;"
"movl $0x1234ABCD, %eax;" "movl $0x1234ABCD, %eax;"
"ret;" "ret;"
); );
#if 1 #if 1
#define us _us #define us _us
#define ss _ss #define ss _ss
#define uc _uc #define uc _uc
#define sc _sc #define sc _sc
#endif #endif
int main() int main()
{ {
unsigned short us(void); unsigned short us(void);
short ss(void); short ss(void);
unsigned char uc(void); unsigned char uc(void);
signed char sc(void); signed char sc(void);
unsigned short (*fpus)(void) = us; unsigned short (*fpus)(void) = us;
short (*fpss)(void) = ss; short (*fpss)(void) = ss;
unsigned char (*fpuc)(void) = uc; unsigned char (*fpuc)(void) = uc;
signed char (*fpsc)(void) = sc; signed char (*fpsc)(void) = sc;
printf("%08X %08X\n", us() + 1, fpus() + 1); printf("%08X %08X\n", us() + 1, fpus() + 1);
printf("%08X %08X\n", ss() + 1, fpss() + 1); printf("%08X %08X\n", ss() + 1, fpss() + 1);
printf("%08X %08X\n", uc() + 1, fpuc() + 1); printf("%08X %08X\n", uc() + 1, fpuc() + 1);
printf("%08X %08X\n", sc() + 1, fpsc() + 1); printf("%08X %08X\n", sc() + 1, fpsc() + 1);
printf("\n"); printf("\n");
printf("%08X %08X\n", fpus() + 1, us() + 1); printf("%08X %08X\n", fpus() + 1, us() + 1);
printf("%08X %08X\n", fpss() + 1, ss() + 1); printf("%08X %08X\n", fpss() + 1, ss() + 1);
printf("%08X %08X\n", fpuc() + 1, uc() + 1); printf("%08X %08X\n", fpuc() + 1, uc() + 1);
printf("%08X %08X\n", fpsc() + 1, sc() + 1); printf("%08X %08X\n", fpsc() + 1, sc() + 1);
return 0; return 0;
} }

View File

@ -1,276 +1,276 @@
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#ifndef _WIN32 #ifndef _WIN32
#define __fastcall __attribute((fastcall)) #define __fastcall __attribute((fastcall))
#endif #endif
#if 1 #if 1
#define SYMBOL(x) _##x #define SYMBOL(x) _##x
#else #else
#define SYMBOL(x) x #define SYMBOL(x) x
#endif #endif
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
////////// TRAP FRAMEWORK ////////// TRAP FRAMEWORK
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// if you cast 'TRAP' to a function pointer and call it, // if you cast 'TRAP' to a function pointer and call it,
// it will save all 8 registers, // it will save all 8 registers,
// and jump into C-code (previously set using 'SET_TRAP_HANDLER(x)'), // and jump into C-code (previously set using 'SET_TRAP_HANDLER(x)'),
// in C-code you can pop DWORDs from stack and modify registers // in C-code you can pop DWORDs from stack and modify registers
// //
void *SYMBOL(trap_handler); void *SYMBOL(trap_handler);
extern unsigned char SYMBOL(trap)[]; extern unsigned char SYMBOL(trap)[];
asm ( asm (
".text;" ".text;"
"_trap:;" "_trap:;"
"pushl %esp;" "pushl %esp;"
"pusha;" "pusha;"
"addl $0x4, 0xc(%esp);" "addl $0x4, 0xc(%esp);"
"pushl %esp;" "pushl %esp;"
"call *_trap_handler;" "call *_trap_handler;"
"addl $0x4, %esp;" "addl $0x4, %esp;"
"movl 0xc(%esp), %eax;" "movl 0xc(%esp), %eax;"
"movl %eax, 0x20(%esp);" "movl %eax, 0x20(%esp);"
"popa;" "popa;"
"popl %esp;" "popl %esp;"
"ret;" "ret;"
); );
struct trapframe { struct trapframe {
unsigned edi, esi, ebp, esp, ebx, edx, ecx, eax; unsigned edi, esi, ebp, esp, ebx, edx, ecx, eax;
}; };
#define M_FLOAT(addr) (*(float *)(addr)) #define M_FLOAT(addr) (*(float *)(addr))
#define M_DWORD(addr) (*(unsigned *)(addr)) #define M_DWORD(addr) (*(unsigned *)(addr))
#define M_WORD(addr) (*(unsigned short *)(addr)) #define M_WORD(addr) (*(unsigned short *)(addr))
#define M_BYTE(addr) (*(unsigned char *)(addr)) #define M_BYTE(addr) (*(unsigned char *)(addr))
#define R_EAX ((tf)->eax) #define R_EAX ((tf)->eax)
#define R_ECX ((tf)->ecx) #define R_ECX ((tf)->ecx)
#define R_EDX ((tf)->edx) #define R_EDX ((tf)->edx)
#define R_EBX ((tf)->ebx) #define R_EBX ((tf)->ebx)
#define R_ESP ((tf)->esp) #define R_ESP ((tf)->esp)
#define R_EBP ((tf)->ebp) #define R_EBP ((tf)->ebp)
#define R_ESI ((tf)->esi) #define R_ESI ((tf)->esi)
#define R_EDI ((tf)->edi) #define R_EDI ((tf)->edi)
#define ARG(x) (M_DWORD(R_ESP + (x) * 4)) #define ARG(x) (M_DWORD(R_ESP + (x) * 4))
#define RETN(x) do { \ #define RETN(x) do { \
M_DWORD(R_ESP + (x)) = M_DWORD(R_ESP); \ M_DWORD(R_ESP + (x)) = M_DWORD(R_ESP); \
R_ESP += (x); \ R_ESP += (x); \
} while (0) } while (0)
#define DUMP() do { \ #define DUMP() do { \
unsigned i; \ unsigned i; \
printf("EAX: %08X\n", R_EAX); \ printf("EAX: %08X\n", R_EAX); \
printf("ECX: %08X\n", R_ECX); \ printf("ECX: %08X\n", R_ECX); \
printf("EDX: %08X\n", R_EDX); \ printf("EDX: %08X\n", R_EDX); \
printf("EBX: %08X\n", R_EBX); \ printf("EBX: %08X\n", R_EBX); \
printf("ESP: %08X\n", R_ESP); \ printf("ESP: %08X\n", R_ESP); \
printf("EBP: %08X\n", R_EBP); \ printf("EBP: %08X\n", R_EBP); \
printf("ESI: %08X\n", R_ESI); \ printf("ESI: %08X\n", R_ESI); \
printf("EDI: %08X\n", R_EDI); \ printf("EDI: %08X\n", R_EDI); \
printf("\n"); \ printf("\n"); \
printf("[RETADDR]: %08X\n", M_DWORD(R_ESP)); \ printf("[RETADDR]: %08X\n", M_DWORD(R_ESP)); \
for (i = 1; i <= 8; i++) { \ for (i = 1; i <= 8; i++) { \
printf("[ARG%4d]: %08X\n", i, ARG(i)); \ printf("[ARG%4d]: %08X\n", i, ARG(i)); \
} \ } \
} while (0) } while (0)
#define SET_TRAP_HANDLER(x) ((SYMBOL(trap_handler)) = (x)) #define SET_TRAP_HANDLER(x) ((SYMBOL(trap_handler)) = (x))
#define TRAP ((void *) &SYMBOL(trap)) #define TRAP ((void *) &SYMBOL(trap))
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
////////// SAFECALL FRAMEWORK ////////// SAFECALL FRAMEWORK
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// this framwork will convert any calling convention to cdecl // this framwork will convert any calling convention to cdecl
// usage: first set call target with 'SET_SAFECALL_TARGET(x)' // usage: first set call target with 'SET_SAFECALL_TARGET(x)'
// then cast 'SAFECALL' to target funtion pointer type and invoke it // then cast 'SAFECALL' to target funtion pointer type and invoke it
// after calling, 'ESPDIFF' is the differance of old and new esp // after calling, 'ESPDIFF' is the differance of old and new esp
void *SYMBOL(sc_call_target); void *SYMBOL(sc_call_target);
unsigned SYMBOL(sc_retn_addr); unsigned SYMBOL(sc_retn_addr);
unsigned SYMBOL(sc_old_esp); unsigned SYMBOL(sc_old_esp);
unsigned SYMBOL(sc_new_esp); unsigned SYMBOL(sc_new_esp);
extern unsigned char SYMBOL(safecall)[]; extern unsigned char SYMBOL(safecall)[];
asm ( asm (
".text;" ".text;"
"_safecall:;" "_safecall:;"
"popl _sc_retn_addr;" "popl _sc_retn_addr;"
"movl %esp, _sc_old_esp;" "movl %esp, _sc_old_esp;"
"call *_sc_call_target;" "call *_sc_call_target;"
"movl %esp, _sc_new_esp;" "movl %esp, _sc_new_esp;"
"movl _sc_old_esp, %esp;" "movl _sc_old_esp, %esp;"
"jmp *_sc_retn_addr;" "jmp *_sc_retn_addr;"
); );
#define SET_SAFECALL_TARGET(x) ((SYMBOL(sc_call_target)) = (x)) #define SET_SAFECALL_TARGET(x) ((SYMBOL(sc_call_target)) = (x))
#define SAFECALL ((void *) &SYMBOL(safecall)) #define SAFECALL ((void *) &SYMBOL(safecall))
#define ESPDIFF (SYMBOL(sc_new_esp) - SYMBOL(sc_old_esp)) #define ESPDIFF (SYMBOL(sc_new_esp) - SYMBOL(sc_old_esp))
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
////////// TEST FASTCALL INVOKE ////////// TEST FASTCALL INVOKE
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
void check_fastcall_invoke_0(struct trapframe *tf) void check_fastcall_invoke_0(struct trapframe *tf)
{ {
//DUMP(); //DUMP();
RETN(0); RETN(0);
} }
void check_fastcall_invoke_1(struct trapframe *tf) void check_fastcall_invoke_1(struct trapframe *tf)
{ {
//DUMP(); //DUMP();
assert(R_ECX == 0x11111111); assert(R_ECX == 0x11111111);
RETN(0); RETN(0);
} }
void check_fastcall_invoke_2(struct trapframe *tf) void check_fastcall_invoke_2(struct trapframe *tf)
{ {
//DUMP(); //DUMP();
assert(R_ECX == 0x11111111); assert(R_ECX == 0x11111111);
assert(R_EDX == 0x22222222); assert(R_EDX == 0x22222222);
RETN(0); RETN(0);
} }
void check_fastcall_invoke_3(struct trapframe *tf) void check_fastcall_invoke_3(struct trapframe *tf)
{ {
//DUMP(); //DUMP();
assert(R_ECX == 0x11111111); assert(R_ECX == 0x11111111);
assert(R_EDX == 0x22222222); assert(R_EDX == 0x22222222);
assert(ARG(1) == 0x33333333); assert(ARG(1) == 0x33333333);
RETN(1*4); RETN(1*4);
} }
void check_fastcall_invoke_4(struct trapframe *tf) void check_fastcall_invoke_4(struct trapframe *tf)
{ {
//DUMP(); //DUMP();
assert(R_ECX == 0x11111111); assert(R_ECX == 0x11111111);
assert(R_EDX == 0x22222222); assert(R_EDX == 0x22222222);
assert(ARG(1) == 0x33333333); assert(ARG(1) == 0x33333333);
assert(ARG(2) == 0x44444444); assert(ARG(2) == 0x44444444);
RETN(2*4); RETN(2*4);
} }
void check_fastcall_invoke_5(struct trapframe *tf) void check_fastcall_invoke_5(struct trapframe *tf)
{ {
//DUMP(); //DUMP();
assert(R_ECX == 0x11111111); assert(R_ECX == 0x11111111);
assert(R_EDX == 0x22222222); assert(R_EDX == 0x22222222);
assert(ARG(1) == 0x33333333); assert(ARG(1) == 0x33333333);
assert(ARG(2) == 0x44444444); assert(ARG(2) == 0x44444444);
assert(ARG(3) == 0x55555555); assert(ARG(3) == 0x55555555);
RETN(3*4); RETN(3*4);
} }
void test_fastcall_invoke() void test_fastcall_invoke()
{ {
SET_TRAP_HANDLER(check_fastcall_invoke_0); SET_TRAP_HANDLER(check_fastcall_invoke_0);
((void __fastcall (*)(void)) TRAP)(); ((void __fastcall (*)(void)) TRAP)();
SET_TRAP_HANDLER(check_fastcall_invoke_1); SET_TRAP_HANDLER(check_fastcall_invoke_1);
((void __fastcall (*)(unsigned)) TRAP)(0x11111111); ((void __fastcall (*)(unsigned)) TRAP)(0x11111111);
SET_TRAP_HANDLER(check_fastcall_invoke_2); SET_TRAP_HANDLER(check_fastcall_invoke_2);
((void __fastcall (*)(unsigned, unsigned)) TRAP)(0x11111111, 0x22222222); ((void __fastcall (*)(unsigned, unsigned)) TRAP)(0x11111111, 0x22222222);
SET_TRAP_HANDLER(check_fastcall_invoke_3); SET_TRAP_HANDLER(check_fastcall_invoke_3);
((void __fastcall (*)(unsigned, unsigned, unsigned)) TRAP)(0x11111111, 0x22222222, 0x33333333); ((void __fastcall (*)(unsigned, unsigned, unsigned)) TRAP)(0x11111111, 0x22222222, 0x33333333);
SET_TRAP_HANDLER(check_fastcall_invoke_4); SET_TRAP_HANDLER(check_fastcall_invoke_4);
((void __fastcall (*)(unsigned, unsigned, unsigned, unsigned)) TRAP)(0x11111111, 0x22222222, 0x33333333, 0x44444444); ((void __fastcall (*)(unsigned, unsigned, unsigned, unsigned)) TRAP)(0x11111111, 0x22222222, 0x33333333, 0x44444444);
SET_TRAP_HANDLER(check_fastcall_invoke_5); SET_TRAP_HANDLER(check_fastcall_invoke_5);
((void __fastcall (*)(unsigned, unsigned, unsigned, unsigned, unsigned)) TRAP)(0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555); ((void __fastcall (*)(unsigned, unsigned, unsigned, unsigned, unsigned)) TRAP)(0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555);
} }
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
////////// TEST FUNCTION CODE GENERATION ////////// TEST FUNCTION CODE GENERATION
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
int __fastcall check_fastcall_espdiff_0(void) int __fastcall check_fastcall_espdiff_0(void)
{ {
return 0; return 0;
} }
int __fastcall check_fastcall_espdiff_1(int a) int __fastcall check_fastcall_espdiff_1(int a)
{ {
return a; return a;
} }
int __fastcall check_fastcall_espdiff_2(int a, int b) int __fastcall check_fastcall_espdiff_2(int a, int b)
{ {
return a + b; return a + b;
} }
int __fastcall check_fastcall_espdiff_3(int a, int b, int c) int __fastcall check_fastcall_espdiff_3(int a, int b, int c)
{ {
return a + b + c; return a + b + c;
} }
int __fastcall check_fastcall_espdiff_4(int a, int b, int c, int d) int __fastcall check_fastcall_espdiff_4(int a, int b, int c, int d)
{ {
return a + b + c + d; return a + b + c + d;
} }
int __fastcall check_fastcall_espdiff_5(int a, int b, int c, int d, int e) int __fastcall check_fastcall_espdiff_5(int a, int b, int c, int d, int e)
{ {
return a + b + c + d + e; return a + b + c + d + e;
} }
void test_fastcall_espdiff() void test_fastcall_espdiff()
{ {
int x; int x;
SET_SAFECALL_TARGET(check_fastcall_espdiff_0); SET_SAFECALL_TARGET(check_fastcall_espdiff_0);
x = ((typeof(&check_fastcall_espdiff_0))SAFECALL)(); x = ((typeof(&check_fastcall_espdiff_0))SAFECALL)();
assert(x == 0); assert(x == 0);
assert(ESPDIFF == 0); assert(ESPDIFF == 0);
SET_SAFECALL_TARGET(check_fastcall_espdiff_1); SET_SAFECALL_TARGET(check_fastcall_espdiff_1);
x = ((typeof(&check_fastcall_espdiff_1))SAFECALL)(1); x = ((typeof(&check_fastcall_espdiff_1))SAFECALL)(1);
assert(x == 1); assert(x == 1);
assert(ESPDIFF == 0); assert(ESPDIFF == 0);
SET_SAFECALL_TARGET(check_fastcall_espdiff_2); SET_SAFECALL_TARGET(check_fastcall_espdiff_2);
x = ((typeof(&check_fastcall_espdiff_2))SAFECALL)(1, 2); x = ((typeof(&check_fastcall_espdiff_2))SAFECALL)(1, 2);
assert(x == 1 + 2); assert(x == 1 + 2);
assert(ESPDIFF == 0); assert(ESPDIFF == 0);
SET_SAFECALL_TARGET(check_fastcall_espdiff_3); SET_SAFECALL_TARGET(check_fastcall_espdiff_3);
x = ((typeof(&check_fastcall_espdiff_3))SAFECALL)(1, 2, 3); x = ((typeof(&check_fastcall_espdiff_3))SAFECALL)(1, 2, 3);
assert(x == 1 + 2 + 3); assert(x == 1 + 2 + 3);
assert(ESPDIFF == 1*4); assert(ESPDIFF == 1*4);
SET_SAFECALL_TARGET(check_fastcall_espdiff_4); SET_SAFECALL_TARGET(check_fastcall_espdiff_4);
x = ((typeof(&check_fastcall_espdiff_4))SAFECALL)(1, 2, 3, 4); x = ((typeof(&check_fastcall_espdiff_4))SAFECALL)(1, 2, 3, 4);
assert(x == 1 + 2 + 3 + 4); assert(x == 1 + 2 + 3 + 4);
assert(ESPDIFF == 2*4); assert(ESPDIFF == 2*4);
SET_SAFECALL_TARGET(check_fastcall_espdiff_5); SET_SAFECALL_TARGET(check_fastcall_espdiff_5);
x = ((typeof(&check_fastcall_espdiff_5))SAFECALL)(1, 2, 3, 4, 5); x = ((typeof(&check_fastcall_espdiff_5))SAFECALL)(1, 2, 3, 4, 5);
assert(x == 1 + 2 + 3 + 4 + 5); assert(x == 1 + 2 + 3 + 4 + 5);
assert(ESPDIFF == 3*4); assert(ESPDIFF == 3*4);
} }
int main() int main()
{ {
#define N 10000 #define N 10000
int i; int i;
for (i = 1; i <= N; i++) { for (i = 1; i <= N; i++) {
test_fastcall_espdiff(); test_fastcall_espdiff();
} }
for (i = 1; i <= N; i++) { for (i = 1; i <= N; i++) {
test_fastcall_invoke(); test_fastcall_invoke();
} }
puts("TEST OK"); puts("TEST OK");
return 0; return 0;
} }