raatt.pas 56 KB

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