raatt.pas 48 KB

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