raatt.pas 49 KB

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