tccasm.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. /*
  2. * GAS like assembler for TCC
  3. *
  4. * Copyright (c) 2001-2004 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "tcc.h"
  21. #ifdef CONFIG_TCC_ASM
  22. ST_FUNC int asm_get_local_label_name(TCCState *s1, unsigned int n)
  23. {
  24. char buf[64];
  25. TokenSym *ts;
  26. snprintf(buf, sizeof(buf), "L..%u", n);
  27. ts = tok_alloc(buf, strlen(buf));
  28. return ts->tok;
  29. }
  30. static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global);
  31. static Sym* asm_new_label(TCCState *s1, int label, int is_local);
  32. static Sym* asm_new_label1(TCCState *s1, int label, int is_local, int sh_num, int value);
  33. static Sym *asm_label_find(int v)
  34. {
  35. Sym *sym = sym_find(v);
  36. while (sym && sym->sym_scope)
  37. sym = sym->prev_tok;
  38. return sym;
  39. }
  40. static Sym *asm_label_push(int v)
  41. {
  42. /* We always add VT_EXTERN, for sym definition that's tentative
  43. (for .set, removed for real defs), for mere references it's correct
  44. as is. */
  45. Sym *sym = global_identifier_push(v, VT_ASM | VT_EXTERN | VT_STATIC, 0);
  46. sym->r = VT_CONST | VT_SYM;
  47. return sym;
  48. }
  49. /* Return a symbol we can use inside the assembler, having name NAME.
  50. Symbols from asm and C source share a namespace. If we generate
  51. an asm symbol it's also a (file-global) C symbol, but it's
  52. either not accessible by name (like "L.123"), or its type information
  53. is such that it's not usable without a proper C declaration.
  54. Sometimes we need symbols accessible by name from asm, which
  55. are anonymous in C, in this case CSYM can be used to transfer
  56. all information from that symbol to the (possibly newly created)
  57. asm symbol. */
  58. ST_FUNC Sym* get_asm_sym(int name, Sym *csym)
  59. {
  60. Sym *sym = asm_label_find(name);
  61. if (!sym) {
  62. sym = asm_label_push(name);
  63. if (csym)
  64. sym->c = csym->c;
  65. }
  66. return sym;
  67. }
  68. static Sym* asm_section_sym(TCCState *s1, Section *sec)
  69. {
  70. char buf[100];
  71. int label = tok_alloc(buf,
  72. snprintf(buf, sizeof buf, "L.%s", sec->name)
  73. )->tok;
  74. Sym *sym = asm_label_find(label);
  75. return sym ? sym : asm_new_label1(s1, label, 1, sec->sh_num, 0);
  76. }
  77. /* We do not use the C expression parser to handle symbols. Maybe the
  78. C expression parser could be tweaked to do so. */
  79. static void asm_expr_unary(TCCState *s1, ExprValue *pe)
  80. {
  81. Sym *sym;
  82. int op, label;
  83. uint64_t n;
  84. const char *p;
  85. switch(tok) {
  86. case TOK_PPNUM:
  87. p = tokc.str.data;
  88. n = strtoull(p, (char **)&p, 0);
  89. if (*p == 'b' || *p == 'f') {
  90. /* backward or forward label */
  91. label = asm_get_local_label_name(s1, n);
  92. sym = asm_label_find(label);
  93. if (*p == 'b') {
  94. /* backward : find the last corresponding defined label */
  95. if (sym && (!sym->c || elfsym(sym)->st_shndx == SHN_UNDEF))
  96. sym = sym->prev_tok;
  97. if (!sym)
  98. tcc_error("local label '%d' not found backward", n);
  99. } else {
  100. /* forward */
  101. if (!sym || (sym->c && elfsym(sym)->st_shndx != SHN_UNDEF)) {
  102. /* if the last label is defined, then define a new one */
  103. sym = asm_label_push(label);
  104. }
  105. }
  106. pe->v = 0;
  107. pe->sym = sym;
  108. pe->pcrel = 0;
  109. } else if (*p == '\0') {
  110. pe->v = n;
  111. pe->sym = NULL;
  112. pe->pcrel = 0;
  113. } else {
  114. tcc_error("invalid number syntax");
  115. }
  116. next();
  117. break;
  118. case '+':
  119. next();
  120. asm_expr_unary(s1, pe);
  121. break;
  122. case '-':
  123. case '~':
  124. op = tok;
  125. next();
  126. asm_expr_unary(s1, pe);
  127. if (pe->sym)
  128. tcc_error("invalid operation with label");
  129. if (op == '-')
  130. pe->v = -pe->v;
  131. else
  132. pe->v = ~pe->v;
  133. break;
  134. case TOK_CCHAR:
  135. case TOK_LCHAR:
  136. pe->v = tokc.i;
  137. pe->sym = NULL;
  138. pe->pcrel = 0;
  139. next();
  140. break;
  141. case '(':
  142. next();
  143. asm_expr(s1, pe);
  144. skip(')');
  145. break;
  146. case '.':
  147. pe->v = ind;
  148. pe->sym = asm_section_sym(s1, cur_text_section);
  149. pe->pcrel = 0;
  150. next();
  151. break;
  152. default:
  153. if (tok >= TOK_IDENT) {
  154. ElfSym *esym;
  155. /* label case : if the label was not found, add one */
  156. sym = get_asm_sym(tok, NULL);
  157. esym = elfsym(sym);
  158. if (esym && esym->st_shndx == SHN_ABS) {
  159. /* if absolute symbol, no need to put a symbol value */
  160. pe->v = esym->st_value;
  161. pe->sym = NULL;
  162. pe->pcrel = 0;
  163. } else {
  164. pe->v = 0;
  165. pe->sym = sym;
  166. pe->pcrel = 0;
  167. }
  168. next();
  169. } else {
  170. tcc_error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
  171. }
  172. break;
  173. }
  174. }
  175. static void asm_expr_prod(TCCState *s1, ExprValue *pe)
  176. {
  177. int op;
  178. ExprValue e2;
  179. asm_expr_unary(s1, pe);
  180. for(;;) {
  181. op = tok;
  182. if (op != '*' && op != '/' && op != '%' &&
  183. op != TOK_SHL && op != TOK_SAR)
  184. break;
  185. next();
  186. asm_expr_unary(s1, &e2);
  187. if (pe->sym || e2.sym)
  188. tcc_error("invalid operation with label");
  189. switch(op) {
  190. case '*':
  191. pe->v *= e2.v;
  192. break;
  193. case '/':
  194. if (e2.v == 0) {
  195. div_error:
  196. tcc_error("division by zero");
  197. }
  198. pe->v /= e2.v;
  199. break;
  200. case '%':
  201. if (e2.v == 0)
  202. goto div_error;
  203. pe->v %= e2.v;
  204. break;
  205. case TOK_SHL:
  206. pe->v <<= e2.v;
  207. break;
  208. default:
  209. case TOK_SAR:
  210. pe->v >>= e2.v;
  211. break;
  212. }
  213. }
  214. }
  215. static void asm_expr_logic(TCCState *s1, ExprValue *pe)
  216. {
  217. int op;
  218. ExprValue e2;
  219. asm_expr_prod(s1, pe);
  220. for(;;) {
  221. op = tok;
  222. if (op != '&' && op != '|' && op != '^')
  223. break;
  224. next();
  225. asm_expr_prod(s1, &e2);
  226. if (pe->sym || e2.sym)
  227. tcc_error("invalid operation with label");
  228. switch(op) {
  229. case '&':
  230. pe->v &= e2.v;
  231. break;
  232. case '|':
  233. pe->v |= e2.v;
  234. break;
  235. default:
  236. case '^':
  237. pe->v ^= e2.v;
  238. break;
  239. }
  240. }
  241. }
  242. static inline void asm_expr_sum(TCCState *s1, ExprValue *pe)
  243. {
  244. int op;
  245. ExprValue e2;
  246. asm_expr_logic(s1, pe);
  247. for(;;) {
  248. op = tok;
  249. if (op != '+' && op != '-')
  250. break;
  251. next();
  252. asm_expr_logic(s1, &e2);
  253. if (op == '+') {
  254. if (pe->sym != NULL && e2.sym != NULL)
  255. goto cannot_relocate;
  256. pe->v += e2.v;
  257. if (pe->sym == NULL && e2.sym != NULL)
  258. pe->sym = e2.sym;
  259. } else {
  260. pe->v -= e2.v;
  261. /* NOTE: we are less powerful than gas in that case
  262. because we store only one symbol in the expression */
  263. if (!e2.sym) {
  264. /* OK */
  265. } else if (pe->sym == e2.sym) {
  266. /* OK */
  267. pe->sym = NULL; /* same symbols can be subtracted to NULL */
  268. } else {
  269. ElfSym *esym1, *esym2;
  270. esym1 = elfsym(pe->sym);
  271. esym2 = elfsym(e2.sym);
  272. if (esym1 && esym1->st_shndx == esym2->st_shndx
  273. && esym1->st_shndx != SHN_UNDEF) {
  274. /* we also accept defined symbols in the same section */
  275. pe->v += esym1->st_value - esym2->st_value;
  276. pe->sym = NULL;
  277. } else if (esym2->st_shndx == cur_text_section->sh_num) {
  278. /* When subtracting a defined symbol in current section
  279. this actually makes the value PC-relative. */
  280. pe->v -= esym2->st_value - ind - 4;
  281. pe->pcrel = 1;
  282. e2.sym = NULL;
  283. } else {
  284. cannot_relocate:
  285. tcc_error("invalid operation with label");
  286. }
  287. }
  288. }
  289. }
  290. }
  291. static inline void asm_expr_cmp(TCCState *s1, ExprValue *pe)
  292. {
  293. int op;
  294. ExprValue e2;
  295. asm_expr_sum(s1, pe);
  296. for(;;) {
  297. op = tok;
  298. if (op != TOK_EQ && op != TOK_NE
  299. && (op > TOK_GT || op < TOK_ULE))
  300. break;
  301. next();
  302. asm_expr_sum(s1, &e2);
  303. if (pe->sym || e2.sym)
  304. tcc_error("invalid operation with label");
  305. switch(op) {
  306. case TOK_EQ:
  307. pe->v = pe->v == e2.v;
  308. break;
  309. case TOK_NE:
  310. pe->v = pe->v != e2.v;
  311. break;
  312. case TOK_LT:
  313. pe->v = (int64_t)pe->v < (int64_t)e2.v;
  314. break;
  315. case TOK_GE:
  316. pe->v = (int64_t)pe->v >= (int64_t)e2.v;
  317. break;
  318. case TOK_LE:
  319. pe->v = (int64_t)pe->v <= (int64_t)e2.v;
  320. break;
  321. case TOK_GT:
  322. pe->v = (int64_t)pe->v > (int64_t)e2.v;
  323. break;
  324. default:
  325. break;
  326. }
  327. /* GAS compare results are -1/0 not 1/0. */
  328. pe->v = -(int64_t)pe->v;
  329. }
  330. }
  331. ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)
  332. {
  333. asm_expr_cmp(s1, pe);
  334. }
  335. ST_FUNC int asm_int_expr(TCCState *s1)
  336. {
  337. ExprValue e;
  338. asm_expr(s1, &e);
  339. if (e.sym)
  340. expect("constant");
  341. return e.v;
  342. }
  343. static Sym* asm_new_label1(TCCState *s1, int label, int is_local,
  344. int sh_num, int value)
  345. {
  346. Sym *sym;
  347. ElfSym *esym;
  348. sym = asm_label_find(label);
  349. if (sym) {
  350. esym = elfsym(sym);
  351. /* A VT_EXTERN symbol, even if it has a section is considered
  352. overridable. This is how we "define" .set targets. Real
  353. definitions won't have VT_EXTERN set. */
  354. if (esym && esym->st_shndx != SHN_UNDEF) {
  355. /* the label is already defined */
  356. if (IS_ASM_SYM(sym)
  357. && (is_local == 1 || (sym->type.t & VT_EXTERN)))
  358. goto new_label;
  359. if (!(sym->type.t & VT_EXTERN))
  360. tcc_error("assembler label '%s' already defined",
  361. get_tok_str(label, NULL));
  362. }
  363. } else {
  364. new_label:
  365. sym = asm_label_push(label);
  366. }
  367. if (!sym->c)
  368. put_extern_sym2(sym, SHN_UNDEF, 0, 0, 0);
  369. esym = elfsym(sym);
  370. esym->st_shndx = sh_num;
  371. esym->st_value = value;
  372. if (is_local != 2)
  373. sym->type.t &= ~VT_EXTERN;
  374. return sym;
  375. }
  376. static Sym* asm_new_label(TCCState *s1, int label, int is_local)
  377. {
  378. return asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind);
  379. }
  380. /* Set the value of LABEL to that of some expression (possibly
  381. involving other symbols). LABEL can be overwritten later still. */
  382. static Sym* set_symbol(TCCState *s1, int label)
  383. {
  384. long n;
  385. ExprValue e;
  386. Sym *sym;
  387. ElfSym *esym;
  388. next();
  389. asm_expr(s1, &e);
  390. n = e.v;
  391. esym = elfsym(e.sym);
  392. if (esym)
  393. n += esym->st_value;
  394. sym = asm_new_label1(s1, label, 2, esym ? esym->st_shndx : SHN_ABS, n);
  395. elfsym(sym)->st_other |= ST_ASM_SET;
  396. return sym;
  397. }
  398. static void use_section1(TCCState *s1, Section *sec)
  399. {
  400. cur_text_section->data_offset = ind;
  401. cur_text_section = sec;
  402. ind = cur_text_section->data_offset;
  403. }
  404. static void use_section(TCCState *s1, const char *name)
  405. {
  406. Section *sec;
  407. sec = find_section(s1, name);
  408. use_section1(s1, sec);
  409. }
  410. static void push_section(TCCState *s1, const char *name)
  411. {
  412. Section *sec = find_section(s1, name);
  413. sec->prev = cur_text_section;
  414. use_section1(s1, sec);
  415. }
  416. static void pop_section(TCCState *s1)
  417. {
  418. Section *prev = cur_text_section->prev;
  419. if (!prev)
  420. tcc_error(".popsection without .pushsection");
  421. cur_text_section->prev = NULL;
  422. use_section1(s1, prev);
  423. }
  424. static void asm_parse_directive(TCCState *s1, int global)
  425. {
  426. int n, offset, v, size, tok1;
  427. Section *sec;
  428. uint8_t *ptr;
  429. /* assembler directive */
  430. sec = cur_text_section;
  431. switch(tok) {
  432. case TOK_ASMDIR_align:
  433. case TOK_ASMDIR_balign:
  434. case TOK_ASMDIR_p2align:
  435. case TOK_ASMDIR_skip:
  436. case TOK_ASMDIR_space:
  437. tok1 = tok;
  438. next();
  439. n = asm_int_expr(s1);
  440. if (tok1 == TOK_ASMDIR_p2align)
  441. {
  442. if (n < 0 || n > 30)
  443. tcc_error("invalid p2align, must be between 0 and 30");
  444. n = 1 << n;
  445. tok1 = TOK_ASMDIR_align;
  446. }
  447. if (tok1 == TOK_ASMDIR_align || tok1 == TOK_ASMDIR_balign) {
  448. if (n < 0 || (n & (n-1)) != 0)
  449. tcc_error("alignment must be a positive power of two");
  450. offset = (ind + n - 1) & -n;
  451. size = offset - ind;
  452. /* the section must have a compatible alignment */
  453. if (sec->sh_addralign < n)
  454. sec->sh_addralign = n;
  455. } else {
  456. if (n < 0)
  457. n = 0;
  458. size = n;
  459. }
  460. v = 0;
  461. if (tok == ',') {
  462. next();
  463. v = asm_int_expr(s1);
  464. }
  465. zero_pad:
  466. if (sec->sh_type != SHT_NOBITS) {
  467. sec->data_offset = ind;
  468. ptr = section_ptr_add(sec, size);
  469. memset(ptr, v, size);
  470. }
  471. ind += size;
  472. break;
  473. case TOK_ASMDIR_quad:
  474. #ifdef TCC_TARGET_X86_64
  475. size = 8;
  476. goto asm_data;
  477. #else
  478. next();
  479. for(;;) {
  480. uint64_t vl;
  481. const char *p;
  482. p = tokc.str.data;
  483. if (tok != TOK_PPNUM) {
  484. error_constant:
  485. tcc_error("64 bit constant");
  486. }
  487. vl = strtoll(p, (char **)&p, 0);
  488. if (*p != '\0')
  489. goto error_constant;
  490. next();
  491. if (sec->sh_type != SHT_NOBITS) {
  492. /* XXX: endianness */
  493. gen_le32(vl);
  494. gen_le32(vl >> 32);
  495. } else {
  496. ind += 8;
  497. }
  498. if (tok != ',')
  499. break;
  500. next();
  501. }
  502. break;
  503. #endif
  504. case TOK_ASMDIR_byte:
  505. size = 1;
  506. goto asm_data;
  507. case TOK_ASMDIR_word:
  508. case TOK_ASMDIR_short:
  509. size = 2;
  510. goto asm_data;
  511. case TOK_ASMDIR_long:
  512. case TOK_ASMDIR_int:
  513. size = 4;
  514. asm_data:
  515. next();
  516. for(;;) {
  517. ExprValue e;
  518. asm_expr(s1, &e);
  519. if (sec->sh_type != SHT_NOBITS) {
  520. if (size == 4) {
  521. gen_expr32(&e);
  522. #ifdef TCC_TARGET_X86_64
  523. } else if (size == 8) {
  524. gen_expr64(&e);
  525. #endif
  526. } else {
  527. if (e.sym)
  528. expect("constant");
  529. if (size == 1)
  530. g(e.v);
  531. else
  532. gen_le16(e.v);
  533. }
  534. } else {
  535. ind += size;
  536. }
  537. if (tok != ',')
  538. break;
  539. next();
  540. }
  541. break;
  542. case TOK_ASMDIR_fill:
  543. {
  544. int repeat, size, val, i, j;
  545. uint8_t repeat_buf[8];
  546. next();
  547. repeat = asm_int_expr(s1);
  548. if (repeat < 0) {
  549. tcc_error("repeat < 0; .fill ignored");
  550. break;
  551. }
  552. size = 1;
  553. val = 0;
  554. if (tok == ',') {
  555. next();
  556. size = asm_int_expr(s1);
  557. if (size < 0) {
  558. tcc_error("size < 0; .fill ignored");
  559. break;
  560. }
  561. if (size > 8)
  562. size = 8;
  563. if (tok == ',') {
  564. next();
  565. val = asm_int_expr(s1);
  566. }
  567. }
  568. /* XXX: endianness */
  569. repeat_buf[0] = val;
  570. repeat_buf[1] = val >> 8;
  571. repeat_buf[2] = val >> 16;
  572. repeat_buf[3] = val >> 24;
  573. repeat_buf[4] = 0;
  574. repeat_buf[5] = 0;
  575. repeat_buf[6] = 0;
  576. repeat_buf[7] = 0;
  577. for(i = 0; i < repeat; i++) {
  578. for(j = 0; j < size; j++) {
  579. g(repeat_buf[j]);
  580. }
  581. }
  582. }
  583. break;
  584. case TOK_ASMDIR_rept:
  585. {
  586. int repeat;
  587. TokenString *init_str;
  588. next();
  589. repeat = asm_int_expr(s1);
  590. init_str = tok_str_alloc();
  591. while (next(), tok != TOK_ASMDIR_endr) {
  592. if (tok == CH_EOF)
  593. tcc_error("we at end of file, .endr not found");
  594. tok_str_add_tok(init_str);
  595. }
  596. tok_str_add(init_str, -1);
  597. tok_str_add(init_str, 0);
  598. begin_macro(init_str, 1);
  599. while (repeat-- > 0) {
  600. tcc_assemble_internal(s1, (parse_flags & PARSE_FLAG_PREPROCESS),
  601. global);
  602. macro_ptr = init_str->str;
  603. }
  604. end_macro();
  605. next();
  606. break;
  607. }
  608. case TOK_ASMDIR_org:
  609. {
  610. unsigned long n;
  611. ExprValue e;
  612. ElfSym *esym;
  613. next();
  614. asm_expr(s1, &e);
  615. n = e.v;
  616. esym = elfsym(e.sym);
  617. if (esym) {
  618. if (esym->st_shndx != cur_text_section->sh_num)
  619. expect("constant or same-section symbol");
  620. n += esym->st_value;
  621. }
  622. if (n < ind)
  623. tcc_error("attempt to .org backwards");
  624. v = 0;
  625. size = n - ind;
  626. goto zero_pad;
  627. }
  628. break;
  629. case TOK_ASMDIR_set:
  630. next();
  631. tok1 = tok;
  632. next();
  633. /* Also accept '.set stuff', but don't do anything with this.
  634. It's used in GAS to set various features like '.set mips16'. */
  635. if (tok == ',')
  636. set_symbol(s1, tok1);
  637. break;
  638. case TOK_ASMDIR_globl:
  639. case TOK_ASMDIR_global:
  640. case TOK_ASMDIR_weak:
  641. case TOK_ASMDIR_hidden:
  642. tok1 = tok;
  643. do {
  644. Sym *sym;
  645. next();
  646. sym = get_asm_sym(tok, NULL);
  647. if (tok1 != TOK_ASMDIR_hidden)
  648. sym->type.t &= ~VT_STATIC;
  649. if (tok1 == TOK_ASMDIR_weak)
  650. sym->a.weak = 1;
  651. else if (tok1 == TOK_ASMDIR_hidden)
  652. sym->a.visibility = STV_HIDDEN;
  653. update_storage(sym);
  654. next();
  655. } while (tok == ',');
  656. break;
  657. case TOK_ASMDIR_string:
  658. case TOK_ASMDIR_ascii:
  659. case TOK_ASMDIR_asciz:
  660. {
  661. const uint8_t *p;
  662. int i, size, t;
  663. t = tok;
  664. next();
  665. for(;;) {
  666. if (tok != TOK_STR)
  667. expect("string constant");
  668. p = tokc.str.data;
  669. size = tokc.str.size;
  670. if (t == TOK_ASMDIR_ascii && size > 0)
  671. size--;
  672. for(i = 0; i < size; i++)
  673. g(p[i]);
  674. next();
  675. if (tok == ',') {
  676. next();
  677. } else if (tok != TOK_STR) {
  678. break;
  679. }
  680. }
  681. }
  682. break;
  683. case TOK_ASMDIR_text:
  684. case TOK_ASMDIR_data:
  685. case TOK_ASMDIR_bss:
  686. {
  687. char sname[64];
  688. tok1 = tok;
  689. n = 0;
  690. next();
  691. if (tok != ';' && tok != TOK_LINEFEED) {
  692. n = asm_int_expr(s1);
  693. next();
  694. }
  695. if (n)
  696. sprintf(sname, "%s%d", get_tok_str(tok1, NULL), n);
  697. else
  698. sprintf(sname, "%s", get_tok_str(tok1, NULL));
  699. use_section(s1, sname);
  700. }
  701. break;
  702. case TOK_ASMDIR_file:
  703. {
  704. char filename[512];
  705. filename[0] = '\0';
  706. next();
  707. if (tok == TOK_STR)
  708. pstrcat(filename, sizeof(filename), tokc.str.data);
  709. else
  710. pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL));
  711. if (s1->warn_unsupported)
  712. tcc_warning("ignoring .file %s", filename);
  713. next();
  714. }
  715. break;
  716. case TOK_ASMDIR_ident:
  717. {
  718. char ident[256];
  719. ident[0] = '\0';
  720. next();
  721. if (tok == TOK_STR)
  722. pstrcat(ident, sizeof(ident), tokc.str.data);
  723. else
  724. pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
  725. if (s1->warn_unsupported)
  726. tcc_warning("ignoring .ident %s", ident);
  727. next();
  728. }
  729. break;
  730. case TOK_ASMDIR_size:
  731. {
  732. Sym *sym;
  733. next();
  734. sym = asm_label_find(tok);
  735. if (!sym) {
  736. tcc_error("label not found: %s", get_tok_str(tok, NULL));
  737. }
  738. /* XXX .size name,label2-label1 */
  739. if (s1->warn_unsupported)
  740. tcc_warning("ignoring .size %s,*", get_tok_str(tok, NULL));
  741. next();
  742. skip(',');
  743. while (tok != TOK_LINEFEED && tok != ';' && tok != CH_EOF) {
  744. next();
  745. }
  746. }
  747. break;
  748. case TOK_ASMDIR_type:
  749. {
  750. Sym *sym;
  751. const char *newtype;
  752. next();
  753. sym = get_asm_sym(tok, NULL);
  754. next();
  755. skip(',');
  756. if (tok == TOK_STR) {
  757. newtype = tokc.str.data;
  758. } else {
  759. if (tok == '@' || tok == '%')
  760. next();
  761. newtype = get_tok_str(tok, NULL);
  762. }
  763. if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
  764. sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC;
  765. }
  766. else if (s1->warn_unsupported)
  767. tcc_warning("change type of '%s' from 0x%x to '%s' ignored",
  768. get_tok_str(sym->v, NULL), sym->type.t, newtype);
  769. next();
  770. }
  771. break;
  772. case TOK_ASMDIR_pushsection:
  773. case TOK_ASMDIR_section:
  774. {
  775. char sname[256];
  776. int old_nb_section = s1->nb_sections;
  777. tok1 = tok;
  778. /* XXX: support more options */
  779. next();
  780. sname[0] = '\0';
  781. while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
  782. if (tok == TOK_STR)
  783. pstrcat(sname, sizeof(sname), tokc.str.data);
  784. else
  785. pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
  786. next();
  787. }
  788. if (tok == ',') {
  789. /* skip section options */
  790. next();
  791. if (tok != TOK_STR)
  792. expect("string constant");
  793. next();
  794. if (tok == ',') {
  795. next();
  796. if (tok == '@' || tok == '%')
  797. next();
  798. next();
  799. }
  800. }
  801. last_text_section = cur_text_section;
  802. if (tok1 == TOK_ASMDIR_section)
  803. use_section(s1, sname);
  804. else
  805. push_section(s1, sname);
  806. /* If we just allocated a new section reset its alignment to
  807. 1. new_section normally acts for GCC compatibility and
  808. sets alignment to PTR_SIZE. The assembler behaves different. */
  809. if (old_nb_section != s1->nb_sections)
  810. cur_text_section->sh_addralign = 1;
  811. }
  812. break;
  813. case TOK_ASMDIR_previous:
  814. {
  815. Section *sec;
  816. next();
  817. if (!last_text_section)
  818. tcc_error("no previous section referenced");
  819. sec = cur_text_section;
  820. use_section1(s1, last_text_section);
  821. last_text_section = sec;
  822. }
  823. break;
  824. case TOK_ASMDIR_popsection:
  825. next();
  826. pop_section(s1);
  827. break;
  828. #ifdef TCC_TARGET_I386
  829. case TOK_ASMDIR_code16:
  830. {
  831. next();
  832. s1->seg_size = 16;
  833. }
  834. break;
  835. case TOK_ASMDIR_code32:
  836. {
  837. next();
  838. s1->seg_size = 32;
  839. }
  840. break;
  841. #endif
  842. #ifdef TCC_TARGET_X86_64
  843. /* added for compatibility with GAS */
  844. case TOK_ASMDIR_code64:
  845. next();
  846. break;
  847. #endif
  848. default:
  849. tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
  850. break;
  851. }
  852. }
  853. /* assemble a file */
  854. static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global)
  855. {
  856. int opcode;
  857. int saved_parse_flags = parse_flags;
  858. parse_flags = PARSE_FLAG_ASM_FILE | PARSE_FLAG_TOK_STR;
  859. if (do_preprocess)
  860. parse_flags |= PARSE_FLAG_PREPROCESS;
  861. for(;;) {
  862. next();
  863. if (tok == TOK_EOF)
  864. break;
  865. /* generate line number info */
  866. if (global && s1->do_debug)
  867. tcc_debug_line(s1);
  868. parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
  869. redo:
  870. if (tok == '#') {
  871. /* horrible gas comment */
  872. while (tok != TOK_LINEFEED)
  873. next();
  874. } else if (tok >= TOK_ASMDIR_FIRST && tok <= TOK_ASMDIR_LAST) {
  875. asm_parse_directive(s1, global);
  876. } else if (tok == TOK_PPNUM) {
  877. const char *p;
  878. int n;
  879. p = tokc.str.data;
  880. n = strtoul(p, (char **)&p, 10);
  881. if (*p != '\0')
  882. expect("':'");
  883. /* new local label */
  884. asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
  885. next();
  886. skip(':');
  887. goto redo;
  888. } else if (tok >= TOK_IDENT) {
  889. /* instruction or label */
  890. opcode = tok;
  891. next();
  892. if (tok == ':') {
  893. /* new label */
  894. asm_new_label(s1, opcode, 0);
  895. next();
  896. goto redo;
  897. } else if (tok == '=') {
  898. set_symbol(s1, opcode);
  899. goto redo;
  900. } else {
  901. asm_opcode(s1, opcode);
  902. }
  903. }
  904. /* end of line */
  905. if (tok != ';' && tok != TOK_LINEFEED)
  906. expect("end of line");
  907. parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
  908. }
  909. parse_flags = saved_parse_flags;
  910. return 0;
  911. }
  912. /* Assemble the current file */
  913. ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
  914. {
  915. int ret;
  916. tcc_debug_start(s1);
  917. /* default section is text */
  918. cur_text_section = text_section;
  919. ind = cur_text_section->data_offset;
  920. nocode_wanted = 0;
  921. ret = tcc_assemble_internal(s1, do_preprocess, 1);
  922. cur_text_section->data_offset = ind;
  923. tcc_debug_end(s1);
  924. return ret;
  925. }
  926. /********************************************************************/
  927. /* GCC inline asm support */
  928. /* assemble the string 'str' in the current C compilation unit without
  929. C preprocessing. NOTE: str is modified by modifying the '\0' at the
  930. end */
  931. static void tcc_assemble_inline(TCCState *s1, char *str, int len, int global)
  932. {
  933. const int *saved_macro_ptr = macro_ptr;
  934. int dotid = set_idnum('.', IS_ID);
  935. tcc_open_bf(s1, ":asm:", len);
  936. memcpy(file->buffer, str, len);
  937. macro_ptr = NULL;
  938. tcc_assemble_internal(s1, 0, global);
  939. tcc_close();
  940. set_idnum('.', dotid);
  941. macro_ptr = saved_macro_ptr;
  942. }
  943. /* find a constraint by its number or id (gcc 3 extended
  944. syntax). return -1 if not found. Return in *pp in char after the
  945. constraint */
  946. ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands,
  947. const char *name, const char **pp)
  948. {
  949. int index;
  950. TokenSym *ts;
  951. const char *p;
  952. if (isnum(*name)) {
  953. index = 0;
  954. while (isnum(*name)) {
  955. index = (index * 10) + (*name) - '0';
  956. name++;
  957. }
  958. if ((unsigned)index >= nb_operands)
  959. index = -1;
  960. } else if (*name == '[') {
  961. name++;
  962. p = strchr(name, ']');
  963. if (p) {
  964. ts = tok_alloc(name, p - name);
  965. for(index = 0; index < nb_operands; index++) {
  966. if (operands[index].id == ts->tok)
  967. goto found;
  968. }
  969. index = -1;
  970. found:
  971. name = p + 1;
  972. } else {
  973. index = -1;
  974. }
  975. } else {
  976. index = -1;
  977. }
  978. if (pp)
  979. *pp = name;
  980. return index;
  981. }
  982. static void subst_asm_operands(ASMOperand *operands, int nb_operands,
  983. CString *out_str, CString *in_str)
  984. {
  985. int c, index, modifier;
  986. const char *str;
  987. ASMOperand *op;
  988. SValue sv;
  989. cstr_new(out_str);
  990. str = in_str->data;
  991. for(;;) {
  992. c = *str++;
  993. if (c == '%') {
  994. if (*str == '%') {
  995. str++;
  996. goto add_char;
  997. }
  998. modifier = 0;
  999. if (*str == 'c' || *str == 'n' ||
  1000. *str == 'b' || *str == 'w' || *str == 'h' || *str == 'k' ||
  1001. *str == 'q' ||
  1002. /* P in GCC would add "@PLT" to symbol refs in PIC mode,
  1003. and make literal operands not be decorated with '$'. */
  1004. *str == 'P')
  1005. modifier = *str++;
  1006. index = find_constraint(operands, nb_operands, str, &str);
  1007. if (index < 0)
  1008. tcc_error("invalid operand reference after %%");
  1009. op = &operands[index];
  1010. sv = *op->vt;
  1011. if (op->reg >= 0) {
  1012. sv.r = op->reg;
  1013. if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
  1014. sv.r |= VT_LVAL;
  1015. }
  1016. subst_asm_operand(out_str, &sv, modifier);
  1017. } else {
  1018. add_char:
  1019. cstr_ccat(out_str, c);
  1020. if (c == '\0')
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
  1026. int is_output)
  1027. {
  1028. ASMOperand *op;
  1029. int nb_operands;
  1030. if (tok != ':') {
  1031. nb_operands = *nb_operands_ptr;
  1032. for(;;) {
  1033. CString astr;
  1034. if (nb_operands >= MAX_ASM_OPERANDS)
  1035. tcc_error("too many asm operands");
  1036. op = &operands[nb_operands++];
  1037. op->id = 0;
  1038. if (tok == '[') {
  1039. next();
  1040. if (tok < TOK_IDENT)
  1041. expect("identifier");
  1042. op->id = tok;
  1043. next();
  1044. skip(']');
  1045. }
  1046. parse_mult_str(&astr, "string constant");
  1047. op->constraint = tcc_malloc(astr.size);
  1048. strcpy(op->constraint, astr.data);
  1049. cstr_free(&astr);
  1050. skip('(');
  1051. gexpr();
  1052. if (is_output) {
  1053. if (!(vtop->type.t & VT_ARRAY))
  1054. test_lvalue();
  1055. } else {
  1056. /* we want to avoid LLOCAL case, except when the 'm'
  1057. constraint is used. Note that it may come from
  1058. register storage, so we need to convert (reg)
  1059. case */
  1060. if ((vtop->r & VT_LVAL) &&
  1061. ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
  1062. (vtop->r & VT_VALMASK) < VT_CONST) &&
  1063. !strchr(op->constraint, 'm')) {
  1064. gv(RC_INT);
  1065. }
  1066. }
  1067. op->vt = vtop;
  1068. skip(')');
  1069. if (tok == ',') {
  1070. next();
  1071. } else {
  1072. break;
  1073. }
  1074. }
  1075. *nb_operands_ptr = nb_operands;
  1076. }
  1077. }
  1078. /* parse the GCC asm() instruction */
  1079. ST_FUNC void asm_instr(void)
  1080. {
  1081. CString astr, astr1;
  1082. ASMOperand operands[MAX_ASM_OPERANDS];
  1083. int nb_outputs, nb_operands, i, must_subst, out_reg;
  1084. uint8_t clobber_regs[NB_ASM_REGS];
  1085. next();
  1086. /* since we always generate the asm() instruction, we can ignore
  1087. volatile */
  1088. if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
  1089. next();
  1090. }
  1091. parse_asm_str(&astr);
  1092. nb_operands = 0;
  1093. nb_outputs = 0;
  1094. must_subst = 0;
  1095. memset(clobber_regs, 0, sizeof(clobber_regs));
  1096. if (tok == ':') {
  1097. next();
  1098. must_subst = 1;
  1099. /* output args */
  1100. parse_asm_operands(operands, &nb_operands, 1);
  1101. nb_outputs = nb_operands;
  1102. if (tok == ':') {
  1103. next();
  1104. if (tok != ')') {
  1105. /* input args */
  1106. parse_asm_operands(operands, &nb_operands, 0);
  1107. if (tok == ':') {
  1108. /* clobber list */
  1109. /* XXX: handle registers */
  1110. next();
  1111. for(;;) {
  1112. if (tok != TOK_STR)
  1113. expect("string constant");
  1114. asm_clobber(clobber_regs, tokc.str.data);
  1115. next();
  1116. if (tok == ',') {
  1117. next();
  1118. } else {
  1119. break;
  1120. }
  1121. }
  1122. }
  1123. }
  1124. }
  1125. }
  1126. skip(')');
  1127. /* NOTE: we do not eat the ';' so that we can restore the current
  1128. token after the assembler parsing */
  1129. if (tok != ';')
  1130. expect("';'");
  1131. /* save all values in the memory */
  1132. save_regs(0);
  1133. /* compute constraints */
  1134. asm_compute_constraints(operands, nb_operands, nb_outputs,
  1135. clobber_regs, &out_reg);
  1136. /* substitute the operands in the asm string. No substitution is
  1137. done if no operands (GCC behaviour) */
  1138. #ifdef ASM_DEBUG
  1139. printf("asm: \"%s\"\n", (char *)astr.data);
  1140. #endif
  1141. if (must_subst) {
  1142. subst_asm_operands(operands, nb_operands, &astr1, &astr);
  1143. cstr_free(&astr);
  1144. } else {
  1145. astr1 = astr;
  1146. }
  1147. #ifdef ASM_DEBUG
  1148. printf("subst_asm: \"%s\"\n", (char *)astr1.data);
  1149. #endif
  1150. /* generate loads */
  1151. asm_gen_code(operands, nb_operands, nb_outputs, 0,
  1152. clobber_regs, out_reg);
  1153. /* assemble the string with tcc internal assembler */
  1154. tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1, 0);
  1155. /* restore the current C token */
  1156. next();
  1157. /* store the output values if needed */
  1158. asm_gen_code(operands, nb_operands, nb_outputs, 1,
  1159. clobber_regs, out_reg);
  1160. /* free everything */
  1161. for(i=0;i<nb_operands;i++) {
  1162. ASMOperand *op;
  1163. op = &operands[i];
  1164. tcc_free(op->constraint);
  1165. vpop();
  1166. }
  1167. cstr_free(&astr1);
  1168. }
  1169. ST_FUNC void asm_global_instr(void)
  1170. {
  1171. CString astr;
  1172. int saved_nocode_wanted = nocode_wanted;
  1173. /* Global asm blocks are always emitted. */
  1174. nocode_wanted = 0;
  1175. next();
  1176. parse_asm_str(&astr);
  1177. skip(')');
  1178. /* NOTE: we do not eat the ';' so that we can restore the current
  1179. token after the assembler parsing */
  1180. if (tok != ';')
  1181. expect("';'");
  1182. #ifdef ASM_DEBUG
  1183. printf("asm_global: \"%s\"\n", (char *)astr.data);
  1184. #endif
  1185. cur_text_section = text_section;
  1186. ind = cur_text_section->data_offset;
  1187. /* assemble the string with tcc internal assembler */
  1188. tcc_assemble_inline(tcc_state, astr.data, astr.size - 1, 1);
  1189. cur_text_section->data_offset = ind;
  1190. /* restore the current C token */
  1191. next();
  1192. cstr_free(&astr);
  1193. nocode_wanted = saved_nocode_wanted;
  1194. }
  1195. #endif /* CONFIG_TCC_ASM */