Reformat ParseStatement.CC

This commit is contained in:
TomAwezome 2020-07-26 17:46:51 -05:00 committed by VoidNV
parent 1b380ef95d
commit bb497eca7c
3 changed files with 932 additions and 789 deletions

View file

@ -3,7 +3,8 @@ CHashClass *ParseClass(CCompCtrl *cc,I64 keyword,I64 fsp_flags,Bool is_extern)
CHashClass *tmpc, *base_class;
if (cc->token != TK_IDENT)
LexExcept(cc, "Expecting identifier at ");
if (is_extern) {
if (is_extern)
{
tmpc = ParseClassNew;
tmpc->str = cc->cur_str;
cc->cur_str = NULL;
@ -11,23 +12,29 @@ CHashClass *ParseClass(CCompCtrl *cc,I64 keyword,I64 fsp_flags,Bool is_extern)
LBts(&tmpc->flags, Cf_EXTERN);
HashSrcFileSet(cc, tmpc);
Lex(cc);
} else {
}
else
{
if (cc->flags & CCF_AOT_COMPILE)
tmpc = HashFind(cc->cur_str, cc->htc.global_hash_table, HTT_CLASS);
else
tmpc = HashSingleTableFind(cc->cur_str, cc->htc.global_hash_table, HTT_CLASS);
if (tmpc) {
if (tmpc)
{
if (!Bt(&tmpc->flags, Cf_EXTERN))
tmpc = NULL;
else if (tmpc->use_count < 3)
UnusedExternWarning(cc, tmpc);
}
if (tmpc) {
if (tmpc)
{
Free(tmpc->src_link);
tmpc->src_link = NULL;
Free(tmpc->idx);
tmpc->idx = NULL;
} else {
}
else
{
tmpc = ParseClassNew;
tmpc->str = cc->cur_str;
cc->cur_str = NULL;
@ -41,9 +48,9 @@ CHashClass *ParseClass(CCompCtrl *cc,I64 keyword,I64 fsp_flags,Bool is_extern)
HashSrcFileSet(cc, tmpc, -1);
else
HashSrcFileSet(cc, tmpc,0);
if (Lex(cc)==':') {
if (Lex(cc)!=TK_IDENT || !(base_class=cc->hash_entry) ||
!(base_class->type&HTT_CLASS))
if (Lex(cc) == ':')
{
if (Lex(cc) != TK_IDENT || !(base_class = cc->hash_entry) || !(base_class->type & HTT_CLASS))
LexExcept(cc, "Invalid class at ");
if (Lex(cc) == ',')
LexExcept(cc, "Only one base class allowed at this time at ");
@ -59,28 +66,30 @@ CHashClass *ParseClass(CCompCtrl *cc,I64 keyword,I64 fsp_flags,Bool is_extern)
return tmpc;
}
CHashFun *ParseFunJoin(CCompCtrl *cc,CHashClass *tmp_return,
U8 *name,I64 fsp_flags)
CHashFun *ParseFunJoin(CCompCtrl *cc, CHashClass *tmp_return, U8 *name, I64 fsp_flags)
{
CMemberList *tmpm, *header_list;
CAOTCtrl *aotc = cc->aotc;
CHashClass *header_return;
CHashFun *tmpf;
I64 header_arg_count;
if (name) {//if not fun_ptr
if (cc->flags&CCF_AOT_COMPILE) {
if ((tmpf=HashFind(name,cc->htc.global_hash_table,HTT_FUN)) &&
tmpf->type & HTF_IMPORT)
if (name)
{//if not fun_ptr
if (cc->flags & CCF_AOT_COMPILE)
{
if ((tmpf = HashFind(name, cc->htc.global_hash_table, HTT_FUN)) && tmpf->type & HTF_IMPORT)
tmpf=NULL;
} else
if ((tmpf=HashSingleTableFind(name,cc->htc.global_hash_table,HTT_FUN)) &&
!Bt(&tmpf->flags,Cf_EXTERN))
}
else
if ((tmpf = HashSingleTableFind(name, cc->htc.global_hash_table, HTT_FUN)) && !Bt(&tmpf->flags, Cf_EXTERN))
tmpf = NULL;
if (tmpf && tmpf->use_count < 3)
UnusedExternWarning(cc, tmpf);
} else
}
else
tmpf = NULL;
if (tmpf) {
if (tmpf)
{
tmpf->used_reg_mask = REGG_CLOBBERED + REGG_SAVED + REGG_STACK_TMP;
Free(tmpf->src_link);
tmpf->src_link = NULL;
@ -92,7 +101,9 @@ CHashFun *ParseFunJoin(CCompCtrl *cc,CHashClass *tmp_return,
header_return = tmpf->return_class;
tmpf->member_list_and_root = NULL;
ClassMemberListDel(tmpf);
} else {
}
else
{
tmpf = ParseFunNew;
header_return = NULL;
tmpf->used_reg_mask = REGG_CLOBBERED + REGG_SAVED + REGG_STACK_TMP;
@ -116,18 +127,23 @@ CHashFun *ParseFunJoin(CCompCtrl *cc,CHashClass *tmp_return,
if (0 < tmpf->arg_count << 3 <= I16_MAX && !Bt(&tmpf->flags, Ff_DOT_DOT_DOT))
LBts(&tmpf->flags, Ff_RET1);
tmpm = tmpf->member_list_and_root;
while (tmpm) {
while (tmpm)
{
tmpm->offset += 16; //RBP+RETURN
tmpm = tmpm->next;
}
tmpf->size = 0;
if (header_return) {
if (OptionGet(OPTf_WARN_HEADER_MISMATCH)) {
if (tmpf->return_class!=header_return) {
if (header_return)
{
if (OptionGet(OPTf_WARN_HEADER_MISMATCH))
{
if (tmpf->return_class != header_return)
{
PrintWarn("Fun Header return mismatch '%s'\n", tmpf->str);
cc->warning_count++;
}
if (!MemberListCmp(tmpf->member_list_and_root,header_list,header_arg_count)) {
if (!MemberListCmp(tmpf->member_list_and_root, header_list, header_arg_count))
{
PrintWarn("Fun header args mismatch '%s'\n", tmpf->str);
cc->warning_count++;
}
@ -168,21 +184,25 @@ U0 ParseFun(CCompCtrl *cc,CHashClass *tmp_return,U8 *name,I64 fsp_flags)
cc->lb_leave = saved_leave_label;
ICAdd(cc, IC_LEAVE, 0, cc->htc.fun->return_class);
cc->htc.fun->size &= ~7;
if (cc->flags&CCF_AOT_COMPILE) {
if (cc->flags & CCF_AOT_COMPILE)
{
cc->htc.fun->exe_addr = cc->aotc->rip;
cc->htc.fun->type |= HTF_EXPORT | HTF_RESOLVE;
r = COCCompile(cc, &size, &cc->htc.fun->debug_info, NULL);
if (r) {
if (r)
{
j = (size + 7) >> 3;
for (i = 0; i < j; i++)
AOTStoreCodeU64(cc, r[i]);
Free(r);
}
} else {
}
else
{
old_trace = Btr(&cc->opts, OPTf_TRACE);
cc->htc.fun->exe_addr=COCCompile(
cc,&size,&cc->htc.fun->debug_info,NULL);
if (old_trace) {
cc->htc.fun->exe_addr = COCCompile(cc, &size, &cc->htc.fun->debug_info, NULL);
if (old_trace)
{
Bts(&cc->opts, OPTf_TRACE);
Un(cc->htc.fun->exe_addr, size, 64);
}
@ -191,23 +211,23 @@ U0 ParseFun(CCompCtrl *cc,CHashClass *tmp_return,U8 *name,I64 fsp_flags)
LBtr(&cc->htc.fun->flags, Cf_EXTERN);
COCPop(cc);
tmpm = cc->htc.fun->member_list_and_root;
while (tmpm) {
if (tmpm->flags & MLF_NO_UNUSED_WARN) {
while (tmpm)
{
if (tmpm->flags & MLF_NO_UNUSED_WARN)
{
if (tmpm->use_count > 1 && StrCompare(tmpm->str, "_anon_"))
PrintWarn("Unneeded no_warn\n $$LK,\"FL:%s,%d\"$$ '%s' in '%s'\n",
cc->lex_include_stack->full_name,cc->lex_include_stack->line_num,
tmpm->str,cc->htc.fun->str);
} else if (!tmpm->use_count && OptionGet(OPTf_WARN_UNUSED_VAR))
cc->lex_include_stack->full_name, cc->lex_include_stack->line_num, tmpm->str, cc->htc.fun->str);
}
else if (!tmpm->use_count && OptionGet(OPTf_WARN_UNUSED_VAR))
PrintWarn("Unused var\n $$LK,\"FL:%s,%d\"$$ '%s' in '%s'\n",
cc->lex_include_stack->full_name,cc->lex_include_stack->line_num,
tmpm->str,cc->htc.fun->str);
cc->lex_include_stack->full_name, cc->lex_include_stack->line_num, tmpm->str, cc->htc.fun->str);
tmpm = tmpm->next;
}
cc->htc.local_var_list = cc->htc.fun=cc->fun_lex_file = NULL;
}
U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
I64 saved_val,I64 fsp_flags)
U0 ParseGlobalVarList(CCompCtrl *cc, I64 saved_mode, CHashClass *saved_tmpc, I64 saved_val, I64 fsp_flags)
{
I64 i, j, mode, k, val;
U8 *st;
@ -219,28 +239,33 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
CHashFun *tmpf, *tmpf_fun_ptr;
CArrayDim tmpad;
Bool has_alias, undef_array_size, is_array;
while (TRUE) {
tmpc=ParseType(cc,&saved_tmpc,&saved_mode,NULL,&st,
&tmpf_fun_ptr,&tmpex,&tmpad,fsp_flags);
while (TRUE)
{
tmpc = ParseType(cc, &saved_tmpc, &saved_mode, NULL, &st, &tmpf_fun_ptr, &tmpex, &tmpad, fsp_flags);
if (!st) return;
if (!st)
return;
if (tmpad.next)
is_array = TRUE;
else if (tmpad.total_count<0) {
else if (tmpad.total_count < 0)
{
is_array = TRUE;
tmpc--;
} else
}
else
is_array = FALSE;
val = saved_val;
mode = saved_mode;
if (tmpex && mode&255==PRS0_EXTERN && !(cc->flags&CCF_AOT_COMPILE) &&
tmpex->type&HTT_EXPORT_SYS_SYM) {
if (tmpex && mode & 255 == PRS0_EXTERN && !(cc->flags & CCF_AOT_COMPILE) && tmpex->type & HTT_EXPORT_SYS_SYM)
{
val = tmpex->val;
mode = PRS0__EXTERN | PRS1_NOT_REALLY__EXTERN;
}
if (cc->token=='(') {
switch (mode&255) {
if (cc->token == '(')
{
switch (mode & 255)
{
case PRS0__INTERN:
tmpf = ParseFunJoin(cc, tmpc, st, fsp_flags);
tmpf->exe_addr = val;
@ -268,7 +293,8 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
case PRS0_IMPORT:
if (!(cc->flags & CCF_AOT_COMPILE))
LexExcept(cc, "import not needed at ");
else {
else
{
tmpf = ParseFunJoin(cc, tmpc, st, fsp_flags);
tmpf->type |= HTF_IMPORT;
if (mode & 255 == PRS0__IMPORT)
@ -281,11 +307,16 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
ParseFun(cc, tmpc, st, fsp_flags);
return;
}
} else {
if (tmpad.total_count<0) {
}
else
{
if (tmpad.total_count < 0)
{
i = 0;
undef_array_size = TRUE;
} else {
}
else
{
i = tmpad.total_count;
undef_array_size = FALSE;
}
@ -296,13 +327,17 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
j *= i;
has_alias = FALSE;
tmphg = NULL;
switch (mode&255) {
switch (mode & 255)
{
case PRS0__EXTERN:
if (cc->flags&CCF_AOT_COMPILE) {
if (cc->flags & CCF_AOT_COMPILE)
{
tmpg = CAlloc(sizeof(CHashGlobalVar));
tmpg->data_addr_rip = val;
tmpg->type = HTT_GLOBAL_VAR | HTF_EXPORT;
} else {
}
else
{
tmpg = CAlloc(sizeof(CHashGlobalVar), Fs->code_heap);
tmpg->data_addr = val;
tmpg->type = HTT_GLOBAL_VAR;
@ -313,7 +348,8 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
case PRS0_IMPORT:
if (!(cc->flags & CCF_AOT_COMPILE))
LexExcept(cc, "import not needed at ");
else {
else
{
tmpg = CAlloc(sizeof(CHashGlobalVar));
tmpg->type = HTT_GLOBAL_VAR | HTF_IMPORT;
if (mode & 255 == PRS0__IMPORT)
@ -323,16 +359,20 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
}
break;
case PRS0_EXTERN:
if (cc->flags&CCF_AOT_COMPILE) {
if (cc->flags & CCF_AOT_COMPILE)
{
tmpg = CAlloc(sizeof(CHashGlobalVar));
tmpg->type = HTT_GLOBAL_VAR;
} else {
}
else
{
tmpg = CAlloc(sizeof(CHashGlobalVar), Fs->code_heap);
tmpg->type = HTT_GLOBAL_VAR | HTF_UNRESOLVED;
}
break;
default:
if (cc->flags&CCF_AOT_COMPILE) {
if (cc->flags & CCF_AOT_COMPILE)
{
if (Bt(&cc->opts, OPTf_GLOBALS_ON_DATA_HEAP)) {
if (cc->token == '=')
LexExcept(cc, "Can't init global var on data heap in AOT module ");
@ -346,7 +386,9 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
tmpg->type = HTT_GLOBAL_VAR; //TODO: HTF_EXPORT
if (tmpex && tmpex->type & HTT_GLOBAL_VAR) //TODO!! extern
LexExcept(cc, "Feature not implemented ");
} else {
}
else
{
tmpg = CAlloc(sizeof(CHashGlobalVar));
if (cc->token == '=')
tmpg->data_addr = CAlloc(j);
@ -366,18 +408,22 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
for (k = 0; k < j; k++)
AOTStoreCodeU8(cc, 0); //Init AOT global to zero.
}
} else {
if (Bt(&cc->opts,OPTf_GLOBALS_ON_DATA_HEAP)) {
}
else
{
if (Bt(&cc->opts, OPTf_GLOBALS_ON_DATA_HEAP))
{
tmpg = CAlloc(sizeof(CHashGlobalVar), Fs->code_heap);
tmpg->data_addr = MAlloc(j);
tmpg->flags = GVF_DATA_HEAP;
} else {
}
else
{
tmpg = CAlloc(sizeof(CHashGlobalVar), Fs->code_heap);
tmpg->data_addr = MAlloc(j, Fs->code_heap);
}
tmpg->type = HTT_GLOBAL_VAR;
if (tmpex && tmpex->type&HTT_GLOBAL_VAR &&
tmpex->type&HTF_UNRESOLVED &&
if (tmpex && tmpex->type & HTT_GLOBAL_VAR && tmpex->type & HTF_UNRESOLVED &&
MHeapCtrl(tmpex) == MHeapCtrl(tmpg))
has_alias = TRUE;
if (sys_var_init_flag)
@ -400,7 +446,8 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
tmpg->flags |= GVF_IMPORT;
if (mode & 255 == PRS0_EXTERN)
tmpg->flags |= GVF_EXTERN;
if (tmpf_fun_ptr) {
if (tmpf_fun_ptr)
{
tmpg->fun_ptr = tmpf_fun_ptr;
tmpg->flags |= GVF_FUN;
}
@ -409,8 +456,10 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
HashAdd(tmpg, cc->htc.global_hash_table);
if (!(cc->flags & CCF_AOT_COMPILE) && !(tmpg->flags & GVF_EXTERN))
SysSymImportsResolve(tmpg->str);
if (cc->token=='=') {
if (undef_array_size) {
if (cc->token == '=')
{
if (undef_array_size)
{
LexPush(cc);
LexPush(cc);
Lex(cc);
@ -435,8 +484,10 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
AOTStoreCodeU8At(cc, tmpg->data_addr_rip + k, tmpg->data_addr[k]);
LexPopNoRestore(cc);
}
if (has_alias) {
if (tmpex(CHashGlobalVar *)->use_count<2) {
if (has_alias)
{
if (tmpex(CHashGlobalVar *)->use_count < 2)
{
PrintWarn("Unused extern '%s'\n", tmpex(CHashGlobalVar *)->str);
cc->warning_count++;
}
@ -446,7 +497,8 @@ U0 ParseGlobalVarList(CCompCtrl *cc,I64 saved_mode,CHashClass *saved_tmpc,
}
if (cc->token == ',')
Lex(cc);
else {
else
{
if (cc->token != ';')
LexExcept(cc, "Missing ';' at");
Lex(cc);
@ -472,14 +524,16 @@ U0 ParseIf(CCompCtrl *cc,I64 try_count,CCodeMisc *lb_break)
ICAdd(cc, IC_BR_ZERO, lb, 0);
ParseStatement(cc, try_count, lb_break);
k = ParseKeyWord(cc);
if (k==KW_ELSE) {
if (k == KW_ELSE)
{
Lex(cc);
lb1 = COCMiscNew(cc, CMT_LABEL);
ICAdd(cc, IC_JMP, lb1, 0);
ICAdd(cc, IC_LABEL, lb, 0);
ParseStatement(cc, try_count, lb_break);
ICAdd(cc, IC_LABEL, lb1, 0);
} else
}
else
ICAdd(cc, IC_LABEL, lb, 0);
}
@ -563,12 +617,14 @@ U0 ParseFor(CCompCtrl *cc,I64 try_count)
ICAdd(cc, IC_LABEL, lb_done, 0);
}
class CSubSwitch {
class CSubSwitch
{
CSubSwitch *next, *last;
CCodeMisc *lb_start, *lb_break;
};
class CSwitchCase {
class CSwitchCase
{
CSwitchCase *next;
CCodeMisc *label;
I64 val;
@ -605,11 +661,14 @@ U0 ParseSwitch(CCompCtrl *cc,I64 try_count)
tmpi_sub = ICAdd(cc, IC_IMM_I64, 0, cmp.internal_types[RT_I64]);
ICAdd(cc, IC_SUB, 0, cmp.internal_types[RT_I64]);
tmpi_cmp = ICAdd(cc, IC_IMM_I64, 0, cmp.internal_types[RT_I64]);
if (nobound) {
if (nobound)
{
ICAdd(cc, IC_NOBOUND_SWITCH, mc_jt, 0);
if (cc->token != ']')
LexExcept(cc, "Missing ']' at ");
} else {
}
else
{
ICAdd(cc, IC_SWITCH, mc_jt, 0);
if (cc->token != ')')
LexExcept(cc, "Missing ')' at ");
@ -618,10 +677,13 @@ U0 ParseSwitch(CCompCtrl *cc,I64 try_count)
LexExcept(cc, "Expecting '{' at ");
Lex(cc);
ICAdd(cc, IC_LABEL, mc_jt->begin, 0);
while (TRUE) {
while (cc->token && cc->token!='}') {
while (TRUE)
{
while (cc->token && cc->token != '}')
{
sw_cont:
switch (ParseKeyWord(cc)) {
switch (ParseKeyWord(cc))
{
case KW_END:
goto sw_sub_end;
case KW_START:
@ -638,18 +700,23 @@ sw_cont:
tmpss->lb_start = COCMiscNew(cc, CMT_LABEL);
tmpi_start = ICAdd(cc, IC_LABEL, tmpss->lb_start, 0);
while (cc->token && cc->token!='}') {
switch (ParseKeyWord(cc)) {
while (cc->token && cc->token != '}')
{
switch (ParseKeyWord(cc))
{
case KW_END:
OptFree(tmpi_jmp);
goto sw_sub_end;
case KW_START:
case KW_CASE:
case KW_DEFAULT:
if (cc->coc.coc_head.last==tmpi_start) {
if (cc->coc.coc_head.last == tmpi_start)
{
OptFree(tmpi_jmp);
tmpss->lb_start = NULL;
} else {
}
else
{
ICAdd(cc, IC_RET, 0, 0);
ICAdd(cc, IC_LABEL, lb_fwd_case, 0);
ICAdd(cc, IC_SUB_CALL, tmpss->lb_start, 0);//In case fall-thru
@ -661,7 +728,8 @@ sw_cont:
}
break;
case KW_CASE:
if (head.next!=&head) {
if (head.next != &head)
{
lb_fwd_case = COCMiscNew(cc, CMT_LABEL);
tmpi_jmp = ICAdd(cc, IC_JMP, lb_fwd_case, 0);//In case fall-thru
}
@ -669,41 +737,54 @@ sw_cont:
lb_entry = COCMiscNew(cc, CMT_LABEL);
ICAdd(cc, IC_LABEL, lb_entry, 0);
lb_entry->use_count++;
if (head.next!=&head) {
if (head.next != &head)
{
tmpss = head.next;
while (tmpss!=&head) {
while (tmpss != &head)
{
if (tmpss->lb_start)
ICAdd(cc, IC_SUB_CALL, tmpss->lb_start, 0);
tmpss = tmpss->next;
}
ICAdd(cc, IC_LABEL, lb_fwd_case, 0);
}
if (cc->token==':') {
if (cc->token == ':')
{
if (k_start == I64_MIN)
k_start = 0;
else
k_start++;
} else
}
else
k_start = LexExpressionI64(cc);
if (k_start<lo) lo=k_start;
if (k_start>hi) hi=k_start;
if (cc->token==':') {
if (k_start < lo)
lo = k_start;
if (k_start > hi)
hi = k_start;
if (cc->token == ':')
{
Lex(cc);
tmps = MAlloc(sizeof(CSwitchCase));
tmps->label = lb_entry;
tmps->val = k_start;
tmps->next = header;
header = tmps;
} else if (cc->token==TK_ELLIPSIS) {
}
else if (cc->token == TK_ELLIPSIS)
{
Lex(cc);
k_end = LexExpressionI64(cc);
if (cc->token==':') {
if (cc->token == ':')
{
Lex(cc);
if (k_end<lo) lo=k_end;
if (k_end>hi) hi=k_end;
if (k_end < lo)
lo = k_end;
if (k_end > hi)
hi = k_end;
if (k_start > k_end)
SwapI64(&k_start, &k_end);
for (i=k_start;i<=k_end;i++) {
for (i = k_start; i <= k_end; i++)
{
tmps = MAlloc(sizeof(CSwitchCase));
tmps->label = lb_entry;
tmps->val = i;
@ -711,13 +792,16 @@ sw_cont:
header = tmps;
}
k_start = k_end;
} else
}
else
LexExcept(cc, "Expecting ':' at ");
} else
}
else
LexExcept(cc, "Expecting ':' at ");
break;
case KW_DEFAULT:
if (head.next!=&head) {
if (head.next != &head)
{
lb_fwd_case = COCMiscNew(cc, CMT_LABEL);
tmpi_jmp = ICAdd(cc, IC_JMP, lb_fwd_case, 0);//In case fall-thru
}
@ -727,9 +811,11 @@ sw_cont:
Lex(cc);
else
LexExcept(cc, "Expecting ':' at ");
if (head.next!=&head) {
if (head.next != &head)
{
tmpss = head.next;
while (tmpss!=&head) {
while (tmpss != &head)
{
if (tmpss->lb_start)
ICAdd(cc, IC_SUB_CALL, tmpss->lb_start, 0);
tmpss = tmpss->next;
@ -745,12 +831,15 @@ sw_cont:
sw_sub_end:
tmpss = head.last;
ICAdd(cc, IC_LABEL, tmpss->lb_break, 0);
if (tmpss==&head) {
if (tmpss == &head)
{
if (cc->token != '}')
LexExcept(cc, "Missing '}' at ");
Lex(cc);
break;
} else {
}
else
{
QueueRemove(tmpss);
Free(tmpss);
if (ParseKeyWord(cc) != KW_END)
@ -774,7 +863,8 @@ sw_sub_end:
tmpi_sub->ic_data = lo;
tmpi_cmp->ic_data = range;
tmps = header;
while (tmps) {
while (tmps)
{
tmps1 = tmps->next;
if (jmp_table[tmps->val - lo] != lb_default)
LexExcept(cc, "Duplicate case at ");
@ -791,7 +881,8 @@ sw_sub_end:
U0 ParseNoWarn(CCompCtrl *cc)
{
CMemberList *tmpm;
while (cc->token==TK_IDENT) {
while (cc->token == TK_IDENT)
{
if (!(tmpm = cc->local_var_entry))
LexExcept(cc, "Expecting local var at ");
tmpm->flags |= MLF_NO_UNUSED_WARN;
@ -826,8 +917,7 @@ U0 ParseStreamBlk(CCompCtrl *cc)
ExeCmdLine(cc);
MemCopy(&cc->htc, htc, sizeof(CLexHashTableContext));
cc->flags=cc->flags&~CCF_EXE_BLK |
htc->old_flags & (CCF_ASM_EXPRESSIONS|CCF_EXE_BLK|CCF_AOT_COMPILE);
cc->flags = cc->flags & ~CCF_EXE_BLK | htc->old_flags & (CCF_ASM_EXPRESSIONS | CCF_EXE_BLK | CCF_AOT_COMPILE);
Free(htc);
COCPop(cc);
QueueRemove(tmpe);
@ -858,16 +948,17 @@ U0 ParseTryBlk(CCompCtrl *cc,I64 try_count)
ICAdd(cc, IC_CALL_START, 0, 0);
ICAdd(cc, IC_GET_LABEL, lb_untry, tmpc, ICF_PUSH_RES);
ICAdd(cc, IC_GET_LABEL, lb_catch, tmpc, ICF_PUSH_RES);
if (Bt(&tmp_try->flags,Cf_EXTERN)) {
if (Bt(&tmp_try->flags, Cf_EXTERN))
{
cc->abs_counts.externs++;
if (cc->flags & CCF_AOT_COMPILE)
ICAdd(cc, IC_CALL_IMPORT, tmp_try, tmpc);
else
ICAdd(cc, IC_CALL_INDIRECT2, &tmp_try->exe_addr, tmpc);
} else
}
else
ICAdd(cc, IC_CALL, tmp_try->exe_addr, tmpc);
if ((Bt(&tmp_try->flags,Ff_RET1) ||
Bt(&tmp_try->flags,Ff_ARGPOP)) && !Bt(&tmp_try->flags,Ff_NOARGPOP))
if ((Bt(&tmp_try->flags, Ff_RET1) || Bt(&tmp_try->flags, Ff_ARGPOP)) && !Bt(&tmp_try->flags, Ff_NOARGPOP))
ICAdd(cc, IC_ADD_RSP1, 16, tmpc);
else
ICAdd(cc, IC_ADD_RSP, 16, tmpc);
@ -878,13 +969,15 @@ U0 ParseTryBlk(CCompCtrl *cc,I64 try_count)
ICAdd(cc, IC_LABEL, lb_untry, 0);
ICAdd(cc, IC_CALL_START, 0, 0);
if (Bt(&tmp_untry->flags,Cf_EXTERN)) {
if (Bt(&tmp_untry->flags, Cf_EXTERN))
{
cc->abs_counts.externs++;
if (cc->flags & CCF_AOT_COMPILE)
ICAdd(cc, IC_CALL_IMPORT, tmp_untry, tmpc);
else
ICAdd(cc, IC_CALL_INDIRECT2, &tmp_untry->exe_addr, tmpc);
} else
}
else
ICAdd(cc, IC_CALL, tmp_untry->exe_addr, tmpc);
ICAdd(cc, IC_CALL_END, 0, tmpc);
ICAdd(cc, IC_END_EXP, 0, 0, ICF_RES_NOT_USED);
@ -901,8 +994,7 @@ U0 ParseTryBlk(CCompCtrl *cc,I64 try_count)
ICAdd(cc, IC_LABEL, lb_done, 0);
}
Bool ParseStatement(CCompCtrl *cc,I64 try_count=0,
CCodeMisc *lb_break=NULL,I64 comp_flags=CMPF_PRS_SEMICOLON)
Bool ParseStatement(CCompCtrl *cc, I64 try_count=0, CCodeMisc *lb_break=NULL, I64 comp_flags=CMPF_PRS_SEMICOLON)
{
I64 i, fsp_flags = 0;
CHashExport *tmpex;
@ -910,49 +1002,69 @@ Bool ParseStatement(CCompCtrl *cc,I64 try_count=0,
U8 *import_name;
CHashFun *tmp_untry;
CAOT *tmpaot;
if (comp_flags&CMPF_ONE_ASM_INS) {
if (comp_flags & CMPF_ONE_ASM_INS)
{
if (cc->flags & CCF_AOT_COMPILE || cc->aot_depth)
ParseAsmBlk(cc, CMPF_ONE_ASM_INS);
else if (tmpaot = CompJoin(cc, CMPF_ASM_BLK | CMPF_ONE_ASM_INS))
CompFixUpJITAsm(cc, tmpaot);
fsp_flags = FSF_ASM;
} else
while (TRUE) {
}
else
while (TRUE)
{
while (cc->token == ',')
Lex(cc);
if (cc->token=='{') {
if (cc->token == '{')
{
Lex(cc);
while (cc->token != '}' && cc->token != TK_EOF)
ParseStatement(cc, try_count, lb_break);
if (cc->lex_include_stack == cc->fun_lex_file)
cc->max_line = cc->lex_include_stack->line_num;
if (Lex(cc)!=',') goto sm_done;
} else if (cc->token==';') {
if (Lex(cc) != ',')
goto sm_done;
}
else if (cc->token == ';')
{
if (comp_flags & CMPF_PRS_SEMICOLON)
Lex(cc);
if (cc->token!=',') goto sm_done;
} else {
if (cc->token==TK_IDENT) {
if (tmpex=cc->hash_entry) {
if (tmpex->type & HTT_KEYWORD) {
if (cc->token != ',')
goto sm_done;
}
else
{
if (cc->token == TK_IDENT)
{
if (tmpex = cc->hash_entry)
{
if (tmpex->type & HTT_KEYWORD)
{
i = tmpex(CHashGeneric *)->user_data0;
switch [i] {
switch [i]
{
case KW_KWS_NUM - 1: //nobound switch
default: //A keyword that is not valid here is just a symbol.
goto sm_not_keyword_afterall;
start:
case KW_ASM:
if (cc->htc.fun) {
if (cc->htc.fun)
{
if (tmpaot = CompJoin(cc, CMPF_ASM_BLK))
ICAdd(cc, IC_ASM, tmpaot, 0);
Lex(cc); //Skip '}' of asm{}
} else {
if (cc->flags&CCF_AOT_COMPILE || cc->aot_depth) {
}
else
{
if (cc->flags & CCF_AOT_COMPILE || cc->aot_depth)
{
Lex(cc);
ParseAsmBlk(cc, 0);
if (cc->flags & CCF_AOT_COMPILE && cc->aot_depth == 1)
Lex(cc); //Skip '}' of asm{}
} else {
}
else
{
if (tmpaot = CompJoin(cc, CMPF_ASM_BLK))
CompFixUpJITAsm(cc, tmpaot);
Lex(cc); //Skip '}' of asm{}
@ -987,7 +1099,8 @@ Bool ParseStatement(CCompCtrl *cc,I64 try_count=0,
break;
end:
end:
if (cc->token!=',') goto sm_done;
if (cc->token != ',')
goto sm_done;
break;
start:
if (cc->htc.fun)
@ -1021,8 +1134,7 @@ sm_underscore_import:
!(tmpex->type & (HTT_CLASS | HTT_INTERNAL_TYPE)))
LexExcept(cc, "Expecting type at ");
Lex(cc);
ParseGlobalVarList(cc,PRS0__IMPORT|PRS1_NULL,tmpex,
import_name,fsp_flags);
ParseGlobalVarList(cc, PRS0__IMPORT | PRS1_NULL, tmpex, import_name, fsp_flags);
Free(import_name);
break;
case KW_EXTERN:
@ -1030,14 +1142,14 @@ sm_underscore_import:
LexExcept(cc, "Expecting type at ");
tmpex = cc->hash_entry;
i = ParseKeyWord(cc);
if (i==KW_CLASS||i==KW_UNION) {
if (i == KW_CLASS || i == KW_UNION)
{
Lex(cc);
ParseClass(cc, i, fsp_flags, TRUE);
fsp_flags &= FSF_ASM;
goto sm_semicolon;
}
if (!tmpex ||
!(tmpex->type & (HTT_CLASS|HTT_INTERNAL_TYPE)))
if (!tmpex || !(tmpex->type & (HTT_CLASS | HTT_INTERNAL_TYPE)))
LexExcept(cc, "Expecting type at ");
if (Bt(&cc->opts, OPTf_EXTERNS_TO_IMPORTS))
goto sm_import;
@ -1068,8 +1180,7 @@ sm_import:
fsp_flags = FSF_STATIC | fsp_flags & FSF_ASM;
break;
case KW_INTERRUPT:
fsp_flags=FSF_INTERRUPT|FSF_NOARGPOP|
fsp_flags&(FSG_FUN_FLAGS2|FSF_ASM);
fsp_flags = FSF_INTERRUPT | FSF_NOARGPOP | fsp_flags & (FSG_FUN_FLAGS2 | FSF_ASM);
break;
case KW_HASERRCODE:
fsp_flags = FSF_HASERRCODE | fsp_flags & (FSG_FUN_FLAGS2 | FSF_ASM);
@ -1089,39 +1200,42 @@ sm_import:
case KW_RETURN:
if (!cc->htc.fun)
LexExcept(cc, "Not in fun. Can't return a val ");
if (try_count) {
tmp_untry=HashFind("SysUntry",
cc->htc.hash_table_list,HTT_FUN);
for (i=0;i<try_count;i++) {
if (Bt(&tmp_untry->flags,Cf_EXTERN)) {
if (try_count)
{
tmp_untry = HashFind("SysUntry", cc->htc.hash_table_list, HTT_FUN);
for (i = 0; i < try_count; i++)
{
if (Bt(&tmp_untry->flags, Cf_EXTERN))
{
cc->abs_counts.externs++;
if (cc->flags & CCF_AOT_COMPILE)
ICAdd(cc,IC_CALL_IMPORT,
tmp_untry,cmp.internal_types[RT_PTR]);
ICAdd(cc, IC_CALL_IMPORT, tmp_untry, cmp.internal_types[RT_PTR]);
else
ICAdd(cc,IC_CALL_INDIRECT2,
&tmp_untry->exe_addr,
cmp.internal_types[RT_PTR]);
} else
ICAdd(cc,IC_CALL,tmp_untry->exe_addr,
ICAdd(cc, IC_CALL_INDIRECT2, &tmp_untry->exe_addr,
cmp.internal_types[RT_PTR]);
}
else
ICAdd(cc, IC_CALL, tmp_untry->exe_addr, cmp.internal_types[RT_PTR]);
}
if (Lex(cc)!=';') {
}
if (Lex(cc) != ';')
{
if (!cc->htc.fun->return_class->size)
LexWarn(cc, "Function should NOT return val ");
if (!ParseExpression(cc, NULL, FALSE))
throw('Compiler');
ICAdd(cc, IC_RETURN_VAL, 0, cc->htc.fun->return_class);
cc->flags |= CCF_HAS_RETURN;
} else if (cc->htc.fun->return_class->size)
}
else if (cc->htc.fun->return_class->size)
LexWarn(cc, "Function should return val ");
ICAdd(cc, IC_JMP, cc->lb_leave, 0);
goto sm_semicolon;
case KW_GOTO:
if (Lex(cc) != TK_IDENT)
LexExcept(cc, "Expecting identifier at ");
if (!(g_lb=COCGoToLabelFind(cc,cc->cur_str))) {
if (!(g_lb = COCGoToLabelFind(cc, cc->cur_str)))
{
g_lb = COCMiscNew(cc, CMT_GOTO_LABEL);
g_lb->str = cc->cur_str;
cc->cur_str = NULL;
@ -1148,45 +1262,65 @@ sm_import:
ParseGlobalVarList(cc, PRS0_NULL | PRS1_NULL, tmpex, 0, fsp_flags);
fsp_flags &= FSF_ASM;
break;
} else {
}
else
{
fsp_flags &= FSF_ASM;
goto sm_semicolon;
}
}
} else {//Ident, found in hash table, not keyword
}
else
{//Ident, found in hash table, not keyword
sm_not_keyword_afterall:
if (tmpex->type & (HTT_CLASS|HTT_INTERNAL_TYPE)) {
if (cc->htc.fun) {
if (tmpex->type & (HTT_CLASS | HTT_INTERNAL_TYPE))
{
if (cc->htc.fun)
{
if (fsp_flags & FSF_STATIC)
ParseVarList(cc, cc->htc.fun, PRS0_NULL | PRS1_STATIC_LOCAL_VAR);
else
ParseVarList(cc, cc->htc.fun, PRS0_NULL | PRS1_LOCAL_VAR);
if (cc->token=='}') goto sm_done;
} else {
if (cc->token == '}')
goto sm_done;
}
else
{
Lex(cc);
ParseGlobalVarList(cc, PRS0_NULL | PRS1_NULL, tmpex, 0, fsp_flags);
}
} else {
if (tmpex->type & (HTT_OPCODE|HTT_ASM_KEYWORD)) {
if (cc->htc.fun) {
}
else
{
if (tmpex->type & (HTT_OPCODE | HTT_ASM_KEYWORD))
{
if (cc->htc.fun)
{
if (tmpaot = CompJoin(cc, CMPF_ASM_BLK | CMPF_ONE_ASM_INS))
ICAdd(cc, IC_ASM, tmpaot, 0);
} else
}
else
LexExcept(cc, "Use Asm Blk at ");
if (cc->token!=',') goto sm_done;
} else
if (cc->token != ',')
goto sm_done;
}
else
goto sm_parse_exp;
}
fsp_flags &= FSF_ASM;
}
} else {//Ident, not in hash table
}
else
{//Ident, not in hash table
if (cc->local_var_entry)
goto sm_parse_exp;
if (!(g_lb=COCGoToLabelFind(cc,cc->cur_str))) {
if (!(g_lb = COCGoToLabelFind(cc, cc->cur_str)))
{
g_lb = COCMiscNew(cc, CMT_GOTO_LABEL);
g_lb->str = cc->cur_str;
cc->cur_str = NULL;
} else if (g_lb->flags&CMF_DEFINED)
}
else if (g_lb->flags & CMF_DEFINED)
LexExcept(cc, "Duplicate goto label at ");
g_lb->flags |= CMF_DEFINED;
ICAdd(cc, IC_LABEL, g_lb, 0);
@ -1196,24 +1330,32 @@ sm_not_keyword_afterall:
LexExcept(cc, "Undefined identifier at ");
if (!cc->htc.fun)
LexExcept(cc, "No global labels at ");
if (cc->token!=',') goto sm_done;
if (cc->token != ',')
goto sm_done;
}
} else if (cc->token==TK_STR||cc->token==TK_CHAR_CONST) {
}
else if (cc->token == TK_STR || cc->token == TK_CHAR_CONST)
{
ParseFunCall(cc, NULL, FALSE, NULL);
goto sm_semicolon;
} else if (cc->token!=TK_EOF) {//Non-cur_str symbol, num or something
}
else if (cc->token != TK_EOF)
{//Non-cur_str symbol, num or something
sm_parse_exp:
if (!ParseExpression(cc, NULL, TRUE))
throw('Compiler');
sm_semicolon:
if (comp_flags&CMPF_PRS_SEMICOLON) {
if (comp_flags & CMPF_PRS_SEMICOLON)
{
if (cc->token == ';')
Lex(cc);
else if (cc->token != ',')
LexExcept(cc, "Missing ';' at");
}
if (cc->token!=',') goto sm_done;
} else
if (cc->token != ',')
goto sm_done;
}
else
goto sm_done; //TK_EOF
}
}

View file

@ -64,6 +64,7 @@ StartOS.CC
OptPass789A.CC
ParseExp.CC
ParseLib.CC
ParseStatement.CC
Templates.CC
OpCodes.DD
CompilerA.HH