raatt.pas 48 KB

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