asm_engine.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * asm_engine.c
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include "asm_engine.h"
  14. #include "asm_debug.h"
  15. /* Function used when a compare is needed between two labels */
  16. static int compareLabels (void *labelA, void *labelB);
  17. /* Function used to produce the header of an object file.
  18. * This function returns ASM_OK if everything went good*/
  19. static int print_header_infos(FILE *fp);
  20. /* Function that translates every code and data segment.
  21. * This function returns ASM_OK if everything went good */
  22. static int translateCode(t_translation_infos *infos, FILE *fp);
  23. /* This function translates a single instruction found into the
  24. * code segment. Returns ASM_OK if everything went good */
  25. static int translateInstruction(t_translation_infos *infos
  26. , t_asm_instruction *inst, FILE *fp);
  27. /* This function translates a single block of data found into a
  28. * data segment. Returns ASM_OK if everything went good */
  29. static int translateData(t_translation_infos *infos
  30. , t_asm_data *inst, FILE *fp);
  31. /* The function `getBinaryOpcode' returns the coded opcode that will
  32. * be stored into the object file */
  33. static int getBinaryOpcode(int opcode);
  34. /* This function returns the index of the given instruction/data
  35. * that is stored inside the macro block of code plus data */
  36. static int getInstructionOrDataIndex(t_translation_infos *infos, void *target);
  37. /* this function is directly called from `finalizeStructures'. The main
  38. * goal of this function is: free all the memory associated with the
  39. * labels */
  40. static void finalizeLabels(t_list *labels);
  41. /* This function returns the index of the given instruction/data
  42. * that is stored inside the macro block of code plus data */
  43. int getInstructionOrDataIndex(t_translation_infos *infos
  44. , void *target)
  45. {
  46. t_list *target_element;
  47. int position;
  48. /* the list is empty */
  49. if (infos->code == NULL)
  50. return ASM_INVALID_MEMORY_OFFSET;
  51. /* preconditions: infos and inst MUST NOT be NULL references! */
  52. target_element = findElement(infos->code, target);
  53. if (target_element == NULL)
  54. return ASM_INVALID_MEMORY_OFFSET;
  55. /* retrieve the position of the statement */
  56. position = getPosition(infos->code, target_element);
  57. /* the code list doesn't hold target_element */
  58. if (position == -1)
  59. return ASM_INVALID_MEMORY_OFFSET;
  60. /* return the displacement in bytes from the beginning
  61. * of the instruction segment */
  62. if (position < infos->codesize)
  63. return ( ((position * ASM_INSTRUCTION_SIZE) / ASM_ALIGMENT_SIZE)
  64. + ((((position *ASM_INSTRUCTION_SIZE)
  65. % ASM_ALIGMENT_SIZE) > 0)? 1:0) );
  66. else
  67. {
  68. t_list *current_elem;
  69. t_asm_data *current_data;
  70. int counter = 0;
  71. current_elem = getElementAt(infos->code, infos->codesize);
  72. assert(current_elem != NULL);
  73. counter = ( ((infos->codesize * ASM_INSTRUCTION_SIZE) / ASM_ALIGMENT_SIZE)
  74. + ((((infos->codesize * ASM_INSTRUCTION_SIZE)
  75. % ASM_ALIGMENT_SIZE) > 0)? 1:0) );
  76. while (current_elem != NULL && LDATA(current_elem) != target)
  77. {
  78. current_data = LDATA(current_elem);
  79. if (current_data->dataType == ASM_WORD)
  80. {
  81. counter += (ASM_WORD_SIZE / ASM_ALIGMENT_SIZE)
  82. + (((ASM_WORD_SIZE % ASM_ALIGMENT_SIZE) > 0)? 1 : 0);
  83. }
  84. else if (current_data->dataType == ASM_SPACE)
  85. {
  86. /* precondition always verified */
  87. assert(current_data->value > 0);
  88. counter += (current_data->value / ASM_ALIGMENT_SIZE)
  89. + (((current_data->value % ASM_ALIGMENT_SIZE) > 0)? 1: 0);
  90. }
  91. current_elem = LNEXT(current_elem);
  92. }
  93. return counter;
  94. }
  95. return -1;
  96. }
  97. /* this function translates a single block of data found into a
  98. * data segment. Returns ASM_OK if everything went good */
  99. int translateData(t_translation_infos *infos
  100. , t_asm_data *data, FILE *fp)
  101. {
  102. if (data == NULL)
  103. return ASM_UNDEFINED_DATA;
  104. if (data->dataType == ASM_WORD)
  105. {
  106. /* postcondition: print on file the coded data */
  107. if (fwrite(&(data->value), ASM_WORD_SIZE, 1, fp) != 1)
  108. return ASM_FWRITE_ERROR;
  109. return ASM_OK;
  110. }
  111. else if (data->dataType == ASM_SPACE)
  112. {
  113. char *zero_val;
  114. /* preconditions: data->value must be >= 0 */
  115. if (data->value < 0)
  116. return ASM_INVALID_DATA_FORMAT;
  117. /* force alignment to 4 bytes */
  118. if (data->value % ASM_WORD_SIZE != 0)
  119. data->value = data->value + (data->value % ASM_WORD_SIZE);
  120. /* initialize the value `zero_val' */
  121. zero_val = _ASM_ALLOC_FUNCTION(sizeof(char) * data->value);
  122. if (zero_val == NULL)
  123. return ASM_OUT_OF_MEMORY;
  124. /* set the block of memory to zero */
  125. memset(zero_val, 0, data->value);
  126. /* postcondition: print on file the coded data */
  127. if (fwrite(zero_val, data->value, 1, fp) != 1)
  128. {
  129. _ASM_FREE_FUNCTION(zero_val);
  130. return ASM_FWRITE_ERROR;
  131. }
  132. _ASM_FREE_FUNCTION(zero_val);
  133. return ASM_OK;
  134. }
  135. /* default: the data format in unknown */
  136. return ASM_INVALID_DATA_FORMAT;
  137. }
  138. /* This function translates a single instruction found into the
  139. * code segment. Returns ASM_OK if everything went good */
  140. int translateInstruction(t_translation_infos *infos
  141. , t_asm_instruction *inst, FILE *fp)
  142. {
  143. int instruction;
  144. int pattern;
  145. int func;
  146. /* preconditions */
  147. if (inst == NULL)
  148. return ASM_UNDEFINED_INSTRUCTION;
  149. if (fp == NULL)
  150. return ASM_INVALID_INPUT_FILE;
  151. #ifndef NDEBUG
  152. fprintf(stderr, "Coding instruction: [opcode == %s, format == %s] --> "
  153. , opcode_toString(inst->opcode)
  154. , dataFormat_toString(inst->format) );
  155. #endif
  156. /* initialize the instruction */
  157. instruction = 0;
  158. /* set the format of instruction */
  159. if (inst->format == ASM_FORMAT_TER)
  160. {
  161. pattern = 0;
  162. }
  163. else if (inst->format == ASM_FORMAT_BIN)
  164. {
  165. pattern = (1 << 30);
  166. }
  167. else if ( (inst->format == ASM_FORMAT_UNR)
  168. || (inst->format == ASM_FORMAT_NULL) )
  169. {
  170. pattern = (1 << 31);
  171. }
  172. else
  173. {
  174. assert(inst->format == ASM_FORMAT_JMP);
  175. pattern = (3 << 30);
  176. }
  177. /* update the instruction format value */
  178. instruction = pattern;
  179. /* initialize the opcode information */
  180. pattern = getBinaryOpcode(inst->opcode);
  181. /* test if the opcode is valid */
  182. if (pattern == INVALID_OPCODE)
  183. return ASM_INVALID_OPCODE;
  184. /* update the instruction format value */
  185. instruction = instruction + (pattern << 26);
  186. /* initialize the value of `func' */
  187. func = 0;
  188. if (inst->format == ASM_FORMAT_TER)
  189. {
  190. if ((inst->reg_1)->indirect)
  191. func = 4;
  192. if ((inst->reg_3)->indirect)
  193. func = func + 8;
  194. instruction = instruction + ( ((inst->reg_1)->ID) << 21);
  195. instruction = instruction + ( ((inst->reg_2)->ID) << 16);
  196. instruction = instruction + ( ((inst->reg_3)->ID) << 11);
  197. instruction = instruction + func;
  198. }
  199. else if (inst->format == ASM_FORMAT_BIN)
  200. {
  201. instruction = instruction + ( ((inst->reg_1)->ID) << 21);
  202. instruction = instruction + ( ((inst->reg_2)->ID) << 16);
  203. instruction = instruction + ( inst->immediate & 0x0000FFFF);
  204. }
  205. else if (inst->format == ASM_FORMAT_UNR)
  206. {
  207. instruction = instruction + ( ((inst->reg_1)->ID) << 21);
  208. if ((inst->address)->label != NULL)
  209. {
  210. int destinationIndex;
  211. /* we have to retrieve the position of the requested data */
  212. destinationIndex = getInstructionOrDataIndex
  213. (infos, ((inst->address)->label)->data);
  214. /* test if the destinationIndex is a valid destination */
  215. if (destinationIndex == ASM_INVALID_MEMORY_OFFSET)
  216. return ASM_INVALID_LABEL_FOUND;
  217. /* update the displacement information */
  218. instruction = instruction
  219. + ( destinationIndex & 0x000FFFFF);
  220. }
  221. else
  222. {
  223. instruction = instruction + ( (inst->address)->addr & 0x000FFFFF);
  224. }
  225. }
  226. else if (inst->format == ASM_FORMAT_NULL)
  227. {
  228. /* DOES NOTHING */
  229. }
  230. else
  231. {
  232. /* test a precondition */
  233. assert(inst->format == ASM_FORMAT_JMP);
  234. if ((inst->address)->label != NULL)
  235. {
  236. int currentIndex;
  237. int destinationIndex;
  238. /* we have to retrieve the displacement between this
  239. * instruction and the instruction referred by the
  240. * label */
  241. currentIndex = getInstructionOrDataIndex(infos, inst);
  242. destinationIndex = getInstructionOrDataIndex
  243. (infos, ((inst->address)->label)->data);
  244. /* test if the destinationIndex is a valid destination */
  245. if (destinationIndex == ASM_INVALID_MEMORY_OFFSET)
  246. return ASM_INVALID_LABEL_FOUND;
  247. /* postcondition that MUST be always verified */
  248. assert(destinationIndex < infos->codesize);
  249. /* update the displacement information */
  250. instruction = instruction
  251. + ( (destinationIndex - currentIndex) & 0x000FFFFF);
  252. }
  253. else
  254. {
  255. instruction = instruction + ( (inst->address)->addr & 0x000FFFFF);
  256. }
  257. }
  258. /* postcondition: print on file the coded instruction */
  259. if (fwrite(&instruction, 4, 1, fp) != 1)
  260. return ASM_FWRITE_ERROR;
  261. #ifndef NDEBUG
  262. fprintf(stderr, "Coded Instruction:\t[0x%08x] \n", instruction);
  263. #endif
  264. return ASM_OK;
  265. }
  266. /* function used when a compare is needed between two labels */
  267. int compareLabels (void *labelA, void *labelB)
  268. {
  269. t_asm_label *asm_labelA;
  270. t_asm_label *asm_labelB;
  271. /* preconditions */
  272. if (labelA == NULL)
  273. {
  274. if (labelB == NULL)
  275. return 1;
  276. return 0;
  277. }
  278. if (labelB == NULL)
  279. return 0;
  280. /* initialize labels */
  281. asm_labelA = (t_asm_label *) labelA;
  282. asm_labelB = (t_asm_label *) labelB;
  283. /* verify the consistency of this operation */
  284. assert(asm_labelA->ID != NULL);
  285. assert(asm_labelB->ID != NULL);
  286. /* postcondition */
  287. return (!strcmp(asm_labelA->ID, asm_labelB->ID));
  288. }
  289. /* create an instance of `t_translation_info' initializing the internal data
  290. * of every field of the structure */
  291. t_translation_infos * initStructures(int *errorcode)
  292. {
  293. t_translation_infos *result;
  294. /* allocate memory for an instance of `t_translation_infos' */
  295. result = _ASM_ALLOC_FUNCTION(sizeof(t_translation_infos));
  296. /* test the out of memory condition */
  297. if (result == NULL)
  298. {
  299. /* update the value of `errorcode' */
  300. (*errorcode) = ASM_OUT_OF_MEMORY;
  301. return NULL;
  302. }
  303. /* no errors encountered so far */
  304. (*errorcode) = ASM_OK;
  305. /* initialize the content of `result' */
  306. result->code = NULL;
  307. result->labels = NULL;
  308. result->codesize = 0;
  309. /* return a new instance of `t_translation_infos' */
  310. return result;
  311. }
  312. /* Insert an instruction inside the `code' list of `infos' */
  313. int addInstruction(t_translation_infos *infos
  314. , t_asm_instruction *instruction)
  315. {
  316. /* preconditions */
  317. if (infos == NULL)
  318. return ASM_NOT_INITIALIZED_INFO;
  319. if (instruction == NULL)
  320. return ASM_UNDEFINED_INSTRUCTION;
  321. /* update the list of instructions */
  322. infos->code = addElement(infos->code, instruction, infos->codesize);
  323. /* update the codesize */
  324. infos->codesize++;
  325. /* notify that everything went correctly */
  326. return ASM_OK;
  327. }
  328. /* Insert a new label. The label must be initialized externally */
  329. int insertLabel(t_translation_infos *infos, t_asm_label *label)
  330. {
  331. /* preconditions */
  332. if (infos == NULL)
  333. return ASM_NOT_INITIALIZED_INFO;
  334. if (label == NULL)
  335. return ASM_INVALID_LABEL_FOUND;
  336. /* update the list of labels */
  337. infos->labels = addFirst(infos->labels, label);
  338. /* notify that everything went correctly */
  339. return ASM_OK;
  340. }
  341. /* find a label with a given `ID' */
  342. t_asm_label * findLabel(t_translation_infos *infos, char *ID, int *asm_errorcode)
  343. {
  344. t_asm_label pattern;
  345. t_list *label_element;
  346. /* preconditions */
  347. if (infos == NULL && asm_errorcode != NULL)
  348. (*asm_errorcode) = ASM_NOT_INITIALIZED_INFO;
  349. if (ID == NULL && asm_errorcode != NULL)
  350. (*asm_errorcode) = ASM_INVALID_LABEL_FOUND;
  351. /* initialize the value of `asm_errorcode' */
  352. (*asm_errorcode) = ASM_OK;
  353. /* initialize `pattern' */
  354. pattern.ID = ID;
  355. pattern.data = NULL;
  356. /* search the label */
  357. label_element = CustomfindElement(infos->labels, &pattern, compareLabels);
  358. /* if not found return a NULL pointer */
  359. if (label_element == NULL)
  360. return NULL;
  361. /* return the label found */
  362. return (t_asm_label *) LDATA(label_element);
  363. }
  364. /* remove a label */
  365. int removeLabel(t_translation_infos *infos, char *ID)
  366. {
  367. t_asm_label *result;
  368. int asm_errorcode;
  369. /* initialize the value of `asm_errorcode' */
  370. asm_errorcode = ASM_OK;
  371. /* initialize the value of `result' */
  372. result = findLabel(infos, ID, &asm_errorcode);
  373. /* postconditions */
  374. if (result == NULL)
  375. return asm_errorcode;
  376. /* remove the label from the list */
  377. infos->labels = removeElement(infos->labels, result);
  378. return asm_errorcode;
  379. }
  380. /* add a block of data into the data segment */
  381. int addData(t_translation_infos *infos, t_asm_data *data)
  382. {
  383. /* preconditions */
  384. if (infos == NULL)
  385. return ASM_NOT_INITIALIZED_INFO;
  386. if (data == NULL)
  387. return ASM_UNDEFINED_DATA;
  388. /* update the list of instructions */
  389. infos->code = addElement(infos->code, data, -1);
  390. return ASM_OK;
  391. }
  392. /* finalization of the `infos' structure */
  393. int finalizeStructures(t_translation_infos *infos)
  394. {
  395. if (infos == NULL)
  396. return ASM_NOT_INITIALIZED_INFO;
  397. if (infos->code != NULL)
  398. {
  399. t_list *current_element;
  400. t_asm_instruction *current_instr;
  401. /* initialize `data' */
  402. current_element = infos->code;
  403. while ((current_element != NULL) && (infos->codesize > 0) )
  404. {
  405. current_instr = (t_asm_instruction *) LDATA(current_element);
  406. /* free memory associated with the current instruction */
  407. freeInstruction(current_instr);
  408. /* update the value of `current_element' */
  409. current_element = LNEXT(current_element);
  410. infos->codesize --;
  411. }
  412. while (current_element != NULL)
  413. {
  414. /* free memory associated with the current data info. */
  415. freeData((t_asm_data *) LDATA(current_element));
  416. /* update the value of `current_element' */
  417. current_element = LNEXT(current_element);
  418. }
  419. /* free the code and data segment infos */
  420. freeList(infos->code);
  421. }
  422. /* remove labels */
  423. finalizeLabels(infos->labels);
  424. /* free the memory block associated with `infos' */
  425. _ASM_FREE_FUNCTION(infos);
  426. return ASM_OK;
  427. }
  428. /* begin the translation process */
  429. int asm_writeObjectFile(t_translation_infos *infos, char *output_file)
  430. {
  431. FILE *fp;
  432. int errorcode;
  433. if (output_file == NULL)
  434. {
  435. /* set "output.o" as output file name */
  436. output_file = "output.o";
  437. }
  438. #ifndef NDEBUG
  439. fprintf(stdout, "\n\n*******************************************\n");
  440. fprintf(stdout, "INITIALIZE OUTPUT FILE: %s. \n", output_file);
  441. fprintf(stdout, "CODE SEGMENT has a size of %d instructions \n", infos->codesize);
  442. fprintf(stdout, "DATA SEGMENT has a size of %d elements \n"
  443. , (getLength(infos->code) - infos->codesize) );
  444. fprintf(stdout, "NUMBER OF LABELS : %d. \n", getLength(infos->labels));
  445. fprintf(stdout, "*******************************************\n\n");
  446. #endif
  447. /* open a new file */
  448. fp = fopen(output_file, "w");
  449. if (fp == NULL)
  450. return ASM_FOPEN_ERROR;
  451. /* print the header of the object file */
  452. errorcode = print_header_infos(fp);
  453. if (errorcode != ASM_OK)
  454. {
  455. if (fclose(fp) == EOF)
  456. return ASM_FCLOSE_ERROR;
  457. return errorcode;
  458. }
  459. /* print the code and data segment */
  460. errorcode = translateCode(infos, fp);
  461. if (errorcode != ASM_OK)
  462. {
  463. if (fclose(fp) == EOF)
  464. return ASM_FCLOSE_ERROR;
  465. return errorcode;
  466. }
  467. /* print the trailer informations */
  468. //DOES NOTHING
  469. /* close the file and return */
  470. errorcode = fclose(fp);
  471. if (errorcode == EOF)
  472. return ASM_FCLOSE_ERROR;
  473. return ASM_OK;
  474. }
  475. /* Function that translates every code and data segment.
  476. * This function returns ASM_OK if everything went good */
  477. int translateCode(t_translation_infos *infos, FILE *fp)
  478. {
  479. int instruction_counter;
  480. t_list *current_instruction;
  481. void *instruction_or_data;
  482. int errorcode;
  483. /* unchecked preconditions: pf and infos are different from NULL */
  484. if (infos->code == NULL)
  485. return ASM_CODE_NOT_PRESENT;
  486. /* initialize the instruction_counter */
  487. instruction_counter = 0;
  488. current_instruction = infos->code;
  489. errorcode = ASM_OK;
  490. /* translate the instruction segment */
  491. while (instruction_counter < infos->codesize)
  492. {
  493. instruction_or_data = LDATA(current_instruction);
  494. assert(instruction_or_data != NULL);
  495. /* translate every single instruction */
  496. errorcode = translateInstruction
  497. (infos, (t_asm_instruction *) instruction_or_data, fp);
  498. /* verify the errorcode */
  499. if (errorcode != ASM_OK)
  500. return errorcode;
  501. /* update the instruction counter and the current instruction data */
  502. instruction_counter++;
  503. current_instruction = LNEXT(current_instruction);
  504. }
  505. #ifndef NDEBUG
  506. fprintf(stderr, "\n");
  507. #endif
  508. /* translate the data segment */
  509. while (current_instruction != NULL)
  510. {
  511. instruction_or_data = LDATA(current_instruction);
  512. assert(instruction_or_data != NULL);
  513. /* translate every single element of data */
  514. #ifndef NDEBUG
  515. fprintf(stderr, "Adding data into the data segment [datatype == %s \t; "
  516. , dataType_toString(((t_asm_data *)instruction_or_data)->dataType) );
  517. fprintf(stderr, "value == 0x%08x] \n"
  518. , ((t_asm_data *)instruction_or_data)->value);
  519. #endif
  520. errorcode = translateData(infos, (t_asm_data *) instruction_or_data, fp);
  521. /* verify the errorcode */
  522. if (errorcode != ASM_OK)
  523. return errorcode;
  524. current_instruction = LNEXT(current_instruction);
  525. }
  526. #ifndef NDEBUG
  527. fprintf(stderr, "\n");
  528. #endif
  529. return ASM_OK;
  530. }
  531. /* Function used to produce the header of an object file.
  532. * This function returns ASM_OK if everything went good*/
  533. int print_header_infos(FILE *fp)
  534. {
  535. char begin_header[4] = {'L', 'F', 'C', 'M'};
  536. char other_data[16];
  537. /* preconditions */
  538. if (fp == NULL)
  539. return ASM_INVALID_INPUT_FILE;
  540. /* write the starting string `LFCM' without the end of string '\0' */
  541. fputc(begin_header[0], fp);
  542. fputc(begin_header[1], fp);
  543. fputc(begin_header[2], fp);
  544. fputc(begin_header[3], fp);
  545. /* set `other_data' */
  546. memset(other_data, 0, 16);
  547. /* write the other_data infos */
  548. if (fwrite(other_data, 1, 16, fp) != 16)
  549. return ASM_FWRITE_ERROR;
  550. return ASM_OK;
  551. }
  552. /* The function `getBinaryOpcode' returns the coded opcode that will
  553. * be stored into the object file */
  554. int getBinaryOpcode(int opcode)
  555. {
  556. switch(opcode)
  557. {
  558. case ADD_OP: return 0;
  559. case SUB_OP: return 1;
  560. case ANDL_OP: return 2;
  561. case ORL_OP: return 3;
  562. case XORL_OP: return 4;
  563. case ANDB_OP: return 5;
  564. case ORB_OP: return 6;
  565. case XORB_OP: return 7;
  566. case MUL_OP: return 8;
  567. case DIV_OP: return 9;
  568. case SHL_OP: return 10;
  569. case SHR_OP: return 11;
  570. case ROTL_OP: return 12;
  571. case ROTR_OP: return 13;
  572. case NEG_OP: return 14;
  573. case SPCL_OP: return 15;
  574. case ADDI_OP: return 0;
  575. case SUBI_OP: return 1;
  576. case ANDLI_OP: return 2;
  577. case ORLI_OP: return 3;
  578. case XORLI_OP: return 4;
  579. case ANDBI_OP: return 5;
  580. case ORBI_OP: return 6;
  581. case XORBI_OP: return 7;
  582. case MULI_OP: return 8;
  583. case DIVI_OP: return 9;
  584. case SHLI_OP: return 10;
  585. case SHRI_OP: return 11;
  586. case ROTLI_OP: return 12;
  587. case ROTRI_OP: return 13;
  588. case NOTL_OP: return 14;
  589. case NOTB_OP: return 15;
  590. case NOP_OP: return 0;
  591. case MOVA_OP: return 1;
  592. case LOAD_OP: return 4;
  593. case STORE_OP: return 5;
  594. case JSR_OP: return 2;
  595. case RET_OP: return 3;
  596. case HALT_OP: return 6;
  597. case SEQ_OP: return 7;
  598. case SGE_OP: return 8;
  599. case SGT_OP: return 9;
  600. case SLE_OP: return 10;
  601. case SLT_OP: return 11;
  602. case SNE_OP: return 12;
  603. case READ_OP: return 13;
  604. case WRITE_OP: return 14;
  605. case BT_OP: return 0;
  606. case BF_OP: return 1;
  607. case BHI_OP: return 2;
  608. case BLS_OP: return 3;
  609. case BCC_OP: return 4;
  610. case BCS_OP: return 5;
  611. case BNE_OP: return 6;
  612. case BEQ_OP: return 7;
  613. case BVC_OP: return 8;
  614. case BVS_OP: return 9;
  615. case BPL_OP: return 10;
  616. case BMI_OP: return 11;
  617. case BGE_OP: return 12;
  618. case BLT_OP: return 13;
  619. case BGT_OP: return 14;
  620. case BLE_OP: return 15;
  621. default: return INVALID_OPCODE;
  622. }
  623. }
  624. void finalizeLabels(t_list *labels)
  625. {
  626. t_list *current_element;
  627. t_asm_label *current_label;
  628. if (labels == NULL)
  629. return;
  630. current_element = labels;
  631. while(current_element != NULL)
  632. {
  633. current_label = (t_asm_label *) LDATA(current_element);
  634. if (current_label != NULL)
  635. {
  636. if (current_label->ID != NULL)
  637. free(current_label->ID);
  638. _ASM_FREE_FUNCTION(current_label);
  639. }
  640. current_element = LNEXT(current_element);
  641. }
  642. freeList(labels);
  643. }