raatt.pas 52 KB

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