M1-macro_x86.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. ## This file was generated by running:
  2. ## cat functions/exit.c functions/file.c functions/file_print.c functions/malloc.c functions/calloc.c functions/match.c functions/numerate_number.c functions/string.c test/test23/M1-macro.c >| ../stage0/stage3/M1-macro_x86.c
  3. ## inside the M2-Planet repo
  4. ## Copyright (C) 2016 Jeremiah Orians
  5. ## This file is part of stage0.
  6. ##
  7. ## stage0 is free software: you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation, either version 3 of the License, or
  10. ## (at your option) any later version.
  11. ##
  12. ## stage0 is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ## GNU General Public License for more details.
  16. ##
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with stage0. If not, see <http://www.gnu.org/licenses/>.
  19. // CONSTANT EXIT_FAILURE 1
  20. // CONSTANT EXIT_SUCCESS 0
  21. void exit(int value)
  22. {
  23. asm("POP_ebx"
  24. "POP_ebx"
  25. "LOAD_IMMEDIATE_eax %1"
  26. "INT_80");
  27. }
  28. /* Copyright (C) 2016 Jeremiah Orians
  29. * This file is part of stage0.
  30. *
  31. * stage0 is free software: you can redistribute it and/or modify
  32. * it under the terms of the GNU General Public License as published by
  33. * the Free Software Foundation, either version 3 of the License, or
  34. * (at your option) any later version.
  35. *
  36. * stage0 is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  43. */
  44. // CONSTANT stdin 0
  45. // CONSTANT stdout 1
  46. // CONSTANT stderr 2
  47. // CONSTANT EOF 0xFFFFFFFF
  48. int fgetc(FILE* f)
  49. {
  50. asm("LOAD_IMMEDIATE_eax %3"
  51. "LOAD_EFFECTIVE_ADDRESS_ebx %4"
  52. "LOAD_INTEGER_ebx"
  53. "PUSH_ebx"
  54. "COPY_esp_to_ecx"
  55. "LOAD_IMMEDIATE_edx %1"
  56. "INT_80"
  57. "TEST"
  58. "POP_eax"
  59. "JUMP_NE8 !FUNCTION_fgetc_Done"
  60. "LOAD_IMMEDIATE_eax %-1"
  61. ":FUNCTION_fgetc_Done");
  62. }
  63. void fputc(char s, FILE* f)
  64. {
  65. asm("LOAD_IMMEDIATE_eax %4"
  66. "LOAD_EFFECTIVE_ADDRESS_ebx %4"
  67. "LOAD_INTEGER_ebx"
  68. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  69. "LOAD_IMMEDIATE_edx %1"
  70. "INT_80");
  71. }
  72. /* Important values needed for open
  73. * O_RDONLY => 0
  74. * O_WRONLY => 1
  75. * O_RDWR => 2
  76. * O_CREAT => 64
  77. * O_TRUNC => 512
  78. * S_IRWXU => 00700
  79. * S_IXUSR => 00100
  80. * S_IWUSR => 00200
  81. * S_IRUSR => 00400
  82. */
  83. FILE* open(char* name, int flag, int mode)
  84. {
  85. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  86. "LOAD_INTEGER_ebx"
  87. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  88. "LOAD_INTEGER_ecx"
  89. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  90. "LOAD_INTEGER_edx"
  91. "LOAD_IMMEDIATE_eax %5"
  92. "INT_80");
  93. }
  94. FILE* fopen(char* filename, char* mode)
  95. {
  96. FILE* f;
  97. if('w' == mode[0])
  98. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  99. f = open(filename, 577 , 384);
  100. }
  101. else
  102. { /* Everything else is a read */
  103. f = open(filename, 0, 0);
  104. }
  105. /* Negative numbers are error codes */
  106. if(0 > f)
  107. {
  108. return 0;
  109. }
  110. return f;
  111. }
  112. int close(int fd)
  113. {
  114. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  115. "LOAD_IMMEDIATE_eax %6"
  116. "INT_80");
  117. }
  118. int fclose(FILE* stream)
  119. {
  120. int error = close(stream);
  121. return error;
  122. }
  123. /* Copyright (C) 2016 Jeremiah Orians
  124. * This file is part of stage0.
  125. *
  126. * stage0 is free software: you can redistribute it and/or modify
  127. * it under the terms of the GNU General Public License as published by
  128. * the Free Software Foundation, either version 3 of the License, or
  129. * (at your option) any later version.
  130. *
  131. * stage0 is distributed in the hope that it will be useful,
  132. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  133. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  134. * GNU General Public License for more details.
  135. *
  136. * You should have received a copy of the GNU General Public License
  137. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  138. */
  139. #include<stdio.h>
  140. // void fputc(char s, FILE* f);
  141. void file_print(char* s, FILE* f)
  142. {
  143. while(0 != s[0])
  144. {
  145. fputc(s[0], f);
  146. s = s + 1;
  147. }
  148. }
  149. ## Copyright (C) 2016 Jeremiah Orians
  150. ## This file is part of stage0.
  151. ##
  152. ## stage0 is free software: you can redistribute it and/or modify
  153. ## it under the terms of the GNU General Public License as published by
  154. ## the Free Software Foundation, either version 3 of the License, or
  155. ## (at your option) any later version.
  156. ##
  157. ## stage0 is distributed in the hope that it will be useful,
  158. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  159. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  160. ## GNU General Public License for more details.
  161. ##
  162. ## You should have received a copy of the GNU General Public License
  163. ## along with stage0. If not, see <http://www.gnu.org/licenses/>.
  164. // CONSTANT NULL 0
  165. void* malloc(int size)
  166. {
  167. asm("STORE_eax_into_ESP_IMMEDIATE8 !4"
  168. "PUSH_eax"
  169. "LOAD_IMMEDIATE_eax %45"
  170. "LOAD_IMMEDIATE_ebx %0"
  171. "INT_80"
  172. "POP_ebx"
  173. "ADD_eax_to_ebx"
  174. "PUSH_eax"
  175. "PUSH_ebx"
  176. "LOAD_IMMEDIATE_eax %45"
  177. "INT_80"
  178. "POP_ebx"
  179. "CMP"
  180. "POP_eax"
  181. "JUMP_EQ8 !FUNCTION_malloc_Done"
  182. "LOAD_IMMEDIATE_eax %-1"
  183. ":FUNCTION_malloc_Done");
  184. }
  185. /* Copyright (C) 2016 Jeremiah Orians
  186. * This file is part of stage0.
  187. *
  188. * stage0 is free software: you can redistribute it and/or modify
  189. * it under the terms of the GNU General Public License as published by
  190. * the Free Software Foundation, either version 3 of the License, or
  191. * (at your option) any later version.
  192. *
  193. * stage0 is distributed in the hope that it will be useful,
  194. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  195. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  196. * GNU General Public License for more details.
  197. *
  198. * You should have received a copy of the GNU General Public License
  199. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  200. */
  201. // void* malloc(int size);
  202. void* memset(void* ptr, int value, int num)
  203. {
  204. char* s;
  205. for(s = ptr; 0 < num; num = num - 1)
  206. {
  207. s[0] = value;
  208. s = s + 1;
  209. }
  210. }
  211. void* calloc(int count, int size)
  212. {
  213. void* ret = malloc(count * size);
  214. memset(ret, 0, (count * size));
  215. return ret;
  216. }
  217. void free(void* l)
  218. {
  219. return;
  220. }
  221. /* Copyright (C) 2016 Jeremiah Orians
  222. * This file is part of stage0.
  223. *
  224. * stage0 is free software: you can redistribute it and/or modify
  225. * it under the terms of the GNU General Public License as published by
  226. * the Free Software Foundation, either version 3 of the License, or
  227. * (at your option) any later version.
  228. *
  229. * stage0 is distributed in the hope that it will be useful,
  230. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  231. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  232. * GNU General Public License for more details.
  233. *
  234. * You should have received a copy of the GNU General Public License
  235. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  236. */
  237. #define FALSE 0
  238. // CONSTANT FALSE 0
  239. #define TRUE 1
  240. // CONSTANT TRUE 1
  241. int match(char* a, char* b)
  242. {
  243. int i = -1;
  244. do
  245. {
  246. i = i + 1;
  247. if(a[i] != b[i])
  248. {
  249. return FALSE;
  250. }
  251. } while((0 != a[i]) && (0 !=b[i]));
  252. return TRUE;
  253. }
  254. /* Copyright (C) 2016 Jeremiah Orians
  255. * This file is part of stage0.
  256. *
  257. * stage0 is free software: you can redistribute it and/or modify
  258. * it under the terms of the GNU General Public License as published by
  259. * the Free Software Foundation, either version 3 of the License, or
  260. * (at your option) any later version.
  261. *
  262. * stage0 is distributed in the hope that it will be useful,
  263. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  264. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  265. * GNU General Public License for more details.
  266. *
  267. * You should have received a copy of the GNU General Public License
  268. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  269. */
  270. #include<stdlib.h>
  271. #include<string.h>
  272. // void* calloc(int count, int size);
  273. #define TRUE 1
  274. //CONSTANT TRUE 1
  275. #define FALSE 0
  276. //CONSTANT FALSE 0
  277. char* numerate_number(int a)
  278. {
  279. char* result = calloc(16, sizeof(char));
  280. int i = 0;
  281. /* Deal with Zero case */
  282. if(0 == a)
  283. {
  284. result[0] = '0';
  285. return result;
  286. }
  287. /* Deal with negatives */
  288. if(0 > a)
  289. {
  290. result[0] = '-';
  291. i = 1;
  292. a = a * -1;
  293. }
  294. /* Using the largest 10^n number possible in 32bits */
  295. int divisor = 0x3B9ACA00;
  296. /* Skip leading Zeros */
  297. while(0 == (a / divisor)) divisor = divisor / 10;
  298. /* Now simply collect numbers until divisor is gone */
  299. while(0 < divisor)
  300. {
  301. result[i] = ((a / divisor) + 48);
  302. a = a % divisor;
  303. divisor = divisor / 10;
  304. i = i + 1;
  305. }
  306. return result;
  307. }
  308. int char2hex(int c)
  309. {
  310. if (c >= '0' && c <= '9') return (c - 48);
  311. else if (c >= 'a' && c <= 'f') return (c - 87);
  312. else if (c >= 'A' && c <= 'F') return (c - 55);
  313. else return -1;
  314. }
  315. int hex2char(int c)
  316. {
  317. if((c >= 0) && (c <= 9)) return (c + 48);
  318. else if((c >= 10) && (c <= 15)) return (c + 55);
  319. else return -1;
  320. }
  321. int char2dec(int c)
  322. {
  323. if (c >= '0' && c <= '9') return (c - 48);
  324. else return -1;
  325. }
  326. int dec2char(int c)
  327. {
  328. if((c >= 0) && (c <= 9)) return (c + 48);
  329. else return -1;
  330. }
  331. int numerate_string(char *a)
  332. {
  333. int count = 0;
  334. int index;
  335. int negative;
  336. /* If NULL string */
  337. if(0 == a[0])
  338. {
  339. return 0;
  340. }
  341. /* Deal with hex */
  342. else if (a[0] == '0' && a[1] == 'x')
  343. {
  344. if('-' == a[2])
  345. {
  346. negative = TRUE;
  347. index = 3;
  348. }
  349. else
  350. {
  351. negative = FALSE;
  352. index = 2;
  353. }
  354. while(0 != a[index])
  355. {
  356. if(-1 == char2hex(a[index])) return 0;
  357. count = (16 * count) + char2hex(a[index]);
  358. index = index + 1;
  359. }
  360. }
  361. /* Deal with decimal */
  362. else
  363. {
  364. if('-' == a[0])
  365. {
  366. negative = TRUE;
  367. index = 1;
  368. }
  369. else
  370. {
  371. negative = FALSE;
  372. index = 0;
  373. }
  374. while(0 != a[index])
  375. {
  376. if(-1 == char2dec(a[index])) return 0;
  377. count = (10 * count) + char2dec(a[index]);
  378. index = index + 1;
  379. }
  380. }
  381. if(negative)
  382. {
  383. count = count * -1;
  384. }
  385. return count;
  386. }
  387. /* Copyright (C) 2016 Jeremiah Orians
  388. * This file is part of stage0.
  389. *
  390. * stage0 is free software: you can redistribute it and/or modify
  391. * it under the terms of the GNU General Public License as published by
  392. * the Free Software Foundation, either version 3 of the License, or
  393. * (at your option) any later version.
  394. *
  395. * stage0 is distributed in the hope that it will be useful,
  396. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  397. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  398. * GNU General Public License for more details.
  399. *
  400. * You should have received a copy of the GNU General Public License
  401. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  402. */
  403. #include<stdlib.h>
  404. #define MAX_STRING 4096
  405. //CONSTANT MAX_STRING 4096
  406. // void* calloc(int count, int size);
  407. char* copy_string(char* target, char* source)
  408. {
  409. while(0 != source[0])
  410. {
  411. target[0] = source[0];
  412. target = target + 1;
  413. source = source + 1;
  414. }
  415. return target;
  416. }
  417. char* postpend_char(char* s, char a)
  418. {
  419. char* ret = calloc(MAX_STRING, sizeof(char));
  420. char* hold = copy_string(ret, s);
  421. hold[0] = a;
  422. return ret;
  423. }
  424. char* prepend_char(char a, char* s)
  425. {
  426. char* ret = calloc(MAX_STRING, sizeof(char));
  427. ret[0] = a;
  428. copy_string((ret+1), s);
  429. return ret;
  430. }
  431. char* prepend_string(char* add, char* base)
  432. {
  433. char* ret = calloc(MAX_STRING, sizeof(char));
  434. copy_string(copy_string(ret, add), base);
  435. return ret;
  436. }
  437. int string_length(char* a)
  438. {
  439. int i = 0;
  440. while(0 != a[i]) i = i + 1;
  441. return i;
  442. }
  443. /* -*- c-file-style: "linux";indent-tabs-mode:t -*- */
  444. /* Copyright (C) 2016 Jeremiah Orians
  445. * Copyright (C) 2017 Jan Nieuwenhuizen <janneke@gnu.org>
  446. * This file is part of mescc-tools.
  447. *
  448. * mescc-tools is free software: you can redistribute it and/or modify
  449. * it under the terms of the GNU General Public License as published by
  450. * the Free Software Foundation, either version 3 of the License, or
  451. * (at your option) any later version.
  452. *
  453. * mescc-tools is distributed in the hope that it will be useful,
  454. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  455. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  456. * GNU General Public License for more details.
  457. *
  458. * You should have received a copy of the GNU General Public License
  459. * along with mescc-tools. If not, see <http://www.gnu.org/licenses/>.
  460. */
  461. #include <stdio.h>
  462. #include <stdlib.h>
  463. #include <string.h>
  464. #include <getopt.h>
  465. #define max_string 4096
  466. //CONSTANT max_string 4096
  467. #define MACRO 1
  468. //CONSTANT MACRO 1
  469. #define STR 2
  470. //CONSTANT STR 2
  471. #define TRUE 1
  472. //CONSTANT TRUE 1
  473. #define FALSE 0
  474. //CONSTANT FALSE 0
  475. void file_print(char* s, FILE* f);
  476. int match(char* a, char* b);
  477. int string_length(char* a);
  478. char* numerate_number(int a);
  479. int numerate_string(char *a);
  480. int hex2char(int c);
  481. FILE* source_file;
  482. FILE* destination_file;
  483. int BigEndian;
  484. int BigBitEndian;
  485. int ByteMode;
  486. int Architecture;
  487. struct Token
  488. {
  489. struct Token* next;
  490. int type;
  491. char* Text;
  492. char* Expression;
  493. };
  494. struct Token* newToken()
  495. {
  496. struct Token* p;
  497. p = calloc (1, sizeof (struct Token));
  498. if (NULL == p)
  499. {
  500. file_print("calloc failed.\n", stderr);
  501. exit (EXIT_FAILURE);
  502. }
  503. return p;
  504. }
  505. struct Token* reverse_list(struct Token* head)
  506. {
  507. struct Token* root = NULL;
  508. while(NULL != head)
  509. {
  510. struct Token* next = head->next;
  511. head->next = root;
  512. root = head;
  513. head = next;
  514. }
  515. return root;
  516. }
  517. void purge_lineComment()
  518. {
  519. int c = fgetc(source_file);
  520. while((10 != c) && (13 != c))
  521. {
  522. c = fgetc(source_file);
  523. }
  524. }
  525. char* store_atom(char c)
  526. {
  527. char* store = calloc(max_string + 1, sizeof(char));
  528. if(NULL == store)
  529. {
  530. file_print("Exhusted available memory\n", stderr);
  531. exit(EXIT_FAILURE);
  532. }
  533. int ch = c;
  534. int i = 0;
  535. do
  536. {
  537. store[i] = ch;
  538. ch = fgetc(source_file);
  539. i = i + 1;
  540. } while ((9 != ch) && (10 != ch) && (32 != ch) && (i <= max_string));
  541. return store;
  542. }
  543. char* store_string(char c)
  544. {
  545. char* store = calloc(max_string + 1, sizeof(char));
  546. if(NULL == store)
  547. {
  548. file_print("Exhusted available memory\n", stderr);
  549. exit(EXIT_FAILURE);
  550. }
  551. int ch = c;
  552. int i = 0;
  553. do
  554. {
  555. store[i] = ch;
  556. i = i + 1;
  557. ch = fgetc(source_file);
  558. if(-1 == ch)
  559. {
  560. file_print("Unmatched \"!\n", stderr);
  561. exit(EXIT_FAILURE);
  562. }
  563. if(max_string == i)
  564. {
  565. file_print("String: ", stderr);
  566. file_print(store, stderr);
  567. file_print(" exceeds max string size\n", stderr);
  568. exit(EXIT_FAILURE);
  569. }
  570. } while(ch != c);
  571. return store;
  572. }
  573. struct Token* Tokenize_Line(struct Token* head)
  574. {
  575. int c;
  576. struct Token* p;
  577. do
  578. {
  579. restart:
  580. c = fgetc(source_file);
  581. if((35 == c) || (59 == c))
  582. {
  583. purge_lineComment();
  584. goto restart;
  585. }
  586. if((9 == c) || (10 == c) || (32 == c))
  587. {
  588. goto restart;
  589. }
  590. if(-1 == c)
  591. {
  592. goto done;
  593. }
  594. p = newToken();
  595. if((34 == c) || (39 == c))
  596. {
  597. p->Text = store_string(c);
  598. p->type = STR;
  599. }
  600. else
  601. {
  602. p->Text = store_atom(c);
  603. }
  604. p->next = head;
  605. head = p;
  606. } while(TRUE);
  607. done:
  608. return head;
  609. }
  610. void setExpression(struct Token* p, char *c, char *Exp)
  611. {
  612. struct Token* i;
  613. for(i = p; NULL != i; i = i->next)
  614. {
  615. /* Leave macros alone */
  616. if((i->type & MACRO))
  617. {
  618. continue;
  619. }
  620. else if(match(i->Text, c))
  621. { /* Only if there is an exact match replace */
  622. i->Expression = Exp;
  623. }
  624. }
  625. }
  626. void identify_macros(struct Token* p)
  627. {
  628. struct Token* i;
  629. for(i = p; NULL != i; i = i->next)
  630. {
  631. if(match(i->Text, "DEFINE"))
  632. {
  633. i->type = MACRO;
  634. i->Text = i->next->Text;
  635. if(i->next->next->type & STR)
  636. {
  637. i->Expression = i->next->next->Text + 1;
  638. }
  639. else
  640. {
  641. i->Expression = i->next->next->Text;
  642. }
  643. i->next = i->next->next->next;
  644. }
  645. }
  646. }
  647. void line_macro(struct Token* p)
  648. {
  649. struct Token* i;
  650. for(i = p; NULL != i; i = i->next)
  651. {
  652. if(i->type & MACRO)
  653. {
  654. setExpression(i->next, i->Text, i->Expression);
  655. }
  656. }
  657. }
  658. void hexify_string(struct Token* p)
  659. {
  660. char* table = "0123456789ABCDEF";
  661. int i = ((string_length(p->Text + 1)/4) + 1) * 8;
  662. char* d = calloc(max_string, sizeof(char));
  663. p->Expression = d;
  664. while(0 < i)
  665. {
  666. i = i - 1;
  667. d[i] = 0x30;
  668. }
  669. while( i < max_string)
  670. {
  671. if(0 == p->Text[i+1])
  672. {
  673. i = max_string;
  674. }
  675. else
  676. {
  677. d[2*i] = table[p->Text[i+1] / 16];
  678. d[2*i + 1] = table[p->Text[i+1] % 16];
  679. i = i + 1;
  680. }
  681. }
  682. }
  683. void process_string(struct Token* p)
  684. {
  685. struct Token* i;
  686. for(i = p; NULL != i; i = i->next)
  687. {
  688. if(i->type & STR)
  689. {
  690. if('\'' == i->Text[0])
  691. {
  692. i->Expression = i->Text + 1;
  693. }
  694. else if('"' == i->Text[0])
  695. {
  696. hexify_string(i);
  697. }
  698. }
  699. }
  700. }
  701. void preserve_other(struct Token* p)
  702. {
  703. struct Token* i;
  704. for(i = p; NULL != i; i = i->next)
  705. {
  706. if((NULL == i->Expression) && !(i->type & MACRO))
  707. {
  708. char c = i->Text[0];
  709. if(('!' == c) ||('@' == c) ||('$' == c) ||('%' == c) ||('&' == c) ||(':' == c))
  710. {
  711. i->Expression = i->Text;
  712. }
  713. else
  714. {
  715. file_print("Received invalid other; ", stderr);
  716. file_print(i->Text, stderr);
  717. file_print("\n", stderr);
  718. exit(EXIT_FAILURE);
  719. }
  720. }
  721. }
  722. }
  723. void bound_values(int displacement, int number_of_bytes, int low, int high)
  724. {
  725. if((high < displacement) || (displacement < low))
  726. {
  727. file_print("A displacement of ", stderr);
  728. file_print(numerate_number(displacement), stderr);
  729. file_print(" does not fit in ", stderr);
  730. file_print(numerate_number(number_of_bytes), stderr);
  731. file_print(" bytes\n", stderr);
  732. exit(EXIT_FAILURE);
  733. }
  734. }
  735. void range_check(int displacement, int number_of_bytes)
  736. {
  737. if(4 == number_of_bytes) return;
  738. else if(3 == number_of_bytes)
  739. {
  740. bound_values(displacement, number_of_bytes, -8388608, 16777216);
  741. return;
  742. }
  743. else if(2 == number_of_bytes)
  744. {
  745. bound_values(displacement, number_of_bytes, -32768, 65535);
  746. return;
  747. }
  748. else if(1 == number_of_bytes)
  749. {
  750. bound_values(displacement, number_of_bytes, -128, 255);
  751. return;
  752. }
  753. file_print("Received an invalid number of bytes in range_check\n", stderr);
  754. exit(EXIT_FAILURE);
  755. }
  756. void reverseBitOrder(char* c)
  757. {
  758. if(NULL == c) return;
  759. if(0 == c[1]) return;
  760. int hold = c[0];
  761. if(16 == ByteMode)
  762. {
  763. c[0] = c[1];
  764. c[1] = hold;
  765. reverseBitOrder(c+2);
  766. }
  767. else if(8 == ByteMode)
  768. {
  769. c[0] = c[2];
  770. c[2] = hold;
  771. reverseBitOrder(c+3);
  772. }
  773. else if(2 == ByteMode)
  774. {
  775. c[0] = c[7];
  776. c[7] = hold;
  777. hold = c[1];
  778. c[1] = c[6];
  779. c[6] = hold;
  780. hold = c[2];
  781. c[2] = c[5];
  782. c[5] = hold;
  783. hold = c[3];
  784. c[3] = c[4];
  785. c[4] = hold;
  786. reverseBitOrder(c+8);
  787. }
  788. }
  789. void LittleEndian(char* start)
  790. {
  791. char* end = start;
  792. char* c = start;
  793. while(0 != end[0]) end = end + 1;
  794. int hold;
  795. for(end = end - 1; start < end; start = start + 1)
  796. {
  797. hold = start[0];
  798. start[0] = end[0];
  799. end[0] = hold;
  800. end = end - 1;
  801. }
  802. if(BigBitEndian) reverseBitOrder(c);
  803. }
  804. int stringify(char* s, int digits, int divisor, int value, int shift)
  805. {
  806. int i = value;
  807. if(digits > 1)
  808. {
  809. i = stringify(s+1, (digits - 1), divisor, value, shift);
  810. }
  811. s[0] = hex2char(i & (divisor - 1));
  812. return (i >> shift);
  813. }
  814. char* express_number(int value, char c)
  815. {
  816. char* ch = calloc(42, sizeof(char));
  817. int size;
  818. int number_of_bytes;
  819. int shift;
  820. if('!' == c)
  821. {
  822. number_of_bytes = 1;
  823. value = value & 0xFF;
  824. }
  825. else if('@' == c)
  826. {
  827. number_of_bytes = 2;
  828. value = value & 0xFFFF;
  829. }
  830. else if('%' == c)
  831. {
  832. number_of_bytes = 4;
  833. value = value & 0xFFFFFFFF;
  834. }
  835. else
  836. {
  837. file_print("Given symbol ", stderr);
  838. fputc(c, stderr);
  839. file_print(" to express immediate value ", stderr);
  840. file_print(numerate_number(value), stderr);
  841. fputc('\n', stderr);
  842. exit(EXIT_FAILURE);
  843. }
  844. range_check(value, number_of_bytes);
  845. if(16 == ByteMode)
  846. {
  847. size = number_of_bytes * 2;
  848. shift = 4;
  849. }
  850. else if(8 == ByteMode)
  851. {
  852. size = number_of_bytes * 3;
  853. shift = 3;
  854. }
  855. else if(2 == ByteMode)
  856. {
  857. size = number_of_bytes * 8;
  858. shift = 1;
  859. }
  860. else
  861. {
  862. file_print("Got invalid ByteMode in express_number\n", stderr);
  863. exit(EXIT_FAILURE);
  864. }
  865. stringify(ch, size, ByteMode, value, shift);
  866. if(!BigEndian) LittleEndian(ch);
  867. else if(!BigBitEndian) reverseBitOrder(ch);
  868. return ch;
  869. }
  870. void eval_immediates(struct Token* p)
  871. {
  872. struct Token* i;
  873. for(i = p; NULL != i; i = i->next)
  874. {
  875. if((NULL == i->Expression) && !(i->type & MACRO))
  876. {
  877. int value;
  878. if((1 == Architecture) || (2 == Architecture) || (40 == Architecture))
  879. {
  880. value = numerate_string(i->Text + 1);
  881. if(('0' == i->Text[1]) || (0 != value))
  882. {
  883. i->Expression = express_number(value, i->Text[0]);
  884. }
  885. }
  886. else if(0 == Architecture)
  887. {
  888. value = numerate_string(i->Text);
  889. if(('0' == i->Text[0]) || (0 != value))
  890. {
  891. i->Expression = express_number(value, '@');
  892. }
  893. }
  894. else
  895. {
  896. file_print("Unknown architecture received in eval_immediates\n", stderr);
  897. exit(EXIT_FAILURE);
  898. }
  899. }
  900. }
  901. }
  902. void print_hex(struct Token* p)
  903. {
  904. struct Token* i;
  905. for(i = p; NULL != i; i = i->next)
  906. {
  907. if(i->type ^ MACRO)
  908. {
  909. fputc('\n', destination_file);
  910. file_print(i->Expression, destination_file);
  911. }
  912. }
  913. fputc('\n', destination_file);
  914. }
  915. /* Standard C main program */
  916. int main(int argc, char **argv)
  917. {
  918. BigEndian = TRUE;
  919. struct Token* head = NULL;
  920. Architecture = 0;
  921. destination_file = stdout;
  922. BigBitEndian = TRUE;
  923. ByteMode = 16;
  924. int option_index = 1;
  925. while(option_index <= argc)
  926. {
  927. if(NULL == argv[option_index])
  928. {
  929. option_index = option_index + 1;
  930. }
  931. else if(match(argv[option_index], "--BigEndian"))
  932. {
  933. BigEndian = TRUE;
  934. option_index = option_index + 1;
  935. }
  936. else if(match(argv[option_index], "--LittleEndian"))
  937. {
  938. BigEndian = FALSE;
  939. option_index = option_index + 1;
  940. }
  941. else if(match(argv[option_index], "-A") || match(argv[option_index], "--Architecture"))
  942. {
  943. Architecture = numerate_string(argv[option_index + 1]);
  944. option_index = option_index + 2;
  945. }
  946. else if(match(argv[option_index], "-b") || match(argv[option_index], "--binary"))
  947. {
  948. ByteMode = 2;
  949. option_index = option_index + 1;
  950. }
  951. else if(match(argv[option_index], "-h") || match(argv[option_index], "--help"))
  952. {
  953. file_print("Usage: ", stderr);
  954. file_print(argv[0], stderr);
  955. file_print(" -f FILENAME1 {-f FILENAME2} (--BigEndian|--LittleEndian) ", stderr);
  956. file_print("[--Architecture 12345]\nArchitecture 0: Knight; 1: x86; 2: AMD64; 40: armv7", stderr);
  957. exit(EXIT_SUCCESS);
  958. }
  959. else if(match(argv[option_index], "-f") || match(argv[option_index], "--file"))
  960. {
  961. source_file = fopen(argv[option_index + 1], "r");
  962. if(NULL == source_file)
  963. {
  964. file_print("The file: ", stderr);
  965. file_print(argv[option_index + 1], stderr);
  966. file_print(" can not be opened!\n", stderr);
  967. exit(EXIT_FAILURE);
  968. }
  969. head = Tokenize_Line(head);
  970. option_index = option_index + 2;
  971. }
  972. else if(match(argv[option_index], "-o") || match(argv[option_index], "--output"))
  973. {
  974. destination_file = fopen(argv[option_index + 1], "w");
  975. if(NULL == destination_file)
  976. {
  977. file_print("The file: ", stderr);
  978. file_print(argv[option_index + 1], stderr);
  979. file_print(" can not be opened!\n", stderr);
  980. exit(EXIT_FAILURE);
  981. }
  982. option_index = option_index + 2;
  983. }
  984. else if(match(argv[option_index], "-O") || match(argv[option_index], "--octal"))
  985. {
  986. ByteMode = 8;
  987. option_index = option_index + 1;
  988. }
  989. else if(match(argv[option_index], "-V") || match(argv[option_index], "--version"))
  990. {
  991. file_print("M1 0.3\n", stdout);
  992. exit(EXIT_SUCCESS);
  993. }
  994. else
  995. {
  996. file_print("Unknown option\n", stderr);
  997. exit(EXIT_FAILURE);
  998. }
  999. }
  1000. if(NULL == head)
  1001. {
  1002. file_print("Either no input files were given or they were empty\n", stderr);
  1003. exit(EXIT_FAILURE);
  1004. }
  1005. head = reverse_list(head);
  1006. identify_macros(head);
  1007. line_macro(head);
  1008. process_string(head);
  1009. eval_immediates(head);
  1010. preserve_other(head);
  1011. print_hex(head);
  1012. return EXIT_SUCCESS;
  1013. }