PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/compiler/raatt.pas

https://github.com/slibre/freepascal
Pascal | 1683 lines | 1457 code | 108 blank | 118 comment | 111 complexity | ccd1b59afaaf39f46e137a1524e6f14c MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, LGPL-3.0
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing for the GAS styled inline assembler.
  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 2 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, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit raatt;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,
  23. { global }
  24. globtype,
  25. { aasm }
  26. cpubase,cpuinfo,aasmbase,aasmtai,aasmdata,aasmcpu,
  27. { assembler reader }
  28. rabase,
  29. rasm,
  30. rautils,
  31. { symtable }
  32. symconst,
  33. { cg }
  34. cgbase;
  35. type
  36. tasmtoken = (
  37. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  38. AS_REALNUM,AS_COMMA,AS_LPAREN,
  39. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  40. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_DOLLAR,
  41. AS_HASH,AS_LSBRACKET,AS_RSBRACKET,AS_LBRACKET,AS_RBRACKET,
  42. {------------------ Assembler directives --------------------}
  43. AS_DB,AS_DW,AS_DD,AS_DQ,AS_GLOBAL,
  44. AS_ALIGN,AS_BALIGN,AS_P2ALIGN,AS_ASCII,
  45. AS_ASCIIZ,AS_LCOMM,AS_COMM,AS_SINGLE,AS_DOUBLE,AS_EXTENDED,AS_CEXTENDED,
  46. AS_DATA,AS_TEXT,AS_INIT,AS_FINI,AS_RVA,
  47. AS_SET,AS_WEAK,AS_SECTION,AS_END,
  48. {------------------ Assembler Operators --------------------}
  49. AS_TYPE,AS_SIZEOF,AS_VMTOFFSET,AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR,AS_AT,
  50. AS_LO,AS_HI,
  51. {------------------ Target-specific directive ---------------}
  52. AS_TARGET_DIRECTIVE
  53. );
  54. tasmkeyword = string[10];
  55. const
  56. { These tokens should be modified accordingly to the modifications }
  57. { in the different enumerations. }
  58. firstdirective = AS_DB;
  59. lastdirective = AS_END;
  60. token2str : array[tasmtoken] of tasmkeyword=(
  61. '','Label','LLabel','string','integer',
  62. 'float',',','(',
  63. ')',':','.','+','-','*',
  64. ';','identifier','register','opcode','/','$',
  65. '#','{','}','[',']',
  66. '.byte','.word','.long','.quad','.globl',
  67. '.align','.balign','.p2align','.ascii',
  68. '.asciz','.lcomm','.comm','.single','.double','.tfloat','.tcfloat',
  69. '.data','.text','.init','.fini','.rva',
  70. '.set','.weak','.section','END',
  71. 'TYPE','SIZEOF','VMTOFFSET','%','<<','>>','!','&','|','^','~','@','lo','hi',
  72. 'directive');
  73. type
  74. tattreader = class(tasmreader)
  75. actasmtoken : tasmtoken;
  76. prevasmtoken : tasmtoken;
  77. procedure SetupTables;
  78. procedure BuildConstant(constsize: byte);
  79. procedure BuildConstantOperand(oper : toperand);
  80. procedure BuildRealConstant(typ : tfloattype);
  81. procedure BuildStringConstant(asciiz: boolean);
  82. procedure BuildRva;
  83. procedure BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  84. procedure BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  85. function BuildConstExpression(allowref,betweenbracket:boolean): aint;
  86. function Assemble: tlinkedlist;override;
  87. procedure handleopcode;virtual;abstract;
  88. function is_asmopcode(const s: string) : boolean;virtual;abstract;
  89. Function is_asmdirective(const s: string):boolean;
  90. function is_register(const s:string):boolean;virtual;
  91. function is_locallabel(const s: string):boolean;
  92. function is_targetdirective(const s: string): boolean;virtual;
  93. procedure GetToken;
  94. function consume(t : tasmtoken):boolean;
  95. procedure RecoverConsume(allowcomma:boolean);
  96. procedure handlepercent;virtual;
  97. procedure handledollar;virtual;
  98. procedure HandleTargetDirective;virtual;
  99. end;
  100. tcattreader = class of tattreader;
  101. var
  102. cattreader : tcattreader;
  103. implementation
  104. uses
  105. { globals }
  106. verbose,systems,
  107. { input }
  108. scanner,
  109. { symtable }
  110. symbase,symtype,symsym,symdef,symtable,
  111. {$ifdef x86}
  112. rax86,
  113. {$endif x86}
  114. itcpugas,
  115. procinfo;
  116. procedure tattreader.SetupTables;
  117. var
  118. i : tasmop;
  119. Begin
  120. iasmops:=TFPHashList.create;
  121. for i:=firstop to lastop do
  122. iasmops.Add(upper(gas_op2str[i]),Pointer(PtrInt(i)));
  123. end;
  124. function tattreader.is_asmdirective(const s: string):boolean;
  125. var
  126. i : tasmtoken;
  127. hs : string;
  128. Begin
  129. { GNU as is also not casesensitive with this }
  130. hs:=lower(s);
  131. for i:=firstdirective to lastdirective do
  132. if hs=token2str[i] then
  133. begin
  134. actasmtoken:=i;
  135. is_asmdirective:=true;
  136. exit;
  137. end;
  138. is_asmdirective:=false;
  139. end;
  140. function tattreader.is_register(const s:string):boolean;
  141. begin
  142. is_register:=false;
  143. actasmregister:=gas_regnum_search(lower(s));
  144. if actasmregister<>NR_NO then
  145. begin
  146. is_register:=true;
  147. actasmtoken:=AS_REGISTER;
  148. end;
  149. end;
  150. function tattreader.is_locallabel(const s: string):boolean;
  151. begin
  152. is_locallabel:=(length(s)>=2) and (s[1]='.') and (s[2]='L');
  153. end;
  154. procedure tattreader.handledollar;
  155. begin
  156. c:=current_scanner.asmgetchar;
  157. actasmtoken:=AS_DOLLAR;
  158. end;
  159. procedure tattreader.handlepercent;
  160. begin
  161. c:=current_scanner.asmgetchar;
  162. actasmtoken:=AS_MOD;
  163. end;
  164. function tattreader.is_targetdirective(const s: string): boolean;
  165. begin
  166. result:=false;
  167. end;
  168. procedure tattreader.handletargetdirective;
  169. begin
  170. end;
  171. procedure tattreader.GetToken;
  172. var
  173. len : longint;
  174. srsym : tsym;
  175. srsymtable : TSymtable;
  176. begin
  177. { save old token and reset new token }
  178. prevasmtoken:=actasmtoken;
  179. actasmtoken:=AS_NONE;
  180. { reset }
  181. actasmpattern:='';
  182. { while space and tab , continue scan... }
  183. while c in [' ',#9] do
  184. c:=current_scanner.asmgetchar;
  185. { get token pos }
  186. if not (c in [#10,#13,'{',';']) then
  187. current_scanner.gettokenpos;
  188. { Local Label, Label, Directive, Prefix or Opcode }
  189. if firsttoken and not(c in [#10,#13,'{',';']) then
  190. begin
  191. firsttoken:=FALSE;
  192. len:=0;
  193. { directive or local label }
  194. if c = '.' then
  195. begin
  196. inc(len);
  197. actasmpattern[len]:=c;
  198. { Let us point to the next character }
  199. c:=current_scanner.asmgetchar;
  200. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  201. begin
  202. inc(len);
  203. actasmpattern[len]:=c;
  204. c:=current_scanner.asmgetchar;
  205. end;
  206. actasmpattern[0]:=chr(len);
  207. { this is a local label... }
  208. if (c=':') and is_locallabel(actasmpattern) then
  209. Begin
  210. { local variables are case sensitive }
  211. actasmtoken:=AS_LLABEL;
  212. c:=current_scanner.asmgetchar;
  213. firsttoken:=true;
  214. exit;
  215. end
  216. { must be a directive }
  217. else
  218. Begin
  219. { directives are case sensitive!! }
  220. if is_asmdirective(actasmpattern) then
  221. exit;
  222. if is_targetdirective(actasmpattern) then
  223. begin
  224. actasmtoken:=AS_TARGET_DIRECTIVE;
  225. exit;
  226. end;
  227. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  228. end;
  229. end;
  230. { only opcodes and global labels are allowed now. }
  231. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  232. begin
  233. inc(len);
  234. actasmpattern[len]:=c;
  235. c:=current_scanner.asmgetchar;
  236. end;
  237. actasmpattern[0]:=chr(len);
  238. { Label ? }
  239. if c = ':' then
  240. begin
  241. actasmtoken:=AS_LABEL;
  242. { let us point to the next character }
  243. c:=current_scanner.asmgetchar;
  244. firsttoken:=true;
  245. exit;
  246. end;
  247. {$if defined(POWERPC) or defined(POWERPC64)}
  248. { some PowerPC instructions can have the postfix -, + or .
  249. this code could be moved to is_asmopcode but I think
  250. it's better to ifdef it here (FK)
  251. }
  252. case c of
  253. '.', '-', '+':
  254. begin
  255. actasmpattern:=actasmpattern+c;
  256. c:=current_scanner.asmgetchar;
  257. end
  258. end;
  259. {$endif POWERPC}
  260. {$if defined(ARM)}
  261. { Thumb-2 instructions can have a .W postfix to indicate 32bit instructions
  262. }
  263. case c of
  264. '.':
  265. begin
  266. actasmpattern:=actasmpattern+c;
  267. c:=current_scanner.asmgetchar;
  268. if upcase(c) = 'W' then
  269. begin
  270. actasmpattern:=actasmpattern+c;
  271. c:=current_scanner.asmgetchar;
  272. end
  273. else
  274. internalerror(2010122301);
  275. end
  276. end;
  277. {$endif ARM}
  278. { Opcode ? }
  279. If is_asmopcode(upper(actasmpattern)) then
  280. Begin
  281. uppervar(actasmpattern);
  282. exit;
  283. end;
  284. { End of assemblerblock ? }
  285. if upper(actasmpattern) = 'END' then
  286. begin
  287. actasmtoken:=AS_END;
  288. exit;
  289. end;
  290. message1(asmr_e_unknown_opcode,actasmpattern);
  291. actasmtoken:=AS_NONE;
  292. end
  293. else { else firsttoken }
  294. { Here we must handle all possible cases }
  295. begin
  296. case c of
  297. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  298. { - field of object/record }
  299. { - directive. }
  300. begin
  301. if (prevasmtoken in [AS_ID,AS_RPAREN]) then
  302. begin
  303. c:=current_scanner.asmgetchar;
  304. actasmtoken:=AS_DOT;
  305. exit;
  306. end;
  307. actasmpattern:=c;
  308. c:=current_scanner.asmgetchar;
  309. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  310. begin
  311. actasmpattern:=actasmpattern + c;
  312. c:=current_scanner.asmgetchar;
  313. end;
  314. if is_asmdirective(actasmpattern) then
  315. exit;
  316. if is_targetdirective(actasmpattern) then
  317. begin
  318. actasmtoken:=AS_TARGET_DIRECTIVE;
  319. exit;
  320. end;
  321. { local label references and directives }
  322. { are case sensitive }
  323. actasmtoken:=AS_ID;
  324. exit;
  325. end;
  326. { identifier, register, prefix or directive }
  327. '_','A'..'Z','a'..'z':
  328. begin
  329. len:=0;
  330. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  331. begin
  332. inc(len);
  333. actasmpattern[len]:=c;
  334. c:=current_scanner.asmgetchar;
  335. end;
  336. actasmpattern[0]:=chr(len);
  337. uppervar(actasmpattern);
  338. {$ifdef x86}
  339. { only x86 architectures have instruction prefixes }
  340. { Opcode, can only be when the previous was a prefix }
  341. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  342. Begin
  343. uppervar(actasmpattern);
  344. exit;
  345. end;
  346. {$endif x86}
  347. { check for end which is a reserved word unlike the opcodes }
  348. if actasmpattern = 'END' then
  349. Begin
  350. actasmtoken:=AS_END;
  351. exit;
  352. end;
  353. if actasmpattern = 'TYPE' then
  354. Begin
  355. actasmtoken:=AS_TYPE;
  356. exit;
  357. end;
  358. if actasmpattern = 'SIZEOF' then
  359. Begin
  360. actasmtoken:=AS_SIZEOF;
  361. exit;
  362. end;
  363. if actasmpattern = 'VMTOFFSET' then
  364. Begin
  365. actasmtoken:=AS_VMTOFFSET;
  366. exit;
  367. end;
  368. if is_register(actasmpattern) then
  369. begin
  370. actasmtoken:=AS_REGISTER;
  371. exit;
  372. end;
  373. { if next is a '.' and this is a unitsym then we also need to
  374. parse the identifier }
  375. if (c='.') then
  376. begin
  377. searchsym(actasmpattern,srsym,srsymtable);
  378. if assigned(srsym) and
  379. (srsym.typ=unitsym) and
  380. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  381. srsym.owner.iscurrentunit then
  382. begin
  383. actasmpattern:=actasmpattern+c;
  384. c:=current_scanner.asmgetchar;
  385. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  386. begin
  387. actasmpattern:=actasmpattern + upcase(c);
  388. c:=current_scanner.asmgetchar;
  389. end;
  390. end;
  391. end;
  392. actasmtoken:=AS_ID;
  393. exit;
  394. end;
  395. '%' : { register or modulo }
  396. handlepercent;
  397. '1'..'9': { integer number }
  398. begin
  399. len:=0;
  400. while c in ['0'..'9'] do
  401. Begin
  402. inc(len);
  403. actasmpattern[len]:=c;
  404. c:=current_scanner.asmgetchar;
  405. end;
  406. actasmpattern[0]:=chr(len);
  407. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  408. actasmtoken:=AS_INTNUM;
  409. exit;
  410. end;
  411. '0' : { octal,hexa,real or binary number. }
  412. begin
  413. actasmpattern:=c;
  414. c:=current_scanner.asmgetchar;
  415. case upcase(c) of
  416. 'B': { binary }
  417. Begin
  418. c:=current_scanner.asmgetchar;
  419. while c in ['0','1'] do
  420. Begin
  421. actasmpattern:=actasmpattern + c;
  422. c:=current_scanner.asmgetchar;
  423. end;
  424. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  425. actasmtoken:=AS_INTNUM;
  426. exit;
  427. end;
  428. 'D': { real }
  429. Begin
  430. c:=current_scanner.asmgetchar;
  431. { get ridd of the 0d }
  432. if (c in ['+','-']) then
  433. begin
  434. actasmpattern:=c;
  435. c:=current_scanner.asmgetchar;
  436. end
  437. else
  438. actasmpattern:='';
  439. while c in ['0'..'9'] do
  440. Begin
  441. actasmpattern:=actasmpattern + c;
  442. c:=current_scanner.asmgetchar;
  443. end;
  444. if c='.' then
  445. begin
  446. actasmpattern:=actasmpattern + c;
  447. c:=current_scanner.asmgetchar;
  448. while c in ['0'..'9'] do
  449. Begin
  450. actasmpattern:=actasmpattern + c;
  451. c:=current_scanner.asmgetchar;
  452. end;
  453. if upcase(c) = 'E' then
  454. begin
  455. actasmpattern:=actasmpattern + c;
  456. c:=current_scanner.asmgetchar;
  457. if (c in ['+','-']) then
  458. begin
  459. actasmpattern:=actasmpattern + c;
  460. c:=current_scanner.asmgetchar;
  461. end;
  462. while c in ['0'..'9'] do
  463. Begin
  464. actasmpattern:=actasmpattern + c;
  465. c:=current_scanner.asmgetchar;
  466. end;
  467. end;
  468. actasmtoken:=AS_REALNUM;
  469. exit;
  470. end
  471. else
  472. begin
  473. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  474. actasmtoken:=AS_NONE;
  475. end;
  476. end;
  477. 'X': { hexadecimal }
  478. Begin
  479. c:=current_scanner.asmgetchar;
  480. while c in ['0'..'9','a'..'f','A'..'F'] do
  481. Begin
  482. actasmpattern:=actasmpattern + c;
  483. c:=current_scanner.asmgetchar;
  484. end;
  485. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  486. actasmtoken:=AS_INTNUM;
  487. exit;
  488. end;
  489. '1'..'7': { octal }
  490. begin
  491. actasmpattern:=actasmpattern + c;
  492. while c in ['0'..'7'] do
  493. Begin
  494. actasmpattern:=actasmpattern + c;
  495. c:=current_scanner.asmgetchar;
  496. end;
  497. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  498. actasmtoken:=AS_INTNUM;
  499. exit;
  500. end;
  501. else { octal number zero value...}
  502. Begin
  503. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  504. actasmtoken:=AS_INTNUM;
  505. exit;
  506. end;
  507. end; { end case }
  508. end;
  509. '&' :
  510. begin
  511. c:=current_scanner.asmgetchar;
  512. actasmtoken:=AS_AND;
  513. end;
  514. '''' : { char }
  515. begin
  516. current_scanner.in_asm_string:=true;
  517. actasmpattern:='';
  518. repeat
  519. c:=current_scanner.asmgetchar;
  520. case c of
  521. '\' :
  522. begin
  523. { copy also the next char so \" is parsed correctly }
  524. actasmpattern:=actasmpattern+c;
  525. c:=current_scanner.asmgetchar;
  526. actasmpattern:=actasmpattern+c;
  527. end;
  528. '''' :
  529. begin
  530. c:=current_scanner.asmgetchar;
  531. break;
  532. end;
  533. #10,#13:
  534. Message(scan_f_string_exceeds_line);
  535. else
  536. actasmpattern:=actasmpattern+c;
  537. end;
  538. until false;
  539. actasmpattern:=EscapeToPascal(actasmpattern);
  540. actasmtoken:=AS_STRING;
  541. current_scanner.in_asm_string:=false;
  542. exit;
  543. end;
  544. '"' : { string }
  545. begin
  546. current_scanner.in_asm_string:=true;
  547. actasmpattern:='';
  548. repeat
  549. c:=current_scanner.asmgetchar;
  550. case c of
  551. '\' :
  552. begin
  553. { copy also the next char so \" is parsed correctly }
  554. actasmpattern:=actasmpattern+c;
  555. c:=current_scanner.asmgetchar;
  556. actasmpattern:=actasmpattern+c;
  557. end;
  558. '"' :
  559. begin
  560. c:=current_scanner.asmgetchar;
  561. break;
  562. end;
  563. #10,#13:
  564. Message(scan_f_string_exceeds_line);
  565. else
  566. actasmpattern:=actasmpattern+c;
  567. end;
  568. until false;
  569. actasmpattern:=EscapeToPascal(actasmpattern);
  570. actasmtoken:=AS_STRING;
  571. current_scanner.in_asm_string:=false;
  572. exit;
  573. end;
  574. '$' :
  575. begin
  576. handledollar;
  577. exit;
  578. end;
  579. '#' :
  580. begin
  581. actasmtoken:=AS_HASH;
  582. c:=current_scanner.asmgetchar;
  583. exit;
  584. end;
  585. '[' :
  586. begin
  587. actasmtoken:=AS_LBRACKET;
  588. c:=current_scanner.asmgetchar;
  589. exit;
  590. end;
  591. ']' :
  592. begin
  593. actasmtoken:=AS_RBRACKET;
  594. c:=current_scanner.asmgetchar;
  595. exit;
  596. end;
  597. {$ifdef arm}
  598. // the arm assembler uses { ... } for register sets
  599. '{' :
  600. begin
  601. actasmtoken:=AS_LSBRACKET;
  602. c:=current_scanner.asmgetchar;
  603. exit;
  604. end;
  605. '}' :
  606. begin
  607. actasmtoken:=AS_RSBRACKET;
  608. c:=current_scanner.asmgetchar;
  609. exit;
  610. end;
  611. {$endif arm}
  612. ',' :
  613. begin
  614. actasmtoken:=AS_COMMA;
  615. c:=current_scanner.asmgetchar;
  616. exit;
  617. end;
  618. '<' :
  619. begin
  620. actasmtoken:=AS_SHL;
  621. c:=current_scanner.asmgetchar;
  622. if c = '<' then
  623. c:=current_scanner.asmgetchar;
  624. exit;
  625. end;
  626. '>' :
  627. begin
  628. actasmtoken:=AS_SHL;
  629. c:=current_scanner.asmgetchar;
  630. if c = '>' then
  631. c:=current_scanner.asmgetchar;
  632. exit;
  633. end;
  634. '|' :
  635. begin
  636. actasmtoken:=AS_OR;
  637. c:=current_scanner.asmgetchar;
  638. exit;
  639. end;
  640. '^' :
  641. begin
  642. actasmtoken:=AS_XOR;
  643. c:=current_scanner.asmgetchar;
  644. exit;
  645. end;
  646. '(' :
  647. begin
  648. actasmtoken:=AS_LPAREN;
  649. c:=current_scanner.asmgetchar;
  650. exit;
  651. end;
  652. ')' :
  653. begin
  654. actasmtoken:=AS_RPAREN;
  655. c:=current_scanner.asmgetchar;
  656. exit;
  657. end;
  658. ':' :
  659. begin
  660. actasmtoken:=AS_COLON;
  661. c:=current_scanner.asmgetchar;
  662. exit;
  663. end;
  664. '+' :
  665. begin
  666. actasmtoken:=AS_PLUS;
  667. c:=current_scanner.asmgetchar;
  668. exit;
  669. end;
  670. '-' :
  671. begin
  672. actasmtoken:=AS_MINUS;
  673. c:=current_scanner.asmgetchar;
  674. exit;
  675. end;
  676. '*' :
  677. begin
  678. actasmtoken:=AS_STAR;
  679. c:=current_scanner.asmgetchar;
  680. exit;
  681. end;
  682. '/' :
  683. begin
  684. actasmtoken:=AS_SLASH;
  685. c:=current_scanner.asmgetchar;
  686. exit;
  687. end;
  688. '!' :
  689. begin
  690. actasmtoken:=AS_NOT;
  691. c:=current_scanner.asmgetchar;
  692. exit;
  693. end;
  694. '@' :
  695. begin
  696. actasmtoken:=AS_AT;
  697. c:=current_scanner.asmgetchar;
  698. exit;
  699. end;
  700. {$ifndef arm}
  701. '{',
  702. {$endif arm}
  703. #13,#10,';' :
  704. begin
  705. { the comment is read by asmgetchar }
  706. c:=current_scanner.asmgetchar;
  707. firsttoken:=TRUE;
  708. actasmtoken:=AS_SEPARATOR;
  709. exit;
  710. end;
  711. else
  712. current_scanner.illegal_char(c);
  713. end;
  714. end;
  715. end;
  716. function tattreader.consume(t : tasmtoken):boolean;
  717. begin
  718. Consume:=true;
  719. if t<>actasmtoken then
  720. begin
  721. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  722. Consume:=false;
  723. end;
  724. repeat
  725. gettoken;
  726. until actasmtoken<>AS_NONE;
  727. end;
  728. procedure tattreader.RecoverConsume(allowcomma:boolean);
  729. begin
  730. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  731. begin
  732. if allowcomma and (actasmtoken=AS_COMMA) then
  733. break;
  734. Consume(actasmtoken);
  735. end;
  736. end;
  737. Procedure tattreader.BuildConstant(constsize: byte);
  738. var
  739. asmsymtyp : TAsmSymType;
  740. asmsym,
  741. expr: string;
  742. value : aint;
  743. Begin
  744. Repeat
  745. Case actasmtoken of
  746. AS_STRING:
  747. Begin
  748. expr:=actasmpattern;
  749. if length(expr) > 1 then
  750. Message(asmr_e_string_not_allowed_as_const);
  751. Consume(AS_STRING);
  752. Case actasmtoken of
  753. AS_COMMA: Consume(AS_COMMA);
  754. AS_END,
  755. AS_SEPARATOR: ;
  756. else
  757. Message(asmr_e_invalid_string_expression);
  758. end; { end case }
  759. ConcatString(curlist,expr);
  760. end;
  761. AS_INTNUM,
  762. AS_PLUS,
  763. AS_MINUS,
  764. AS_LPAREN,
  765. AS_TYPE,
  766. AS_SIZEOF,
  767. AS_NOT,
  768. AS_VMTOFFSET,
  769. AS_ID :
  770. Begin
  771. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  772. if asmsym<>'' then
  773. begin
  774. if constsize<>sizeof(pint) then
  775. Message(asmr_w_32bit_const_for_address);
  776. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value)
  777. end
  778. else
  779. ConcatConstant(curlist,value,constsize);
  780. end;
  781. AS_COMMA:
  782. Consume(AS_COMMA);
  783. AS_END,
  784. AS_SEPARATOR:
  785. break;
  786. else
  787. begin
  788. Message(asmr_e_syn_constant);
  789. RecoverConsume(false);
  790. end
  791. end; { end case }
  792. Until false;
  793. end;
  794. Procedure tattreader.BuildRealConstant(typ : tfloattype);
  795. var
  796. expr : string;
  797. r : bestreal;
  798. code : integer;
  799. negativ : boolean;
  800. errorflag: boolean;
  801. Begin
  802. errorflag:=FALSE;
  803. Repeat
  804. negativ:=false;
  805. expr:='';
  806. if actasmtoken=AS_PLUS then
  807. Consume(AS_PLUS)
  808. else
  809. if actasmtoken=AS_MINUS then
  810. begin
  811. negativ:=true;
  812. consume(AS_MINUS);
  813. end;
  814. Case actasmtoken of
  815. AS_INTNUM:
  816. Begin
  817. expr:=actasmpattern;
  818. Consume(AS_INTNUM);
  819. if negativ then
  820. expr:='-'+expr;
  821. val(expr,r,code);
  822. if code<>0 then
  823. Begin
  824. r:=0;
  825. Message(asmr_e_invalid_float_expr);
  826. End;
  827. ConcatRealConstant(curlist,r,typ);
  828. end;
  829. AS_REALNUM:
  830. Begin
  831. expr:=actasmpattern;
  832. Consume(AS_REALNUM);
  833. { in ATT syntax you have 0d in front of the real }
  834. { should this be forced ? yes i think so, as to }
  835. { conform to gas as much as possible. }
  836. if (expr[1]='0') and (upper(expr[2])='D') then
  837. Delete(expr,1,2);
  838. if negativ then
  839. expr:='-'+expr;
  840. val(expr,r,code);
  841. if code<>0 then
  842. Begin
  843. r:=0;
  844. Message(asmr_e_invalid_float_expr);
  845. End;
  846. ConcatRealConstant(curlist,r,typ);
  847. end;
  848. AS_COMMA:
  849. begin
  850. Consume(AS_COMMA);
  851. end;
  852. AS_END,
  853. AS_SEPARATOR:
  854. begin
  855. break;
  856. end;
  857. else
  858. Begin
  859. Consume(actasmtoken);
  860. if not errorflag then
  861. Message(asmr_e_invalid_float_expr);
  862. errorflag:=TRUE;
  863. end;
  864. end;
  865. Until false;
  866. end;
  867. Procedure tattreader.BuildStringConstant(asciiz: boolean);
  868. var
  869. expr: string;
  870. errorflag : boolean;
  871. Begin
  872. errorflag:=FALSE;
  873. Repeat
  874. Case actasmtoken of
  875. AS_STRING:
  876. Begin
  877. expr:=actasmpattern;
  878. if asciiz then
  879. expr:=expr+#0;
  880. ConcatPasString(curlist,expr);
  881. Consume(AS_STRING);
  882. end;
  883. AS_COMMA:
  884. begin
  885. Consume(AS_COMMA);
  886. end;
  887. AS_END,
  888. AS_SEPARATOR:
  889. begin
  890. break;
  891. end;
  892. else
  893. Begin
  894. Consume(actasmtoken);
  895. if not errorflag then
  896. Message(asmr_e_invalid_string_expression);
  897. errorflag:=TRUE;
  898. end;
  899. end;
  900. Until false;
  901. end;
  902. Function tattreader.Assemble: tlinkedlist;
  903. Var
  904. hl : tasmlabel;
  905. commname,
  906. symname,
  907. symval : string;
  908. lasTSec : TAsmSectiontype;
  909. l1,
  910. l2,
  911. symofs : aint;
  912. symtyp : TAsmsymtype;
  913. Begin
  914. Message1(asmr_d_start_reading,'GNU AS');
  915. firsttoken:=TRUE;
  916. { sets up all opcode and register tables in uppercase }
  917. if not _asmsorted then
  918. Begin
  919. SetupTables;
  920. _asmsorted:=TRUE;
  921. end;
  922. curlist:=TAsmList.Create;
  923. lasTSec:=sec_code;
  924. { setup label linked list }
  925. LocalLabelList:=TLocalLabelList.Create;
  926. { start tokenizer }
  927. c:=current_scanner.asmgetcharstart;
  928. gettoken;
  929. { main loop }
  930. repeat
  931. case actasmtoken of
  932. AS_LLABEL:
  933. Begin
  934. if CreateLocalLabel(actasmpattern,hl,true) then
  935. ConcatLabel(curlist,hl);
  936. Consume(AS_LLABEL);
  937. end;
  938. AS_LABEL:
  939. Begin
  940. if SearchLabel(upper(actasmpattern),hl,true) then
  941. ConcatLabel(curlist,hl)
  942. else
  943. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  944. Consume(AS_LABEL);
  945. end;
  946. AS_DW:
  947. Begin
  948. Consume(AS_DW);
  949. BuildConstant(2);
  950. end;
  951. AS_DATA:
  952. Begin
  953. new_section(curList,sec_data,lower(current_procinfo.procdef.mangledname),0);
  954. lasTSec:=sec_data;
  955. Consume(AS_DATA);
  956. end;
  957. AS_TEXT:
  958. Begin
  959. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  960. lasTSec:=sec_code;
  961. Consume(AS_TEXT);
  962. end;
  963. AS_INIT:
  964. Begin
  965. new_section(curList,sec_init,lower(current_procinfo.procdef.mangledname),0);
  966. lasTSec:=sec_init;
  967. Consume(AS_INIT);
  968. end;
  969. AS_FINI:
  970. Begin
  971. new_section(curList,sec_fini,lower(current_procinfo.procdef.mangledname),0);
  972. lasTSec:=sec_fini;
  973. Consume(AS_FINI);
  974. end;
  975. AS_DB:
  976. Begin
  977. Consume(AS_DB);
  978. BuildConstant(1);
  979. end;
  980. AS_DD:
  981. Begin
  982. Consume(AS_DD);
  983. BuildConstant(4);
  984. end;
  985. AS_DQ:
  986. Begin
  987. Consume(AS_DQ);
  988. {$ifdef cpu64bitaddr}
  989. BuildConstant(8);
  990. {$else cpu64bitaddr}
  991. BuildRealConstant(s64comp);
  992. {$endif cpu64bitaddr}
  993. end;
  994. AS_SINGLE:
  995. Begin
  996. Consume(AS_SINGLE);
  997. BuildRealConstant(s32real);
  998. end;
  999. AS_DOUBLE:
  1000. Begin
  1001. Consume(AS_DOUBLE);
  1002. BuildRealConstant(s64real);
  1003. end;
  1004. AS_EXTENDED:
  1005. Begin
  1006. Consume(AS_EXTENDED);
  1007. BuildRealConstant(s80real);
  1008. end;
  1009. AS_CEXTENDED:
  1010. Begin
  1011. Consume(AS_CEXTENDED);
  1012. BuildRealConstant(sc80real);
  1013. end;
  1014. AS_GLOBAL:
  1015. Begin
  1016. Consume(AS_GLOBAL);
  1017. if actasmtoken=AS_ID then
  1018. ConcatPublic(curlist,actasmpattern);
  1019. Consume(AS_ID);
  1020. if actasmtoken<>AS_SEPARATOR then
  1021. Consume(AS_SEPARATOR);
  1022. end;
  1023. AS_ALIGN:
  1024. Begin
  1025. Consume(AS_ALIGN);
  1026. l1:=BuildConstExpression(false,false);
  1027. if (target_info.system in [system_i386_GO32V2]) then
  1028. begin
  1029. l2:=1;
  1030. if (l1>=0) and (l1<=16) then
  1031. while (l1>0) do
  1032. begin
  1033. l2:=2*l2;
  1034. dec(l1);
  1035. end;
  1036. l1:=l2;
  1037. end;
  1038. ConcatAlign(curlist,l1);
  1039. Message(asmr_n_align_is_target_specific);
  1040. if actasmtoken<>AS_SEPARATOR then
  1041. Consume(AS_SEPARATOR);
  1042. end;
  1043. AS_BALIGN:
  1044. Begin
  1045. Consume(AS_BALIGN);
  1046. ConcatAlign(curlist,BuildConstExpression(false,false));
  1047. if actasmtoken<>AS_SEPARATOR then
  1048. Consume(AS_SEPARATOR);
  1049. end;
  1050. AS_P2ALIGN:
  1051. Begin
  1052. Consume(AS_P2ALIGN);
  1053. l1:=BuildConstExpression(false,false);
  1054. l2:=1;
  1055. if (l1>=0) and (l1<=16) then
  1056. while (l1>0) do
  1057. begin
  1058. l2:=2*l2;
  1059. dec(l1);
  1060. end;
  1061. l1:=l2;
  1062. ConcatAlign(curlist,l1);
  1063. if actasmtoken<>AS_SEPARATOR then
  1064. Consume(AS_SEPARATOR);
  1065. end;
  1066. AS_ASCIIZ:
  1067. Begin
  1068. Consume(AS_ASCIIZ);
  1069. BuildStringConstant(TRUE);
  1070. end;
  1071. AS_ASCII:
  1072. Begin
  1073. Consume(AS_ASCII);
  1074. BuildStringConstant(FALSE);
  1075. end;
  1076. AS_LCOMM:
  1077. Begin
  1078. Consume(AS_LCOMM);
  1079. commname:=actasmpattern;
  1080. Consume(AS_ID);
  1081. Consume(AS_COMMA);
  1082. curList.concat(Tai_datablock.Create(commname,BuildConstExpression(false,false)));
  1083. if actasmtoken<>AS_SEPARATOR then
  1084. Consume(AS_SEPARATOR);
  1085. end;
  1086. AS_COMM:
  1087. Begin
  1088. Consume(AS_COMM);
  1089. commname:=actasmpattern;
  1090. Consume(AS_ID);
  1091. Consume(AS_COMMA);
  1092. curList.concat(Tai_datablock.Create_global(commname,BuildConstExpression(false,false)));
  1093. if actasmtoken<>AS_SEPARATOR then
  1094. Consume(AS_SEPARATOR);
  1095. end;
  1096. AS_OPCODE:
  1097. Begin
  1098. HandleOpCode;
  1099. end;
  1100. AS_SEPARATOR:
  1101. Begin
  1102. Consume(AS_SEPARATOR);
  1103. end;
  1104. AS_RVA:
  1105. begin
  1106. { .rva generally applies to systems with COFF output format,
  1107. not just Windows. }
  1108. if not (target_info.system in systems_all_windows) then
  1109. Message1(asmr_e_unsupported_directive,token2str[AS_RVA]);
  1110. Consume(AS_RVA);
  1111. BuildRva;
  1112. end;
  1113. AS_SET:
  1114. begin
  1115. Consume(AS_SET);
  1116. BuildConstSymbolExpression(true,false,false, symofs,symname,symtyp);
  1117. Consume(AS_COMMA);
  1118. BuildConstSymbolExpression(true,false,false, symofs,symval,symtyp);
  1119. curList.concat(tai_set.create(symname,symval));
  1120. end;
  1121. AS_WEAK:
  1122. begin
  1123. Consume(AS_WEAK);
  1124. BuildConstSymbolExpression(true,false,false, l1,symname,symtyp);
  1125. curList.concat(tai_weak.create(symname));
  1126. end;
  1127. AS_SECTION:
  1128. begin
  1129. Consume(AS_SECTION);
  1130. new_section(curlist, sec_user, actasmpattern, 0);
  1131. //curList.concat(tai_section.create(sec_user, actasmpattern, 0));
  1132. consume(AS_STRING);
  1133. end;
  1134. AS_TARGET_DIRECTIVE:
  1135. HandleTargetDirective;
  1136. AS_END:
  1137. begin
  1138. break; { end assembly block }
  1139. end;
  1140. else
  1141. Begin
  1142. Message(asmr_e_syntax_error);
  1143. RecoverConsume(false);
  1144. end;
  1145. end;
  1146. until false;
  1147. { Check LocalLabelList }
  1148. LocalLabelList.CheckEmitted;
  1149. LocalLabelList.Free;
  1150. { are we back in the code section? }
  1151. if lasTSec<>sec_code then
  1152. begin
  1153. Message(asmr_w_assembler_code_not_returned_to_text);
  1154. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1155. end;
  1156. { Return the list in an asmnode }
  1157. assemble:=curlist;
  1158. Message1(asmr_d_finish_reading,'GNU AS');
  1159. end;
  1160. {*****************************************************************************
  1161. Parsing Helpers
  1162. *****************************************************************************}
  1163. Procedure tattreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  1164. { Description: This routine builds up a record offset after a AS_DOT }
  1165. { token is encountered. }
  1166. { On entry actasmtoken should be equal to AS_DOT }
  1167. var
  1168. s : string;
  1169. Begin
  1170. offset:=0;
  1171. size:=0;
  1172. s:=expr;
  1173. while (actasmtoken=AS_DOT) do
  1174. begin
  1175. Consume(AS_DOT);
  1176. if actasmtoken=AS_ID then
  1177. s:=s+'.'+actasmpattern;
  1178. if not Consume(AS_ID) then
  1179. begin
  1180. RecoverConsume(true);
  1181. break;
  1182. end;
  1183. end;
  1184. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs) then
  1185. Message(asmr_e_building_record_offset);
  1186. end;
  1187. procedure tattreader.BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  1188. var
  1189. hssymtyp : TAsmSymType;
  1190. hs,tempstr,expr,mangledname : string;
  1191. parenlevel : longint;
  1192. l,k : aint;
  1193. errorflag : boolean;
  1194. prevtok : tasmtoken;
  1195. sym : tsym;
  1196. srsymtable : TSymtable;
  1197. hl : tasmlabel;
  1198. Begin
  1199. asmsym:='';
  1200. asmsymtyp:=AT_DATA;
  1201. value:=0;
  1202. errorflag:=FALSE;
  1203. tempstr:='';
  1204. expr:='';
  1205. parenlevel:=0;
  1206. Repeat
  1207. Case actasmtoken of
  1208. AS_LPAREN:
  1209. Begin
  1210. { Exit if ref? }
  1211. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  1212. break;
  1213. Consume(AS_LPAREN);
  1214. expr:=expr + '(';
  1215. inc(parenlevel);
  1216. end;
  1217. AS_RBRACKET:
  1218. begin
  1219. if betweenbracket then
  1220. break;
  1221. { write error only once. }
  1222. if not errorflag then
  1223. Message(asmr_e_invalid_constant_expression);
  1224. { consume tokens until we find COMMA or SEPARATOR }
  1225. Consume(actasmtoken);
  1226. errorflag:=TRUE;
  1227. end;
  1228. AS_RPAREN:
  1229. Begin
  1230. { end of ref ? }
  1231. if (parenlevel=0) and betweenbracket then
  1232. break;
  1233. Consume(AS_RPAREN);
  1234. expr:=expr + ')';
  1235. dec(parenlevel);
  1236. end;
  1237. AS_SHL:
  1238. Begin
  1239. Consume(AS_SHL);
  1240. expr:=expr + '<';
  1241. end;
  1242. AS_SHR:
  1243. Begin
  1244. Consume(AS_SHR);
  1245. expr:=expr + '>';
  1246. end;
  1247. AS_SLASH:
  1248. Begin
  1249. Consume(AS_SLASH);
  1250. expr:=expr + '/';
  1251. end;
  1252. AS_MOD:
  1253. Begin
  1254. Consume(AS_MOD);
  1255. expr:=expr + '%';
  1256. end;
  1257. AS_STAR:
  1258. Begin
  1259. Consume(AS_STAR);
  1260. expr:=expr + '*';
  1261. end;
  1262. AS_PLUS:
  1263. Begin
  1264. Consume(AS_PLUS);
  1265. expr:=expr + '+';
  1266. end;
  1267. AS_MINUS:
  1268. Begin
  1269. Consume(AS_MINUS);
  1270. expr:=expr + '-';
  1271. end;
  1272. AS_AND:
  1273. Begin
  1274. Consume(AS_AND);
  1275. expr:=expr + '&';
  1276. end;
  1277. AS_NOT:
  1278. Begin
  1279. Consume(AS_NOT);
  1280. expr:=expr + '~';
  1281. end;
  1282. AS_XOR:
  1283. Begin
  1284. Consume(AS_XOR);
  1285. expr:=expr + '^';
  1286. end;
  1287. AS_OR:
  1288. Begin
  1289. Consume(AS_OR);
  1290. expr:=expr + '|';
  1291. end;
  1292. AS_INTNUM:
  1293. Begin
  1294. expr:=expr + actasmpattern;
  1295. Consume(AS_INTNUM);
  1296. end;
  1297. AS_DOLLAR:
  1298. begin
  1299. Consume(AS_DOLLAR);
  1300. if actasmtoken<>AS_ID then
  1301. Message(asmr_e_dollar_without_identifier);
  1302. end;
  1303. AS_STRING:
  1304. Begin
  1305. l:=0;
  1306. case Length(actasmpattern) of
  1307. 1 :
  1308. l:=ord(actasmpattern[1]);
  1309. 2 :
  1310. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1311. 3 :
  1312. l:=ord(actasmpattern[3]) +
  1313. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1314. 4 :
  1315. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1316. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1317. else
  1318. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1319. end;
  1320. str(l, tempstr);
  1321. expr:=expr + tempstr;
  1322. Consume(AS_STRING);
  1323. end;
  1324. AS_SIZEOF,
  1325. AS_TYPE:
  1326. begin
  1327. l:=0;
  1328. Consume(actasmtoken);
  1329. if actasmtoken<>AS_ID then
  1330. Message(asmr_e_type_without_identifier)
  1331. else
  1332. begin
  1333. tempstr:=actasmpattern;
  1334. Consume(AS_ID);
  1335. if actasmtoken=AS_DOT then
  1336. begin
  1337. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  1338. if mangledname<>'' then
  1339. Message(asmr_e_wrong_sym_type);
  1340. end
  1341. else
  1342. begin
  1343. searchsym(tempstr,sym,srsymtable);
  1344. if assigned(sym) then
  1345. begin
  1346. case sym.typ of
  1347. staticvarsym,
  1348. localvarsym,
  1349. paravarsym :
  1350. l:=tabstractvarsym(sym).getsize;
  1351. typesym :
  1352. l:=ttypesym(sym).typedef.size;
  1353. else
  1354. Message(asmr_e_wrong_sym_type);
  1355. end;
  1356. end
  1357. else
  1358. Message1(sym_e_unknown_id,tempstr);
  1359. end;
  1360. end;
  1361. str(l, tempstr);
  1362. expr:=expr + tempstr;
  1363. end;
  1364. AS_VMTOFFSET:
  1365. begin
  1366. Consume(actasmtoken);
  1367. if actasmtoken<>AS_ID then
  1368. Message(asmr_e_type_without_identifier)
  1369. else
  1370. begin
  1371. tempstr:=actasmpattern;
  1372. consume(AS_ID);
  1373. BuildRecordOffsetSize(tempstr,k,l,mangledname,true);
  1374. if (mangledname <> '') then
  1375. Message(asmr_e_wrong_sym_type);
  1376. str(k,tempstr);
  1377. expr := expr + tempstr;
  1378. end
  1379. end;
  1380. AS_ID:
  1381. Begin
  1382. hs:='';
  1383. hssymtyp:=AT_DATA;
  1384. tempstr:=actasmpattern;
  1385. prevtok:=prevasmtoken;
  1386. consume(AS_ID);
  1387. if SearchIConstant(tempstr,l) then
  1388. begin
  1389. str(l, tempstr);
  1390. expr:=expr + tempstr;
  1391. end
  1392. else
  1393. begin
  1394. if is_locallabel(tempstr) then
  1395. begin
  1396. CreateLocalLabel(tempstr,hl,false);
  1397. hs:=hl.name;
  1398. hssymtyp:=AT_LABEL;
  1399. end
  1400. else
  1401. if SearchLabel(tempstr,hl,false) then
  1402. begin
  1403. hs:=hl.name;
  1404. hssymtyp:=AT_FUNCTION;
  1405. end
  1406. else
  1407. begin
  1408. searchsym(tempstr,sym,srsymtable);
  1409. if assigned(sym) then
  1410. begin
  1411. case sym.typ of
  1412. staticvarsym :
  1413. hs:=tstaticvarsym(sym).mangledname;
  1414. localvarsym,
  1415. paravarsym :
  1416. Message(asmr_e_no_local_or_para_allowed);
  1417. procsym :
  1418. begin
  1419. if Tprocsym(sym).ProcdefList.Count>1 then
  1420. Message(asmr_w_calling_overload_func);
  1421. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1422. hssymtyp:=AT_FUNCTION;
  1423. end;
  1424. typesym :
  1425. begin
  1426. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1427. Message(asmr_e_wrong_sym_type);
  1428. end;
  1429. else
  1430. Message(asmr_e_wrong_sym_type);
  1431. end;
  1432. end
  1433. else
  1434. Message1(sym_e_unknown_id,tempstr);
  1435. end;
  1436. { symbol found? }
  1437. if hs<>'' then
  1438. begin
  1439. if needofs and (prevtok<>AS_DOLLAR) then
  1440. Message(asmr_e_need_dollar);
  1441. if asmsym='' then
  1442. begin
  1443. asmsym:=hs;
  1444. asmsymtyp:=hssymtyp;
  1445. end
  1446. else
  1447. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1448. if (expr='') or (expr[length(expr)]='+') then
  1449. begin
  1450. { don't remove the + if there could be a record field }
  1451. if actasmtoken<>AS_DOT then
  1452. delete(expr,length(expr),1);
  1453. end
  1454. else
  1455. Message(asmr_e_only_add_relocatable_symbol);
  1456. end;
  1457. if actasmtoken=AS_DOT then
  1458. begin
  1459. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1460. if (hs<>'') then
  1461. hssymtyp:=AT_FUNCTION
  1462. else
  1463. begin
  1464. str(l, tempstr);
  1465. expr:=expr + tempstr;
  1466. end
  1467. end
  1468. else
  1469. begin
  1470. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1471. delete(expr,length(expr),1);
  1472. end;
  1473. end;
  1474. { check if there are wrong operator used like / or mod etc. }
  1475. if (hs<>'') and
  1476. not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,
  1477. AS_LPAREN,AS_RPAREN,AS_RBRACKET,AS_END]) then
  1478. Message(asmr_e_only_add_relocatable_symbol);
  1479. end;
  1480. AS_END,
  1481. AS_SEPARATOR,
  1482. AS_COMMA:
  1483. break;
  1484. else
  1485. Begin
  1486. { write error only once. }
  1487. if not errorflag then
  1488. Message(asmr_e_invalid_constant_expression);
  1489. { consume tokens until we find COMMA or SEPARATOR }
  1490. Consume(actasmtoken);
  1491. errorflag:=TRUE;
  1492. end;
  1493. end;
  1494. Until false;
  1495. { calculate expression }
  1496. if not ErrorFlag then
  1497. value:=CalculateExpression(expr)
  1498. else
  1499. value:=0;
  1500. end;
  1501. function tattreader.BuildConstExpression(allowref,betweenbracket:boolean): aint;
  1502. var
  1503. l : aint;
  1504. hs : string;
  1505. hssymtyp : TAsmSymType;
  1506. begin
  1507. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs,hssymtyp);
  1508. if hs<>'' then
  1509. Message(asmr_e_relocatable_symbol_not_allowed);
  1510. BuildConstExpression:=l;
  1511. end;
  1512. Procedure tattreader.BuildConstantOperand(oper : toperand);
  1513. var
  1514. l : aint;
  1515. tempstr : string;
  1516. tempsymtyp : TAsmSymType;
  1517. begin
  1518. BuildConstSymbolExpression(false,false,true,l,tempstr,tempsymtyp);
  1519. if tempstr<>'' then
  1520. begin
  1521. oper.opr.typ:=OPR_SYMBOL;
  1522. oper.opr.symofs:=l;
  1523. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr);
  1524. end
  1525. else
  1526. begin
  1527. oper.opr.typ:=OPR_CONSTANT;
  1528. oper.opr.val:=l;
  1529. end;
  1530. end;
  1531. procedure tattreader.BuildRva;
  1532. var
  1533. asmsymtyp : TAsmSymType;
  1534. asmsym: string;
  1535. value : aint;
  1536. ai:tai_const;
  1537. begin
  1538. repeat
  1539. case actasmtoken of
  1540. AS_INTNUM,
  1541. AS_PLUS,
  1542. AS_MINUS,
  1543. AS_LPAREN,
  1544. AS_ID :
  1545. Begin
  1546. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  1547. if asmsym<>'' then
  1548. begin
  1549. ai:=tai_const.create_type_sym(aitconst_rva_symbol,current_asmdata.RefAsmSymbol(asmsym));
  1550. ai.value:=value;
  1551. curlist.concat(ai);
  1552. end
  1553. else
  1554. Message(asmr_e_invalid_symbol_ref);
  1555. end;
  1556. AS_COMMA:
  1557. Consume(AS_COMMA);
  1558. AS_END,
  1559. AS_SEPARATOR:
  1560. break;
  1561. else
  1562. begin
  1563. Message(asmr_e_syn_constant);
  1564. RecoverConsume(false);
  1565. end
  1566. end; { end case }
  1567. until false;
  1568. end;
  1569. end.