raatt.pas 51 KB

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