From 6909c317ef25cbe793ec651526d615e4f88c10f4 Mon Sep 17 00:00:00 2001 From: pavelyakov Date: Fri, 13 Jul 2018 17:30:22 +0000 Subject: [PATCH] Libraries for compiling Python programs! git-svn-id: svn://kolibrios.org@7301 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/pythonLibraryCompiler/function.h | 60 +++ programs/cmm/pythonLibraryCompiler/head.h | 64 +++ programs/cmm/pythonLibraryCompiler/init.h | 221 +++++++++++ programs/cmm/pythonLibraryCompiler/opcode.h | 374 ++++++++++++++++++ 4 files changed, 719 insertions(+) create mode 100644 programs/cmm/pythonLibraryCompiler/function.h create mode 100644 programs/cmm/pythonLibraryCompiler/head.h create mode 100644 programs/cmm/pythonLibraryCompiler/init.h create mode 100644 programs/cmm/pythonLibraryCompiler/opcode.h diff --git a/programs/cmm/pythonLibraryCompiler/function.h b/programs/cmm/pythonLibraryCompiler/function.h new file mode 100644 index 0000000000..a639158d95 --- /dev/null +++ b/programs/cmm/pythonLibraryCompiler/function.h @@ -0,0 +1,60 @@ + +:void _stdout(dword txt) +{ + dword addr = txt; + while(DSBYTE[addr]) addr++; + CreateFile(addr-txt,txt,"/sys/stdout"); +} + +:dword stdcall std_stdout(dword count) +{ + _stdout(_str(popFast())); +} + +:dword stdcall std_str(dword c,v) +{ + dword n = 0; + dword m = 0; + dword l = 0; + + if(DSBYTE[v+4] == PY_STR) + { + return v; + } + + n = malloc(30); + m = malloc(MEMBUF); + + DSDWORD[n] = m; + DSBYTE[n+4] = PY_STR; + DSDWORD[n+9] = MEMBUF; + DSDWORD[n+13] = HASH; + HASH++; + + switch(DSBYTE[v+4]) + { + case PY_BOOL: + + if(DSDWORD[v]) + { + strlcpy(m,"True",4); + DSDWORD[n+5] = 4; + } + else + { + strlcpy(m,"False",5); + DSDWORD[n+5] = 5; + } + + break; + case PY_INT: + l = itoa(DSDWORD[v],m); + DSDWORD[n+5] = l; + break; + case PY_NONE: + strlcpy(m,"None",4); + DSDWORD[n+5] = 4; + break; + } + return n; +} \ No newline at end of file diff --git a/programs/cmm/pythonLibraryCompiler/head.h b/programs/cmm/pythonLibraryCompiler/head.h new file mode 100644 index 0000000000..36f7e80a89 --- /dev/null +++ b/programs/cmm/pythonLibraryCompiler/head.h @@ -0,0 +1,64 @@ +#pragma option OST +#pragma option ON +#pragma option cri- +#pragma option -CPA +#initallvar 0 +#jumptomain FALSE + +#startaddress 65536 + +#code32 TRUE + +#define PY_NONE 0 +#define PY_BOOL 1 +#define PY_INT 2 +#define PY_FLT 3 +#define PY_STR 4 +#define PY_FNC 6 +#define PY_CPL 7 +#define PY_LST 8 +#define PY_TPL 9 +#define PY_RNG 10 +#define PY_BTS 11 +#define PY_BTA 12 +#define PY_MVW 13 +#define PY_SET 14 +#define PY_FST 15 +#define PY_DCT 16 + +#define PY_ADD 0 +#define PY_POW 1 +#define PY_MUL 2 +#define PY_SUB 3 +#define PY_DIV 4 +#define PY_MOD 5 +#define PY_XOR 6 +#define PY_AND 7 +#define PY__OR 8 +#define PY_LSH 9 +#define PY_RSH 10 + +#define MEMBUF 0xF + +char os_name[8] = {'M','E','N','U','E','T','0','1'}; +dword os_version = 0x00000001; +dword start_addr = #______INIT______; +dword final_addr = #______STOP______+32; +dword alloc_mem = 160000; +dword x86esp_reg = 160000; +dword I_Param = #param; +dword I_Path = #program_path; +char param[4096]={0}; +char program_path[4096]={0}; + +dword HASH = 0; + +:struct f70{ + dword func; + dword param1; + dword param2; + dword param3; + dword param4; + char rezerv; + dword name; +}; diff --git a/programs/cmm/pythonLibraryCompiler/init.h b/programs/cmm/pythonLibraryCompiler/init.h new file mode 100644 index 0000000000..7af5b7d10f --- /dev/null +++ b/programs/cmm/pythonLibraryCompiler/init.h @@ -0,0 +1,221 @@ +void ExitProcess() +{ + EAX = -1; + $int 0x40 +} + +:f70 write_file_70; +:int CreateFile(dword data_size, buffer, file_path) +{ + write_file_70.func = 2; + write_file_70.param1 = 0; + write_file_70.param2 = 0; + write_file_70.param3 = data_size; + write_file_70.param4 = buffer; + write_file_70.rezerv = 0; + write_file_70.name = file_path; + $mov eax,70 + $mov ebx,#write_file_70.func + $int 0x40 +} + +dword malloc(dword size) +{ + $push ebx + $push ecx + + $mov eax, 68 + $mov ebx, 12 + $mov ecx, size + $int 0x40 + + $pop ecx + $pop ebx + return EAX; +} +dword free(dword mptr) +{ + $push ebx + $push ecx + + $mov eax, 68 + $mov ebx, 13 + $mov ecx, mptr + $test ecx, ecx + $jz end0 + $int 0x40 + @end0: + $pop ecx + $pop ebx + return 0; +} +:dword sleep(dword x) +{ + EAX = 5; + EBX = x; + $int 0x40 +} +:dword mem_init() +{ + $push ebx + $mov eax, 68 + $mov ebx, 11 + $int 0x40 + + $pop ebx + return EAX; +} + +:dword itoa(signed long number,dword p) +{ + dword ret=0; + byte cmd=0; + long mask=0; + long tmp=0; + IF(!number) + { + DSBYTE[p] = '0'; + p++; + DSBYTE[p] = 0; + return 1; + } + mask = 1000000000; + cmd = 1; + if(!number){ + ESBYTE[p] = '0'; + ESBYTE[p+1] = 0; + return p; + } + ret = p; + if(number<0) + { + $neg number + ESBYTE[p] = '-'; + $inc p + } + while(mask) + { + tmp = number / mask; + tmp = tmp%10; + + if(cmd){ + if(tmp){ + ESBYTE[p] = tmp + '0'; + $inc p + cmd = 0; + } + } + else { + ESBYTE[p] = tmp + '0'; + $inc p + } + mask /= 10; + } + ESBYTE[p] = 0; + return p - ret; +} + +:void strlcpy(dword s1,s2,l) +{ + WHILE(l) + { + DSBYTE[s1] = DSBYTE[s2]; + s1++; + s2++; + l--; + } +} + +:dword _str(dword v) +{ + switch(DSBYTE[v+4]) + { + case PY_STR: + return DSDWORD[v]; + break; + case PY_BOOL: + if(DSDWORD[v])return "True"; + return "False"; + break; + + } +} + +:dword stack = 0; + +:dword popStack() +{ + stack-=4; + return DSDWORD[stack]; +} +:void pushStack(dword x) +{ + DSDWORD[stack] = x; + stack+=4; +} +:dword stackFast = 0; +:dword popFast() +{ + stackFast-=4; + return DSDWORD[stackFast]; +} +:void pushFast(dword x) +{ + DSDWORD[stackFast] = x; + stackFast+=4; +} + +:dword stackWhile = 0; + +:dword popWhile() +{ + stackWhile-=4; + return DSDWORD[stackWhile]; +} +:void pushWhile(dword x) +{ + DSDWORD[stackWhile] = x; + stackWhile+=4; +} + +:void checkType(dword x,y) +{ + if(DSBYTE[x+4]!=DSBYTE[y+4]) + { + stdout("Error type!"); + ExitProcess(); + } +} + + +:byte BUFTST[15] = {0}; +:void test(dword x,c) +{ + if(c) + { + itoa(x,#BUFTST); + _stdout(#BUFTST); + } + else _stdout(x); + EAX = -1; + $int 0x40; +} + +void ______INIT______() +{ + mem_init(); + + HASH = 0; + + stack = malloc(1000); + stackFast = malloc(1000); + stackWhile = malloc(1000); + + main(); + ExitProcess(); +} + +void ______STOP______() +{ + ExitProcess(); +} \ No newline at end of file diff --git a/programs/cmm/pythonLibraryCompiler/opcode.h b/programs/cmm/pythonLibraryCompiler/opcode.h new file mode 100644 index 0000000000..3b37280b06 --- /dev/null +++ b/programs/cmm/pythonLibraryCompiler/opcode.h @@ -0,0 +1,374 @@ +#define buffer 1024 + +:struct VAR +{ + dword data; + byte type; + dword length; + dword stack; +}; + + +:void load_const(dword data,type,length) +{ + dword m = 0; + dword v = 0; + dword i = 0; + dword b = 0; + dword o = 0; + dword t = 0; + v = malloc(30); + switch(type) + { + case PY_STR: + b = length+MEMBUF; + m = malloc(b); + t = m; + i = 0; + while(i= DSDWORD[y]) DSDWORD[n] = 1; + BREAK; + CASE 3: + IF(DSDWORD[x] <= DSDWORD[y]) DSDWORD[n] = 1; + BREAK; + CASE 4: + IF(DSDWORD[x] > DSDWORD[y]) DSDWORD[n] = 1; + BREAK; + CASE 5: + IF(DSDWORD[x] < DSDWORD[y]) DSDWORD[n] = 1; + BREAK; + } + pushStack(n); +} +:byte pop_jump_if(byte cmd) +{ + dword x = 0; + + x = popStack(); + //test(DSDWORD[x+4],1); + SWITCH(DSBYTE[x+4]) + { + CASE PY_INT: + CASE PY_BOOL: + IF(cmd){ IF (DSDWORD[x]) RETURN 1;} + IF(!cmd){ IF (!DSDWORD[x]) RETURN 1;} + BREAK; + CASE PY_STR: + IF(cmd){ IF (DSDWORD[x+5]) RETURN 1;} + IF(!cmd){ IF (!DSDWORD[x+5]) RETURN 1;} + BREAK; + } + RETURN 0; +} +:void pop_block() +{ + stackWhile-=4; +} +:void binary(byte cmd) +{ + dword x = 0; + dword y = 0; + dword l1 = 0; + dword l2 = 0; + dword i = 0; + dword n = 0; + byte type = 0; + dword buffer = 0; + dword length = 0; + dword position = 0; + + y = popStack(); + x = popStack(); + + IF(cmd==PY_ADD) checkType(x,y); + + n = malloc(30); + + type = DSBYTE[x+4]; + DSBYTE[n+4] = type; + + if(type==PY_STR) + { + + l1 = DSDWORD[x+5]; + + IF(cmd==PY_ADD) l2 = DSDWORD[y+5]; + ELSE + { + i = DSDWORD[y]; + l2 = i*l1; + } + + length = l1+l2; + + buffer = length+MEMBUF; + + position = malloc(buffer); + DSDWORD[n] = position; + + DSDWORD[n+5] = length; + DSDWORD[n+9] = buffer; + + IF(cmd==PY_ADD) + { + strlcpy(position,DSDWORD[x],l1); + strlcpy(position+l1,DSDWORD[y],l2); + } + ELSE IF(cmd==PY_POW) + { + WHILE(i) + { + strlcpy(position,DSDWORD[x],l1); + position+=l1; + i--; + } + } + goto RETURN_BINARY; + } + if(type==PY_INT) + { + IF(cmd==PY_ADD) + { + DSDWORD[n] = DSDWORD[x] + DSDWORD[y]; + goto RETURN_BINARY; + } + IF(cmd==PY_POW) + { + length = DSDWORD[y]; + position = DSDWORD[x]; + DSDWORD[n] = 1; + WHILE(length) {DSDWORD[n]*=position; length--;} + goto RETURN_BINARY; + } + IF(cmd==PY_SUB) + { + DSDWORD[n] = DSDWORD[x] - DSDWORD[y]; + goto RETURN_BINARY; + } + IF(cmd==PY_MUL) + { + DSDWORD[n] = DSDWORD[x] * DSDWORD[y]; + goto RETURN_BINARY; + } + IF(cmd==PY_MOD) + { + DSDWORD[n] = DSDWORD[x] % DSDWORD[y]; + goto RETURN_BINARY; + } + IF(cmd==PY_XOR) + { + DSDWORD[n] = DSDWORD[x] ^ DSDWORD[y]; + goto RETURN_BINARY; + } + IF(cmd==PY_AND) + { + DSDWORD[n] = DSDWORD[x] & DSDWORD[y]; + goto RETURN_BINARY; + } + IF(cmd==PY__OR) + { + DSDWORD[n] = DSDWORD[x] | DSDWORD[y]; + goto RETURN_BINARY; + } + IF(cmd==PY_LSH) + { + DSDWORD[n] = DSDWORD[x] << DSDWORD[y]; + goto RETURN_BINARY; + } + IF(cmd==PY_RSH) + { + DSDWORD[n] = DSDWORD[x] >> DSDWORD[y]; + goto RETURN_BINARY; + } + } + RETURN_BINARY: + pushStack(n); +} + +:byte del(dword x) +{ + IF(x) + { + IF(DSDWORD[x+9]) free(DSDWORD[x]); + free(x); + + RETURN 1; + } + RETURN 0; +} +:void make_function() +{ + stack-=4; +} +:dword store_name(dword x) +{ + dword z = 0; + dword y = 0; + y = popStack(); + z = DSDWORD[x]; + IF(z) IF(y != z) del(z); + DSDWORD[x] = y; + RETURN y; +} + +:void load_name(dword addr) +{ + pushStack(addr); +} +:void pop_top() +{ + stack-=4; +} +:void load_fast(dword addr) +{ + popFast(); + DSDWORD[addr] = EAX; + pushStack(EAX); +} +:void call_function(dword arg) +{ + dword x = 0; + + dword c = arg; + dword o = 0; + + dword v = 0; + + WHILE(c) + { + popStack(); + pushFast(EAX); + c--; + } + + v = popStack(); + + IF(DSBYTE[v+4]!=PY_FNC) test("No function!",0); + + v = DSDWORD[v]; + IF(!v) + { + test(1,1); + pushStack(0); + return; + } + v(arg); + pushStack(EAX); +} + +/* + +:void call_function(dword arg) +{ + dword x = 0; + + dword c = arg; + dword o = 0; + + dword v = 0; + + WHILE(c) + { + o+=4; + popStack(); + $push eax + c--; + } + $push arg + + v = popStack(); + + IF(DSBYTE[v+4]!=PY_FNC) test("No function!",0); + + v = DSDWORD[v]; + IF(!v) + { + test(1,1); + pushStack(0); + return; + } + + $call v + ESP+=o; + + pushStack(EAX); +} +*/ \ No newline at end of file