From 4f79e17394c1932cda36b5527659d0f8903cf043 Mon Sep 17 00:00:00 2001 From: pavelyakov Date: Sat, 14 Jul 2018 14:51:53 +0000 Subject: [PATCH] Python update online compiler git-svn-id: svn://kolibrios.org@7304 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/python/examples/dectohex.py | 10 + programs/cmm/python/library/function.h | 83 +++++ programs/cmm/python/library/head.h | 69 ++++ programs/cmm/python/library/init.h | 226 ++++++++++++ programs/cmm/python/library/opcode.h | 433 +++++++++++++++++++++++ 5 files changed, 821 insertions(+) create mode 100644 programs/cmm/python/examples/dectohex.py create mode 100644 programs/cmm/python/library/function.h create mode 100644 programs/cmm/python/library/head.h create mode 100644 programs/cmm/python/library/init.h create mode 100644 programs/cmm/python/library/opcode.h diff --git a/programs/cmm/python/examples/dectohex.py b/programs/cmm/python/examples/dectohex.py new file mode 100644 index 0000000000..ded173956f --- /dev/null +++ b/programs/cmm/python/examples/dectohex.py @@ -0,0 +1,10 @@ +def test(x): + f = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'] + txt = "" + while x: + txt = f[x&0xF]+txt + x = x>>4 + + return txt + +stdout(test(0x123454)) \ No newline at end of file diff --git a/programs/cmm/python/library/function.h b/programs/cmm/python/library/function.h new file mode 100644 index 0000000000..22ced1b128 --- /dev/null +++ b/programs/cmm/python/library/function.h @@ -0,0 +1,83 @@ +:void copyVariable(dword x) +{ + load_const(DSDWORD[x],DSBYTE[x+4],DSDWORD[x+5]); +} + +: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__len(dword c) +{ + popFast(); + load_const(DSDWORD[EAX+5],PY_INT,0); + RETURN 0; +} + +:void hexAddress(dword x,y) +{ + dword i = 0; + i = 8; + WHILE(i) + { + DSBYTE[x] = DSBYTE[y&0xF+#arraySymbolHEX]; + y>>=4; + x--; + i--; + } +} +:dword stdcall std__str(dword c) +{ + dword n = 0; + dword m = 0; + dword l = 0; + dword v = 0; + dword f = ""; + dword t = 0; + dword o = 0; + + v = popFast(); + + IF(DSBYTE[v+4] == PY_STR) + { + copyVariable(v); + return 0; + } + + switch(DSBYTE[v+4]) + { + case PY_BOOL: + if(DSDWORD[v]) + { + load_const("True",PY_STR,4); + return 0; + } + load_const("False",PY_STR,5); + return 0; + break; + case PY_INT: + m = malloc(MEMBUF); + l = itoa(DSDWORD[v],m); + load_const(m,PY_NA_STR,l); + return 0; + break; + case PY_NONE: + load_const("None",PY_STR,4); + return 0; + break; + case PY_FNC: + hexAddress(f+24,v); + load_const(f,PY_STR,26); + return 0; + break; + } + load_const("None",PY_STR,4); + return 0; +} \ No newline at end of file diff --git a/programs/cmm/python/library/head.h b/programs/cmm/python/library/head.h new file mode 100644 index 0000000000..762a5a2c05 --- /dev/null +++ b/programs/cmm/python/library/head.h @@ -0,0 +1,69 @@ +#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_NA_STR 32 + +#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 +#define MEMARR 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 arraySymbolHEX = "0123456789ABCDEF"; +:dword HASH = 0; +:dword TEMP = 0; + +:struct f70{ + dword func; + dword param1; + dword param2; + dword param3; + dword param4; + char rezerv; + dword name; +}; diff --git a/programs/cmm/python/library/init.h b/programs/cmm/python/library/init.h new file mode 100644 index 0000000000..d03557bf8f --- /dev/null +++ b/programs/cmm/python/library/init.h @@ -0,0 +1,226 @@ +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() +{ + IF(!stack) RETURN 0; + stack-=4; + RETURN DSDWORD[stack]; +} +:void pushStack(dword x) +{ + DSDWORD[stack] = x; + stack+=4; +} + +:dword stackFast = 0; +:dword beginStackFast = 0; +:dword popFast() +{ + IF(!stackFast) RETURN 0; + stackFast-=4; + RETURN DSDWORD[stackFast]; +} +:void pushFast(dword x) +{ + DSDWORD[stackFast] = x; + stackFast+=4; +} + +:dword stackLoop = 0; +:dword popLoop() +{ + stackLoop-=4; + RETURN DSDWORD[stackLoop]; +} +:void pushLoop(dword x) +{ + DSDWORD[stackLoop] = x; + stackLoop+=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); + beginStackFast = stackFast; + stackLoop = malloc(1000); + TEMP = malloc(100); + + main(); + ExitProcess(); +} + +void ______STOP______() +{ + ExitProcess(); +} \ No newline at end of file diff --git a/programs/cmm/python/library/opcode.h b/programs/cmm/python/library/opcode.h new file mode 100644 index 0000000000..907d5a6bd0 --- /dev/null +++ b/programs/cmm/python/library/opcode.h @@ -0,0 +1,433 @@ +#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() +{ + stackLoop-=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 v) +{ + dword x = 0; + x = DSDWORD[v]; + IF(x) + { + IF(DSDWORD[x+9]) free(DSDWORD[x]); + free(x); + DSDWORD[v] = 0; + 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(x); + DSDWORD[x] = y; + RETURN y; +} + +:void load_name(dword addr) +{ + pushStack(addr); +} +:void pop_top() +{ + stack-=4; +} +:void load_fast(dword addr) +{ + dword x = 0; + IF(!DSDWORD[addr]) + { + x = popFast(); + DSDWORD[addr] = x; + } + ELSE x = DSDWORD[addr]; + pushStack(x); +} +: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); + IF(EAX) pushStack(EAX); + stackFast = beginStackFast; +} + +:void build_list(dword count) +{ + dword n = 0; + dword mem = 0; + dword i = 0; + dword sizeBuffer = 0; + + mem = malloc(count*4+MEMBUF); + i = count; + n = i*4+mem; + WHILE(i) + { + n-=4; + DSDWORD[n] = popStack(); + i--; + } + load_const(mem,PY_LST,count); +} +:dword store_fast() +{ + return popStack(); +} +:void binary_subscr() +{ + dword x = 0; + dword y = 0; + dword l = 0; + x = popStack(); + y = popStack(); + + //IF(DSBYTE[x+4]!=PY_INT) error type; + + x = DSDWORD[x]; + + l = DSDWORD[y+5]; + + IF(l<=x) test("error max list",0); + IF(0>l) test("error min list",0); + + y = DSDWORD[y]; + pushStack(DSDWORD[x*4+y]); +} + +/* + +: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