raatt.pas 52 KB

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