📄 pass1.c
字号:
{
lb.text = strtbl_iStr;
lb.address = address;
lb.line = line;
addStrTbl(name);
addProcLblHL(&lb);
addHashTblEntry(name, lb.text, PROC_LBL, iProc - 1, (proc[iProc - 1].iLabel - 1), line);
}
} /*end .PL*/
else if (strcmp((*tptr).text, ".PE") == 0)
{
/*.PE*/
bRet = match(&t, TOK_NO_MORE);
if (bRet == TRUE)
{
/*
cannot define code outside of a procedure
*/
if (currentProcPass1 == OUTSIDE_PROC_PASS1)
{
ERROR1("pass1_processPDirective(): line %d, .PE declaration not in a proc\n", line);
return;
}
currentProcPass1 = OUTSIDE_PROC_PASS1;
}
else
{
ERROR1("pass1_processPDirective(): line %lu, bad directive\n", line);
return;
}
} /*end .PE*/
else
{
ERROR1("Pass1::processPDirective(): line %lu, bad directive\n", line);
}
return;
}
/*
Do not generate bytecode in first pass, but we need to know
-offset of label directives
-offset of procedures
Thus, we need to do minimal instruction processing to keep
track of bytesize via ->bytePos<- variable
use switch to help cut down on calls to strcmp()
can probably recycle basic skeleton and extend on this code for Pass2
*/
void pass1_processInstruction(struct Token *tptr)
{
if ((*tptr).type != TOK_IDENTIFIER)
{
ERROR3("pass1_processInstruction(): %s on line %lu type %s not a valid opcode\n",
(*tptr).text, (*tptr).line, TokStr[(*tptr).type]);
return;
}
switch ((*tptr).text[0])
{
case 'A':
if (strcmp((*tptr).text, "ADD") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "AND") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'B':
if (strcmp((*tptr).text, "BS") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "BT") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'C':
if (strcmp((*tptr).text, "CAST_IF") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "CAST_ID") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "CAST_FI") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "CAST_FD") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "CAST_DI") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "CAST_DF") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line,(*tptr).text);
return;
}
break;
case 'D':
if (strcmp((*tptr).text, "DIV") == 0)
{
bytePosPass1 = bytePosPass1 + 5;
}
else if (strcmp((*tptr).text, "DI") == 0)
{
bytePosPass1 = bytePosPass1 + 1;
}
else if (strcmp((*tptr).text, "DADD")==0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "DSUB") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "DMULT") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "DDIV") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "DSLT") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'E':
if (strcmp((*tptr).text, "EI") == 0)
{
bytePosPass1 = bytePosPass1 + 1;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'F':
if (strcmp((*tptr).text, "FADD") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "FSUB") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "FMULT") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "FDIV") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "FSLT") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'H':
if (strcmp((*tptr).text, "HALT") == 0)
{
bytePosPass1 = bytePosPass1 + 1;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'I':
if (strcmp((*tptr).text, "INT") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'J':
if (strcmp((*tptr).text, "JMP") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "JE") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "JNE") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'L':
if (strcmp((*tptr).text, "LBI") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "LWI") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "LDI") == 0)
{
bytePosPass1 = bytePosPass1 + 6;
}
else if (strcmp((*tptr).text, "LQI") == 0)
{
bytePosPass1 = bytePosPass1 + 10;
}
else if (strcmp((*tptr).text, "LF1I") == 0)
{
bytePosPass1 = bytePosPass1 + 6;
}
else if (strcmp((*tptr).text, "LF2I") == 0)
{
bytePosPass1 = bytePosPass1 + 10;
}
else if (strcmp((*tptr).text, "LB") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "LW") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "LD") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "LQ") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "LF1") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "LF2") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "LAD") == 0)
{
bytePosPass1 = bytePosPass1 + 10;
}
else if (strcmp((*tptr).text, "LAI") == 0)
{
bytePosPass1 = bytePosPass1 + 11;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'M':
if (strcmp((*tptr).text, "MOV") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "MOVF") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "MOVD") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "MULT") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'N':
if (strcmp((*tptr).text, "NOT") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "NOP") == 0)
{
bytePosPass1 = bytePosPass1 + 1;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line,(*tptr).text);
return;
}
break;
case 'O':
if (strcmp((*tptr).text, "OR") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'P':
if (strcmp((*tptr).text, "PUSHB") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "PUSHW") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "PUSHD") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "PUSHQ") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "PUSHF1") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "PUSHF2") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "POPB") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "POPW") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "POPD") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "POPQ") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "POPF1") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else if (strcmp((*tptr).text, "POPF2") == 0)
{
bytePosPass1 = bytePosPass1 + 2;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'S':
if (strcmp((*tptr).text, "SB") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "SW") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "SD") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "SQ") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "SF1") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "SF2") == 0)
{
bytePosPass1 = bytePosPass1 + 3;
}
else if (strcmp((*tptr).text, "SRA") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "SRL") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "SL") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "SUB") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else if (strcmp((*tptr).text, "SLT") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
case 'X':
if (strcmp((*tptr).text, "XOR") == 0)
{
bytePosPass1 = bytePosPass1 + 4;
}
else
{
ERROR2("pass1_processInstruction(): line %d, invalid opcode (%s)\n",
(*tptr).line, (*tptr).text);
return;
}
break;
default:
ERROR3("pass1_processInstruction(): %s on line %lu type %s not a valid opcode\n",
(*tptr).text, (*tptr).line, TokStr[(*tptr).type]);
return;
} /*end switch*/
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -