📄 gb_code_temp.h
字号:
/*************************************************************************** code_t.h P-code assembler (c) 2000-2004 Beno� Minisini <gambas@users.sourceforge.net> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.***************************************************************************//*#define DEBUG*/#define write_Zxxx(code, val) write_short(code | ((short)val & 0x0FFF))#define write_Z8xx(code, val) write_short(code | ((short)val & 0x07FF))#define write_ZZxx(code, val) write_short(code | ((short)val & 0x00FF))#define LAST_CODE start_code()PUBLIC short CODE_stack_usage;PUBLIC short CODE_stack;#ifdef PROJECT_EXEC#define cur_func EVAL#elsePRIVATE FUNCTION *cur_func = NULL;PRIVATE int last_line = 0;#endifPRIVATE void start_code(void){ #ifndef PROJECT_EXEC if (JOB->debug && !JOB->nobreak) CODE_break(); #endif cur_func->last_code = ARRAY_count(cur_func->code);}PRIVATE void write_short(short value){ short *ptr; ptr = ARRAY_add(&cur_func->code); *ptr = value;}static void write_long(long value){ ushort *ptr; ptr = ARRAY_add(&cur_func->code); *ptr = value & 0xFFFF; ptr = ARRAY_add(&cur_func->code); *ptr = (unsigned long)value >> 16;}PRIVATE void remove_last(void){ ARRAY_remove_last(&cur_func->code); cur_func->last_code = ARRAY_count(cur_func->code);}PRIVATE void use_stack(int use){ CODE_stack += use; CODE_stack_usage = Max(CODE_stack_usage, CODE_stack); #ifdef DEBUG printf("%04ld: %d\n", ARRAY_count(cur_func->code), CODE_stack); #endif}PUBLIC long CODE_get_current_pos(){ return ARRAY_count(cur_func->code);}#ifdef PROJECT_EXECPUBLIC void CODE_begin_function(){ CODE_stack = 0; CODE_stack_usage = 0;}PUBLIC void CODE_end_function(){ if (CODE_stack) ERROR_panic("Bad stack usage computed !");}#elsePUBLIC void CODE_begin_function(FUNCTION *func){ cur_func = func; CODE_stack = 0; CODE_stack_usage = 0; if (func->start == NULL) last_line = (-1); else last_line = 0;}PUBLIC void CODE_end_function(FUNCTION *func){ if (CODE_stack) ERROR_panic("Bad stack usage computed !");}#endifPRIVATE ushort *get_last_code(){ if (cur_func->last_code < 0) return NULL; return &cur_func->code[cur_func->last_code];}#ifdef PROJECT_COMPPUBLIC boolean CODE_popify_last(void){ /* #ifdef DEBUG printf("CODE_is_last_popable ? "); if (!last_code) printf("FALSE, last_code = NULL"); else printf("0x%04hX", *last_code); printf("\n"); #endif */ unsigned short *last_code, op; last_code = get_last_code(); if (!last_code) return FALSE; op = *last_code & 0xFF00; if ((op >= C_PUSH_LOCAL) && (op <= C_PUSH_UNKNOWN)) { *last_code += 0x0800; use_stack(-2); #ifdef DEBUG printf("Popify Last\n"); #endif return TRUE; } if ((op & 0xF000) == C_PUSH_DYNAMIC) { *last_code += 0x1000; use_stack(-2); #ifdef DEBUG printf("Popify Last\n"); #endif return TRUE; } /* if (op == C_CALL) { *last_code = C_CALL_POP | (*last_code & 0xFF); return TRUE; } */ return FALSE;}PUBLIC boolean CODE_check_statement_last(void){ unsigned short op; PCODE *last_code; last_code = get_last_code(); if (!last_code) return FALSE; op = *last_code & 0xFF00; if (op == C_CALL) return TRUE; if (op == C_PUSH_RETURN) return TRUE; op >>= 8; if (op >= CODE_FIRST_SUBR && op <= CODE_LAST_SUBR) return TRUE; return FALSE;}PUBLIC boolean CODE_check_pop_local_last(short *local){ PCODE *last_code; short l; last_code = get_last_code(); if (!last_code) return FALSE; if ((*last_code & 0xFF00) == C_POP_LOCAL) { l = (char)(*last_code & 0xFF); if (l < 0) return FALSE; *local = l; return TRUE; } return FALSE;}#endifPUBLIC void CODE_push_number(long value){ LAST_CODE; use_stack(1); if (value >= -2048L && value < 2048L) { #ifdef DEBUG printf("PUSH QUICK %ld\n", value); #endif write_Zxxx(C_PUSH_QUICK, value); } else if (value >= -32768L && value < 32768L) { #ifdef DEBUG printf("PUSH INTEGER %ld\n", value); #endif write_short(C_PUSH_INTEGER); write_short((short)value); } else { #ifdef DEBUG printf("PUSH LONG %ld\n", value); #endif write_short(C_PUSH_LONG); write_long(value); }}PUBLIC void CODE_push_const(short value){ LAST_CODE; use_stack(1); #ifdef DEBUG printf("PUSH CONST %d %s\n", value, TABLE_get_symbol_name(JOB->class->table, JOB->class->constant[value].index)); #endif write_Zxxx(C_PUSH_CONST, value);}PUBLIC void CODE_push_local(short num){ LAST_CODE; use_stack(1); #ifdef DEBUG if (num >= 0) printf("PUSH LOCAL %d\n", num); else printf("PUSH PARAM %d\n", (-1) - num); #endif write_ZZxx(C_PUSH_LOCAL, num);}#ifdef PROJECT_COMPPUBLIC void CODE_pop_local(short num){ LAST_CODE; use_stack(-1); #ifdef DEBUG if (num >= 0) printf("POP LOCAL #%d\n", num); else printf("POP PARAM #%d\n", (-1) - num); #endif write_ZZxx(C_POP_LOCAL, num);}PUBLIC void CODE_pop_ctrl(short num){ LAST_CODE; use_stack(-1); #ifdef DEBUG printf("POP CTRL #%d\n", num); #endif write_ZZxx(C_POP_CTRL, num);}PUBLIC void CODE_pop_optional(short num){ LAST_CODE; use_stack(-1); #ifdef DEBUG printf("POP OPTIONAL #%d\n", (-1) - num); #endif write_ZZxx(C_POP_OPTIONAL, num);}#endif /* PROJECT_COMP */PUBLIC void CODE_push_array(short nparam){ LAST_CODE; use_stack(1 - nparam); write_ZZxx(C_PUSH_ARRAY, nparam);}PUBLIC void CODE_push_global(short global, boolean is_static, boolean is_function){ LAST_CODE; use_stack(1); #ifdef DEBUG printf("PUSH %s %d\n", is_static ? "STATIC" : "DYNAMIC", global); #endif if (is_function) write_Z8xx(C_PUSH_FUNCTION, global); else if (is_static) write_Z8xx(C_PUSH_STATIC, global); else write_Z8xx(C_PUSH_DYNAMIC, global);}#ifdef PROJECT_COMPPUBLIC void CODE_pop_global(short global, boolean is_static){ LAST_CODE; use_stack(-1); #ifdef DEBUG printf("POP %s %d\n", is_static ? "STATIC" : "DYNAMIC", global); #endif if (is_static) write_Z8xx(C_POP_STATIC, global); else write_Z8xx(C_POP_DYNAMIC, global);}#endif/*PUBLIC void CODE_push_symbol(short symbol){ LAST_CODE; use_stack(0); #ifdef DEBUG printf("PUSH SYMBOL %s\n", TABLE_get_symbol_name(JOB->class->table, symbol)); #endif write_short(C_PUSH_SYMBOL); write_short(symbol);}PUBLIC void CODE_pop_symbol(short symbol){ LAST_CODE; use_stack(-2); #ifdef DEBUG printf("POP SYMBOL %s\n", TABLE_get_symbol_name(JOB->class->table, symbol)); #endif write_short(C_POP_SYMBOL); write_short(symbol);}*/PUBLIC void CODE_push_unknown(short symbol){ LAST_CODE; use_stack(0); #ifdef DEBUG printf("PUSH UNKNOWN %s\n", TABLE_get_symbol_name(JOB->class->table, symbol)); #endif write_short(C_PUSH_UNKNOWN); write_short(symbol);}#ifdef PROJECT_COMPPUBLIC void CODE_pop_unknown(short symbol){ LAST_CODE; use_stack(-2); #ifdef DEBUG printf("POP UNKNOWN %s\n", TABLE_get_symbol_name(JOB->class->table, symbol)); #endif write_short(C_POP_UNKNOWN); write_short(symbol);}#endifPUBLIC void CODE_push_class(short class){ LAST_CODE; use_stack(1); #ifdef DEBUG printf("PUSH CLASS %d\n", class); #endif write_Z8xx(C_PUSH_CLASS, class);}#ifdef PROJECT_COMPPUBLIC void CODE_jump(){ LAST_CODE; #ifdef DEBUG printf("JUMP\n"); #endif write_short(C_JUMP); /**pos = CODE_get_current_pos();*/ write_short(0);}PUBLIC void CODE_jump_if_true(){ /* ushort *last_code = get_last_code(); ushort op; if (last_code && PCODE_is(*last_code, C_NOT)) { remove_last(); op = C_JUMP_IF_FALSE; } else op = C_JUMP_IF_TRUE; */ use_stack(-1); #ifdef DEBUG printf("JUMP IF TRUE\n"); #endif LAST_CODE; write_short(C_JUMP_IF_TRUE); /**pos = CODE_get_current_pos();*/ write_short(0);}PUBLIC void CODE_jump_if_false(){ /* ushort *last_code = get_last_code(); ushort op; if (last_code && PCODE_is(*last_code, C_NOT)) { remove_last(); op = C_JUMP_IF_TRUE; } else op = C_JUMP_IF_FALSE; */ use_stack(-1); #ifdef DEBUG printf("JUMP IF FALSE\n"); #endif LAST_CODE; write_short(C_JUMP_IF_FALSE); /**pos = CODE_get_current_pos();*/ write_short(0);}PUBLIC void CODE_jump_first(short local){ LAST_CODE; use_stack(-2); #ifdef DEBUG printf("JUMP FIRST LOCAL %d\n", local); #endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -