staging.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /* This file is part of asmc, a bootstrapping OS with minimal seed
  2. Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. https://gitlab.com/giomasce/asmc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include "platform.h"
  15. #define INPUT_BUF_LEN 1024
  16. #define MAX_SYMBOL_NAME_LEN 128
  17. #define SYMBOL_TABLE_LEN 1024
  18. typedef void (*opcode_func)(int, char*);
  19. char *get_input_buf();
  20. char *get_symbol_names();
  21. int *get_symbol_num();
  22. int *get_symbol_locs();
  23. int *get_symbol_arities();
  24. int *get_current_loc();
  25. int *get_stage();
  26. int *get_emit_fd();
  27. char *get_opcode_names();
  28. opcode_func *get_opcode_funcs();
  29. int *get_rm32_opcode();
  30. int *get_imm32_opcode();
  31. int *get_rm8r8_opcode();
  32. int *get_rm32r32_opcode();
  33. int *get_r8rm8_opcode();
  34. int *get_r32rm32_opcode();
  35. int *get_rm8imm8_opcode();
  36. int *get_rm32imm32_opcode();
  37. int line;
  38. void assert(int cond);
  39. void assert2(int cond) {
  40. if (!cond) {
  41. platform_panic();
  42. }
  43. }
  44. int readline(int fd, unsigned char *buf, int len);
  45. int readline2(int fd, unsigned char *buf, int len) {
  46. while (len > 0) {
  47. int c = platform_read_char(fd);
  48. if (c == '\n' || c == -1) {
  49. *buf = '\0';
  50. return c == -1;
  51. } else {
  52. *buf = (unsigned char) c;
  53. }
  54. buf++;
  55. len--;
  56. }
  57. platform_panic();
  58. }
  59. void trimstr(char *buf);
  60. void trimstr2(char *buf) {
  61. char *write_buf = buf;
  62. char *read_buf = buf;
  63. while (*read_buf == ' ' || *read_buf == '\t') {
  64. read_buf++;
  65. }
  66. while (*read_buf != '\0') {
  67. *write_buf = *read_buf;
  68. write_buf++;
  69. read_buf++;
  70. }
  71. *write_buf = '\0';
  72. write_buf--;
  73. while (write_buf >= buf && (*write_buf == ' ' || *write_buf == '\t')) {
  74. *write_buf = '\0';
  75. write_buf--;
  76. }
  77. }
  78. void remove_spaces(char *buf);
  79. void remove_spaces2(char *buf) {
  80. char *read_buf = buf;
  81. while (1) {
  82. if (*read_buf == '\0') {
  83. *buf = '\0';
  84. return;
  85. }
  86. if (*read_buf == ' ' || *read_buf == '\t') {
  87. read_buf++;
  88. } else {
  89. *buf = *read_buf;
  90. buf++;
  91. read_buf++;
  92. }
  93. }
  94. }
  95. int strcmp(const char *s1, const char *s2);
  96. int strcmp2(const char *s1, const char *s2) {
  97. while (1) {
  98. if (*s1 < *s2) {
  99. return -1;
  100. }
  101. if (*s1 > *s2) {
  102. return 1;
  103. }
  104. if (*s1 == '\0') {
  105. return 0;
  106. }
  107. s1++;
  108. s2++;
  109. }
  110. }
  111. int isstrpref(const char *s1, const char *s2);
  112. int isstrpref2(const char *s1, const char *s2) {
  113. while (1) {
  114. if (*s1 == '\0') {
  115. return 1;
  116. }
  117. if (*s1 != *s2) {
  118. return 0;
  119. }
  120. s1++;
  121. s2++;
  122. }
  123. }
  124. void strcpy(char *d, const char *s);
  125. void strcpy2(char *d, const char *s) {
  126. while (1) {
  127. *d = *s;
  128. if (*s == '\0') {
  129. return;
  130. }
  131. d++;
  132. s++;
  133. }
  134. }
  135. int strlen(const char *s);
  136. int strlen2(const char *s) {
  137. const char *s2 = s;
  138. while (*s2 != '\0') {
  139. s2++;
  140. }
  141. return s2 - s;
  142. }
  143. int find_char(char *s, char c);
  144. int find_char2(char *s, char c) {
  145. char *s2 = s;
  146. while (1) {
  147. if (*s2 == c) {
  148. return s2 - s;
  149. }
  150. if (*s2 == '\0') {
  151. return -1;
  152. }
  153. s2++;
  154. }
  155. }
  156. int get_symbol_idx(const char *name);
  157. int get_symbol_idx2(const char *name) {
  158. int i = 0;
  159. for (i = 0; i < *get_symbol_num(); i++) {
  160. if (strcmp(name, get_symbol_names() + i * MAX_SYMBOL_NAME_LEN) == 0) {
  161. break;
  162. }
  163. }
  164. return i;
  165. }
  166. int find_symbol(const char *name, int *loc, int *arity);
  167. int find_symbol2(const char *name, int *loc, int *arity) {
  168. int i = get_symbol_idx(name);
  169. if (i == *get_symbol_num()) {
  170. return 0;
  171. } else {
  172. if (loc != 0) {
  173. *loc = get_symbol_locs()[i];
  174. }
  175. if (arity != 0) {
  176. *arity = get_symbol_arities()[i];
  177. }
  178. return 1;
  179. }
  180. }
  181. void add_symbol(const char *name, int loc, int arity);
  182. void add_symbol2(const char *name, int loc, int arity) {
  183. int len = strlen(name);
  184. assert(len > 0);
  185. assert(len < MAX_SYMBOL_NAME_LEN);
  186. int symbol_num = *get_symbol_num();
  187. assert(!find_symbol(name, 0, 0));
  188. assert(symbol_num < SYMBOL_TABLE_LEN);
  189. get_symbol_locs()[symbol_num] = loc;
  190. get_symbol_arities()[symbol_num] = arity;
  191. strcpy(get_symbol_names() + symbol_num * MAX_SYMBOL_NAME_LEN, name);
  192. *get_symbol_num() = symbol_num + 1;
  193. }
  194. void add_symbol_wrapper(const char *name, int loc, int arity);
  195. void add_symbol_wrapper2(const char *name, int loc, int arity) {
  196. int stage = *get_stage();
  197. if (stage == 0) {
  198. add_symbol(name, loc, arity);
  199. } else if (stage == 1) {
  200. int loc2;
  201. int arity2;
  202. int res = find_symbol(name, &loc2, &arity2);
  203. assert(res);
  204. assert(loc == loc2);
  205. assert(arity == arity2);
  206. } else {
  207. platform_panic();
  208. }
  209. }
  210. void add_symbol_placeholder(const char *name, int arity);
  211. void add_symbol_placeholder2(const char *name, int arity) {
  212. int stage = *get_stage();
  213. int arity2;
  214. int res = find_symbol(name, 0, &arity2);
  215. if (stage != 0) {
  216. assert(res);
  217. }
  218. if (!res) {
  219. add_symbol(name, 0xffffffff, arity);
  220. } else {
  221. assert(arity == arity2);
  222. }
  223. }
  224. void fix_symbol_placeholder(const char *name, int loc, int arity);
  225. void fix_symbol_placeholder2(const char *name, int loc, int arity) {
  226. int stage = *get_stage();
  227. int loc2;
  228. int arity2;
  229. int res = find_symbol(name, &loc2, &arity2);
  230. if (stage != 0) {
  231. assert(res);
  232. }
  233. if (!res) {
  234. add_symbol(name, loc, arity);
  235. } else {
  236. assert(arity == arity2);
  237. assert(loc == loc2 || (stage == 0 && loc2 == 0xffffffff));
  238. int idx = get_symbol_idx(name);
  239. assert(idx != *get_symbol_num());
  240. get_symbol_locs()[idx] = loc;
  241. }
  242. }
  243. int decode_reg32(char *reg);
  244. int decode_reg322(char *reg) {
  245. if (strcmp(reg, "eax") == 0) {
  246. return 0;
  247. } else if (strcmp(reg, "ecx") == 0) {
  248. return 1;
  249. } else if (strcmp(reg, "edx") == 0) {
  250. return 2;
  251. } else if (strcmp(reg, "ebx") == 0) {
  252. return 3;
  253. } else if (strcmp(reg, "esp") == 0) {
  254. return 4;
  255. } else if (strcmp(reg, "ebp") == 0) {
  256. return 5;
  257. } else if (strcmp(reg, "esi") == 0) {
  258. return 6;
  259. } else if (strcmp(reg, "edi") == 0) {
  260. return 7;
  261. } else {
  262. return -1;
  263. }
  264. }
  265. int decode_reg8(char *reg);
  266. int decode_reg82(char *reg) {
  267. if (strcmp(reg, "al") == 0) {
  268. return 0;
  269. } else if (strcmp(reg, "cl") == 0) {
  270. return 1;
  271. } else if (strcmp(reg, "dl") == 0) {
  272. return 2;
  273. } else if (strcmp(reg, "bl") == 0) {
  274. return 3;
  275. } else if (strcmp(reg, "ah") == 0) {
  276. return 4;
  277. } else if (strcmp(reg, "ch") == 0) {
  278. return 5;
  279. } else if (strcmp(reg, "dh") == 0) {
  280. return 6;
  281. } else if (strcmp(reg, "bh") == 0) {
  282. return 7;
  283. } else {
  284. return -1;
  285. }
  286. }
  287. int decode_number(const char *operand, unsigned int *num);
  288. int decode_number2(const char *operand, unsigned int *num) {
  289. *num = 0;
  290. int is_decimal = 1;
  291. int digit_seen = 0;
  292. if (operand[0] == '0' && operand[1] == 'x') {
  293. operand += 2;
  294. is_decimal = 0;
  295. }
  296. while (1) {
  297. if (operand[0] == '\0') {
  298. if (digit_seen) {
  299. return 1;
  300. } else {
  301. return 0;
  302. }
  303. }
  304. digit_seen = 1;
  305. if (is_decimal) {
  306. *num *= 10;
  307. } else {
  308. *num *= 16;
  309. }
  310. if ('0' <= operand[0] && operand[0] <= '9') {
  311. *num += operand[0] - '0';
  312. } else if (!is_decimal && 'a' <= operand[0] && operand[0] <= 'f') {
  313. *num += operand[0] - 'a' + 10;
  314. } else {
  315. return 0;
  316. }
  317. operand++;
  318. }
  319. }
  320. int decode_number_or_symbol(const char *operand, unsigned int *num, int force_symbol);
  321. int decode_number_or_symbol2(const char *operand, unsigned int *num, int force_symbol) {
  322. int res = decode_number(operand, num);
  323. if (res) {
  324. return 1;
  325. }
  326. int stage = *get_stage();
  327. if (stage == 1 || force_symbol) {
  328. return find_symbol(operand, num, 0);
  329. } else if (stage == 0) {
  330. *num = 0;
  331. return 1;
  332. } else {
  333. platform_panic();
  334. }
  335. }
  336. int decode_operand(char *operand, int *is_direct, int *reg, int *disp, int *is8, int *is32);
  337. int decode_operand2(char *operand, int *is_direct, int *reg, int *disp, int *is8, int *is32) {
  338. remove_spaces(operand);
  339. *is8 = 0;
  340. *is32 = 0;
  341. if (isstrpref("BYTE", operand)) {
  342. operand += 4;
  343. *is8 = 1;
  344. }
  345. if (isstrpref("DWORD", operand)) {
  346. operand += 5;
  347. *is32 = 1;
  348. }
  349. assert(!*is8 || !*is32);
  350. if (operand[0] == '[') {
  351. *is_direct = 0;
  352. operand++;
  353. int plus_pos = find_char(operand, '+');
  354. if (plus_pos == -1) {
  355. *disp = 0;
  356. int closed_pos = find_char(operand, ']');
  357. if (closed_pos == -1) {
  358. return 0;
  359. } else {
  360. if (operand[closed_pos+1] != '\0') {
  361. return 0;
  362. } else {
  363. operand[closed_pos] = '\0';
  364. *reg = decode_reg32(operand);
  365. return *reg != -1;
  366. }
  367. }
  368. } else {
  369. operand[plus_pos] = '\0';
  370. *reg = decode_reg32(operand);
  371. if (*reg == -1) {
  372. return 0;
  373. } else {
  374. operand = operand + plus_pos + 1;
  375. int closed_pos = find_char(operand, ']');
  376. if (closed_pos == -1) {
  377. return 0;
  378. } else {
  379. if (operand[closed_pos+1] != '\0') {
  380. return 0;
  381. } else {
  382. operand[closed_pos] = '\0';
  383. return decode_number_or_symbol(operand, disp, 0);
  384. }
  385. }
  386. }
  387. }
  388. } else {
  389. *is_direct = 1;
  390. if (*is32 || *is8) {
  391. return 0;
  392. }
  393. *reg = decode_reg32(operand);
  394. if (*reg != -1) {
  395. *is32 = 1;
  396. assert(!*is8);
  397. return 1;
  398. } else {
  399. *reg = decode_reg8(operand);
  400. if (*reg != -1) {
  401. *is8 = 1;
  402. assert(!*is32);
  403. return 1;
  404. } else {
  405. return 0;
  406. }
  407. }
  408. }
  409. }
  410. void emit(char c);
  411. void emit2(char c) {
  412. int stage = *get_stage();
  413. if (stage == 1) {
  414. platform_write_char(*get_emit_fd(), c);
  415. }
  416. (*get_current_loc())++;
  417. }
  418. void emit32(int x);
  419. void emit322(int x) {
  420. emit(x);
  421. emit(x >> 8);
  422. emit(x >> 16);
  423. emit(x >> 24);
  424. }
  425. int process_bss_line(char *opcode, char *data);
  426. int process_bss_line2(char *opcode, char *data) {
  427. if (strcmp(opcode, "resb") == 0) {
  428. int val;
  429. int res = decode_number_or_symbol(data, &val, 1);
  430. if (!res) {
  431. platform_panic();
  432. }
  433. int i;
  434. for (i = 0; i < val; i++) {
  435. emit(0);
  436. }
  437. } else if (strcmp(opcode, "resd") == 0) {
  438. int val;
  439. int res = decode_number_or_symbol(data, &val, 1);
  440. if (!res) {
  441. platform_panic();
  442. }
  443. int i;
  444. for (i = 0; i < val; i++) {
  445. emit32(0);
  446. }
  447. } else {
  448. return 0;
  449. }
  450. return 1;
  451. }
  452. int process_data_line(char *opcode, char *data);
  453. int process_data_line2(char *opcode, char *data) {
  454. if (strcmp(opcode, "db") == 0) {
  455. if (data[0] == '\'') {
  456. int len = strlen(data);
  457. assert(len >= 2);
  458. assert(data[len-1] == '\'');
  459. data[len-1] = '\0';
  460. data++;
  461. for ( ; *data != '\0'; data++) {
  462. emit(*data);
  463. }
  464. } else {
  465. int val;
  466. int res = decode_number_or_symbol(data, &val, 0);
  467. if (!res) {
  468. platform_panic();
  469. }
  470. emit(val);
  471. }
  472. } else if (strcmp(opcode, "dd") == 0) {
  473. int val;
  474. int res = decode_number_or_symbol(data, &val, 0);
  475. if (!res) {
  476. platform_panic();
  477. }
  478. emit32(val);
  479. } else {
  480. return 0;
  481. }
  482. return 1;
  483. }
  484. int emit_modrm(int mod, int reg, int rm);
  485. int emit_modrm2(int mod, int reg, int rm) {
  486. assert(mod == mod & 0x3);
  487. assert(reg == reg & 0x7);
  488. assert(rm == rm & 0x7);
  489. // The only two supported mode are a direct register, or an indirect
  490. // register + disp32
  491. assert(mod == 2 || mod == 3);
  492. emit((mod << 6) + (reg << 3) + rm);
  493. // In the particular case of ESP used as indirect base, a SIB is
  494. // needed
  495. if (mod == 2 && rm == 4) {
  496. emit(0x24);
  497. }
  498. }
  499. enum {
  500. OP_PUSH = 0,
  501. OP_POP,
  502. OP_ADD,
  503. OP_SUB,
  504. OP_MOV,
  505. OP_CMP,
  506. OP_AND,
  507. OP_OR,
  508. OP_JMP,
  509. OP_CALL,
  510. OP_JE,
  511. OP_JNE,
  512. OP_JA,
  513. OP_JNA,
  514. OP_JAE,
  515. OP_JNAE,
  516. OP_JB,
  517. OP_JNB,
  518. OP_JBE,
  519. OP_JNBE,
  520. OP_JG,
  521. OP_JNG,
  522. OP_JGE,
  523. OP_JNGE,
  524. OP_JL,
  525. OP_JNL,
  526. OP_JLE,
  527. OP_JNLE,
  528. OP_MUL,
  529. OP_IMUL,
  530. OP_INT,
  531. OP_RET,
  532. OP_IN,
  533. OP_OUT,
  534. };
  535. void emit_helper(int opcode_data, int is_direct, int reg, int rm, int disp);
  536. void emit_helper2(int opcode_data, int is_direct, int reg, int rm, int disp) {
  537. int opcode = opcode_data & 0xff;
  538. int opcode2 = (opcode_data >> 8) & 0xff;
  539. int has_opcode2 = opcode_data & 0xff0000;
  540. if (reg == -1) {
  541. reg = opcode2;
  542. }
  543. int mod;
  544. if (is_direct) {
  545. mod = 3;
  546. } else {
  547. mod = 2;
  548. }
  549. int has_modrm = (rm != -1);
  550. assert(opcode != 0xf0);
  551. emit(opcode);
  552. if (has_opcode2) {
  553. emit(opcode2);
  554. }
  555. if (has_modrm) {
  556. emit_modrm(mod, reg, rm);
  557. }
  558. if (!is_direct) {
  559. emit32(disp);
  560. }
  561. }
  562. void process_jmp_like(int op, char *data);
  563. void process_jmp_like2(int op, char *data) {
  564. int is_direct, reg, disp, is8, is32;
  565. int res = decode_operand(data, &is_direct, &reg, &disp, &is8, &is32);
  566. if (res) {
  567. assert(!is8);
  568. // r/m32
  569. int opcode_data = get_rm32_opcode()[op];
  570. emit_helper(opcode_data, is_direct, -1, reg, disp);
  571. } else {
  572. // rel32
  573. int opcode_data = get_imm32_opcode()[op];
  574. emit_helper(opcode_data, 1, -1, -1, 0);
  575. int rel;
  576. int res = decode_number_or_symbol(data, &rel, 0);
  577. if (!res) {
  578. platform_panic();
  579. }
  580. int current_loc = *get_current_loc();
  581. rel = rel - current_loc - 4;
  582. emit32(rel);
  583. }
  584. }
  585. void process_push_like(int op, char *data);
  586. void process_push_like2(int op, char *data) {
  587. int is_direct, reg, disp, is8, is32;
  588. int res = decode_operand(data, &is_direct, &reg, &disp, &is8, &is32);
  589. if (res) {
  590. assert(!is8);
  591. // r/m32
  592. int opcode_data = get_rm32_opcode()[op];
  593. emit_helper(opcode_data, is_direct, -1, reg, disp);
  594. } else {
  595. assert(op == OP_PUSH);
  596. int imm;
  597. int res = decode_number_or_symbol(data, &imm, 0);
  598. if (res) {
  599. emit(0x68);
  600. emit32(imm);
  601. } else {
  602. platform_panic();
  603. }
  604. }
  605. }
  606. void process_add_like(int op, char *data);
  607. void process_add_like2(int op, char *data) {
  608. int comma_pos = find_char(data, ',');
  609. if (comma_pos == -1) {
  610. platform_panic();
  611. }
  612. data[comma_pos] = '\0';
  613. char *dest = data;
  614. char *src = data + comma_pos + 1;
  615. int dest_is_direct, dest_reg, dest_disp, dest_is8, dest_is32;
  616. int src_is_direct, src_reg, src_disp, src_is8, src_is32;
  617. int dest_res = decode_operand(dest, &dest_is_direct, &dest_reg, &dest_disp, &dest_is8, &dest_is32);
  618. if (!dest_res) {
  619. platform_panic();
  620. }
  621. int src_res = decode_operand(src, &src_is_direct, &src_reg, &src_disp, &src_is8, &src_is32);
  622. if (src_res) {
  623. // First we decide whether this is an 8 or 32 bits operation
  624. int is8 = dest_is8 || src_is8;
  625. int is32 = dest_is32 || src_is32;
  626. assert(is8 || is32);
  627. assert(!is8 || !is32);
  628. if (dest_is_direct) {
  629. if (is8) {
  630. // r8, r/m8
  631. int opcode_data = get_r8rm8_opcode()[op];
  632. emit_helper(opcode_data, src_is_direct, dest_reg, src_reg, src_disp);
  633. } else {
  634. // r32, r/m32
  635. int opcode_data = get_r32rm32_opcode()[op];
  636. emit_helper(opcode_data, src_is_direct, dest_reg, src_reg, src_disp);
  637. }
  638. } else {
  639. if (src_is_direct) {
  640. if (is8) {
  641. // r/m8, r8
  642. int opcode_data = get_rm8r8_opcode()[op];
  643. emit_helper(opcode_data, 0, src_reg, dest_reg, dest_disp);
  644. } else {
  645. // r/m32, r32
  646. int opcode_data = get_rm32r32_opcode()[op];
  647. emit_helper(opcode_data, 0, src_reg, dest_reg, dest_disp);
  648. }
  649. } else {
  650. platform_panic();
  651. }
  652. }
  653. } else {
  654. assert(dest_is8 || dest_is32);
  655. int imm;
  656. int res = decode_number_or_symbol(src, &imm, 0);
  657. if (res) {
  658. if (dest_is8) {
  659. // r/m8, imm8
  660. int opcode_data = get_rm8imm8_opcode()[op];
  661. emit_helper(opcode_data, dest_is_direct, -1, dest_reg, dest_disp);
  662. emit(imm);
  663. } else {
  664. // r/m32, imm32
  665. int opcode_data = get_rm32imm32_opcode()[op];
  666. emit_helper(opcode_data, dest_is_direct, -1, dest_reg, dest_disp);
  667. emit32(imm);
  668. }
  669. } else {
  670. platform_panic();
  671. }
  672. }
  673. }
  674. void process_int(int op, char *data);
  675. void process_int2(int op, char *data) {
  676. assert(op == OP_INT);
  677. int imm;
  678. int res = decode_number_or_symbol(data, &imm, 0);
  679. if (!res) {
  680. platform_panic();
  681. }
  682. if (res < 0 || res >= 0x100) {
  683. platform_panic();
  684. }
  685. emit(0xcd);
  686. emit(imm);
  687. }
  688. void process_ret(int op, char *data);
  689. void process_ret2(int op, char *data) {
  690. assert(op == OP_RET);
  691. assert(data[0] == '\0');
  692. emit(0xc3);
  693. }
  694. void process_in_like(int op, char *data);
  695. void process_in_like2(int op, char *data) {
  696. assert(op == OP_IN || op == OP_OUT);
  697. int comma_pos = find_char(data, ',');
  698. if (comma_pos == -1) {
  699. platform_panic();
  700. }
  701. data[comma_pos] = '\0';
  702. int opcode;
  703. char *port;
  704. char *reg;
  705. if (op == OP_IN) {
  706. opcode = 0xec;
  707. port = data + comma_pos + 1;
  708. reg = data;
  709. } else {
  710. opcode = 0xee;
  711. port = data;
  712. reg = data + comma_pos + 1;
  713. }
  714. trimstr(port);
  715. trimstr(reg);
  716. assert(strcmp(port, "dx") == 0);
  717. if (strcmp(reg, "al") == 0) {
  718. emit(opcode);
  719. } else if (strcmp(reg, "ax") == 0) {
  720. emit(0x66);
  721. emit(opcode+1);
  722. } else if (strcmp(reg, "eax") == 0) {
  723. emit(opcode+1);
  724. } else {
  725. platform_panic();
  726. }
  727. }
  728. int process_text_line(char *opcode, char *data);
  729. int process_text_line2(char *opcode, char *data) {
  730. char *names = get_opcode_names();
  731. int idx = 0;
  732. while (1) {
  733. if (*names == '\0') {
  734. return 0;
  735. }
  736. if (strcmp(names, opcode) == 0) {
  737. get_opcode_funcs()[idx](idx, data);
  738. return 1;
  739. }
  740. int len = strlen(names);
  741. names += len + 1;
  742. idx++;
  743. }
  744. }
  745. int process_directive_line(char *opcode, char *data);
  746. int process_directive_line2(char *opcode, char *data) {
  747. if (strcmp(opcode, "section") == 0) {
  748. } else if (strcmp(opcode, "global") == 0) {
  749. } else if (strcmp(opcode, "align") == 0) {
  750. int val;
  751. int res = decode_number_or_symbol(data, &val, 1);
  752. if (!res) {
  753. platform_panic();
  754. }
  755. int to_skip = val - (*get_current_loc() % val);
  756. int i;
  757. for (i = 0; i < to_skip; i++) {
  758. emit(0);
  759. }
  760. } else if (strcmp(opcode, "extern") == 0) {
  761. add_symbol_wrapper(data, 0, -1);
  762. } else {
  763. return 0;
  764. }
  765. return 1;
  766. }
  767. int process_equ_line(char *opcode, char *data);
  768. int process_equ_line2(char *opcode, char *data) {
  769. int data_space_pos = find_char(data, ' ');
  770. if (data_space_pos >= 0) {
  771. data[data_space_pos] = '\0';
  772. if (strcmp(data, "equ") == 0) {
  773. char *val_str = data + data_space_pos + 1;
  774. trimstr(val_str);
  775. int val;
  776. int res = decode_number_or_symbol(val_str, &val, 0);
  777. if (res) {
  778. add_symbol_wrapper(opcode, val, -1);
  779. } else {
  780. platform_panic();
  781. }
  782. } else {
  783. return 0;
  784. }
  785. } else {
  786. return 0;
  787. }
  788. return 1;
  789. }
  790. void process_line(char *line);
  791. void process_line2(char *line) {
  792. char *opcode = line;
  793. int opcode_len = find_char(line, ' ');
  794. char *data;
  795. int data_len;
  796. if (opcode_len == -1) {
  797. data = "";
  798. } else {
  799. opcode[opcode_len] = '\0';
  800. data = line + opcode_len + 1;
  801. trimstr(data);
  802. }
  803. int processed = 0;
  804. if (!processed) {
  805. processed = process_directive_line(opcode, data);
  806. }
  807. if (!processed) {
  808. processed = process_bss_line(opcode, data);
  809. }
  810. if (!processed) {
  811. processed = process_text_line(opcode, data);
  812. }
  813. if (!processed) {
  814. processed = process_data_line(opcode, data);
  815. }
  816. if (!processed) {
  817. processed = process_equ_line(opcode, data);
  818. }
  819. if (!processed) {
  820. platform_panic();
  821. }
  822. }
  823. void assemble(int fd_in, int fd_out, int start_loc);
  824. void assemble2(int fd_in, int fd_out, int start_loc) {
  825. *get_emit_fd() = fd_out;
  826. for (*get_stage() = 0; *get_stage() < 2; (*get_stage())++) {
  827. platform_reset_file(fd_in);
  828. line = 0;
  829. *get_current_loc() = start_loc;
  830. while (1) {
  831. char *input_buf = get_input_buf();
  832. int finished = readline(fd_in, input_buf, INPUT_BUF_LEN);
  833. if (1) {
  834. platform_log(2, "Decoding line: ");
  835. platform_log(2, input_buf);
  836. platform_log(2, "\n");
  837. }
  838. int semicolon_pos = find_char(input_buf, ';');
  839. if (semicolon_pos != -1) {
  840. input_buf[semicolon_pos] = '\0';
  841. }
  842. trimstr(input_buf);
  843. int len = strlen(input_buf);
  844. if (finished && len == 0) {
  845. break;
  846. }
  847. if (len == 0) {
  848. line++;
  849. continue;
  850. }
  851. if (input_buf[len-1] == ':') {
  852. input_buf[len-1] = '\0';
  853. add_symbol_wrapper(input_buf, *get_current_loc(), -1);
  854. } else {
  855. process_line(input_buf);
  856. }
  857. line++;
  858. }
  859. }
  860. }
  861. void init_assembler();
  862. void init_symbols();
  863. int main(int argc, char **argv) {
  864. init_symbols();
  865. init_assembler();
  866. int fd_in = platform_open_file(argv[1]);
  867. int fd_out = 1;
  868. assemble(fd_in, fd_out, 0x100000);
  869. return 0;
  870. }