raatt.pas 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  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,AS_DC_A,
  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','.dc.a',
  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, ldX.arrangement }
  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','0'..'9']);
  299. end;
  300. end;
  301. {$endif aarch64}
  302. {$if defined(riscv32) or defined(riscv64)}
  303. {
  304. amo* instructions contain a postfix with size, and optionally memory ordering
  305. fence* can contain memory type identifier
  306. floating point instructions contain size, and optionally rounding mode
  307. }
  308. case c of
  309. '.':
  310. begin
  311. repeat
  312. actasmpattern:=actasmpattern+c;
  313. c:=current_scanner.asmgetchar;
  314. until not(c in ['a'..'z','A'..'Z','.']);
  315. end;
  316. end;
  317. {$endif riscv}
  318. {$ifdef xtensa}
  319. {
  320. Xtensa can have multiple postfixes
  321. MULA.DD.LL.LDDEC
  322. or postfixes with numbers
  323. RSR.CCOMPARE2
  324. }
  325. case c of
  326. '.':
  327. begin
  328. repeat
  329. actasmpattern:=actasmpattern+c;
  330. c:=current_scanner.asmgetchar;
  331. until not(c in ['a'..'z','A'..'Z', '0'..'9', '.']);
  332. end;
  333. end;
  334. {$endif xtensa}
  335. { Opcode ? }
  336. If is_asmopcode(upper(actasmpattern)) then
  337. Begin
  338. uppervar(actasmpattern);
  339. exit;
  340. end;
  341. { End of assemblerblock ? }
  342. if upper(actasmpattern) = 'END' then
  343. begin
  344. actasmtoken:=AS_END;
  345. exit;
  346. end;
  347. message1(asmr_e_unknown_opcode,actasmpattern);
  348. actasmtoken:=AS_NONE;
  349. end
  350. else { else firsttoken }
  351. { Here we must handle all possible cases }
  352. begin
  353. case c of
  354. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  355. { - field of object/record }
  356. { - directive. }
  357. begin
  358. if (prevasmtoken in [AS_ID,AS_RPAREN]) then
  359. begin
  360. c:=current_scanner.asmgetchar;
  361. actasmtoken:=AS_DOT;
  362. exit;
  363. end;
  364. actasmpattern:=c;
  365. c:=current_scanner.asmgetchar;
  366. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  367. begin
  368. actasmpattern:=actasmpattern + c;
  369. c:=current_scanner.asmgetchar;
  370. end;
  371. if is_asmdirective(actasmpattern) then
  372. exit;
  373. if is_targetdirective(actasmpattern) then
  374. begin
  375. actasmtoken:=AS_TARGET_DIRECTIVE;
  376. exit;
  377. end;
  378. { local label references and directives }
  379. { are case sensitive }
  380. actasmtoken:=AS_ID;
  381. exit;
  382. end;
  383. { identifier, register, prefix or directive }
  384. '_','A'..'Z','a'..'z':
  385. begin
  386. len:=0;
  387. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  388. begin
  389. inc(len);
  390. actasmpattern[len]:=c;
  391. c:=current_scanner.asmgetchar;
  392. end;
  393. actasmpattern[0]:=chr(len);
  394. uppervar(actasmpattern);
  395. {$ifdef x86}
  396. { only x86 architectures have instruction prefixes }
  397. { Opcode, can only be when the previous was a prefix }
  398. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  399. Begin
  400. uppervar(actasmpattern);
  401. exit;
  402. end;
  403. {$endif x86}
  404. { check for end which is a reserved word unlike the opcodes }
  405. if actasmpattern = 'END' then
  406. Begin
  407. actasmtoken:=AS_END;
  408. exit;
  409. end;
  410. if actasmpattern = 'TYPE' then
  411. Begin
  412. actasmtoken:=AS_TYPE;
  413. exit;
  414. end;
  415. if actasmpattern = 'SIZEOF' then
  416. Begin
  417. actasmtoken:=AS_SIZEOF;
  418. exit;
  419. end;
  420. if actasmpattern = 'VMTOFFSET' then
  421. Begin
  422. actasmtoken:=AS_VMTOFFSET;
  423. exit;
  424. end;
  425. if is_register(actasmpattern) then
  426. begin
  427. actasmtoken:=AS_REGISTER;
  428. exit;
  429. end;
  430. { if next is a '.' and this is a unitsym then we also need to
  431. parse the identifier }
  432. if (c='.') then
  433. begin
  434. searchsym(actasmpattern,srsym,srsymtable);
  435. if assigned(srsym) and
  436. (srsym.typ=unitsym) and
  437. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  438. srsym.owner.iscurrentunit then
  439. begin
  440. actasmpattern:=actasmpattern+c;
  441. c:=current_scanner.asmgetchar;
  442. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  443. begin
  444. actasmpattern:=actasmpattern + upcase(c);
  445. c:=current_scanner.asmgetchar;
  446. end;
  447. end;
  448. end;
  449. actasmtoken:=AS_ID;
  450. exit;
  451. end;
  452. '%' : { register or modulo }
  453. handlepercent;
  454. '1'..'9': { integer number }
  455. begin
  456. len:=0;
  457. while c in ['0'..'9'] do
  458. Begin
  459. inc(len);
  460. actasmpattern[len]:=c;
  461. c:=current_scanner.asmgetchar;
  462. end;
  463. actasmpattern[0]:=chr(len);
  464. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  465. actasmtoken:=AS_INTNUM;
  466. exit;
  467. end;
  468. '0' : { octal,hexa,real or binary number. }
  469. begin
  470. actasmpattern:=c;
  471. c:=current_scanner.asmgetchar;
  472. case upcase(c) of
  473. 'B': { binary }
  474. Begin
  475. c:=current_scanner.asmgetchar;
  476. while c in ['0','1'] do
  477. Begin
  478. actasmpattern:=actasmpattern + c;
  479. c:=current_scanner.asmgetchar;
  480. end;
  481. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  482. actasmtoken:=AS_INTNUM;
  483. exit;
  484. end;
  485. 'D': { real }
  486. Begin
  487. c:=current_scanner.asmgetchar;
  488. { get ridd of the 0d }
  489. if (c in ['+','-']) then
  490. begin
  491. actasmpattern:=c;
  492. c:=current_scanner.asmgetchar;
  493. end
  494. else
  495. actasmpattern:='';
  496. while c in ['0'..'9'] do
  497. Begin
  498. actasmpattern:=actasmpattern + c;
  499. c:=current_scanner.asmgetchar;
  500. end;
  501. if c='.' then
  502. begin
  503. actasmpattern:=actasmpattern + c;
  504. c:=current_scanner.asmgetchar;
  505. while c in ['0'..'9'] do
  506. Begin
  507. actasmpattern:=actasmpattern + c;
  508. c:=current_scanner.asmgetchar;
  509. end;
  510. if upcase(c) = 'E' then
  511. begin
  512. actasmpattern:=actasmpattern + c;
  513. c:=current_scanner.asmgetchar;
  514. if (c in ['+','-']) then
  515. begin
  516. actasmpattern:=actasmpattern + c;
  517. c:=current_scanner.asmgetchar;
  518. end;
  519. while c in ['0'..'9'] do
  520. Begin
  521. actasmpattern:=actasmpattern + c;
  522. c:=current_scanner.asmgetchar;
  523. end;
  524. end;
  525. actasmtoken:=AS_REALNUM;
  526. exit;
  527. end
  528. else
  529. begin
  530. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  531. actasmtoken:=AS_NONE;
  532. end;
  533. end;
  534. 'X': { hexadecimal }
  535. Begin
  536. c:=current_scanner.asmgetchar;
  537. while c in ['0'..'9','a'..'f','A'..'F'] do
  538. Begin
  539. actasmpattern:=actasmpattern + c;
  540. c:=current_scanner.asmgetchar;
  541. end;
  542. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  543. actasmtoken:=AS_INTNUM;
  544. exit;
  545. end;
  546. '1'..'7': { octal }
  547. begin
  548. actasmpattern:=actasmpattern + c;
  549. while c in ['0'..'7'] do
  550. Begin
  551. actasmpattern:=actasmpattern + c;
  552. c:=current_scanner.asmgetchar;
  553. end;
  554. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  555. actasmtoken:=AS_INTNUM;
  556. exit;
  557. end;
  558. else { octal number zero value...}
  559. Begin
  560. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  561. actasmtoken:=AS_INTNUM;
  562. exit;
  563. end;
  564. end; { end case }
  565. end;
  566. '&' :
  567. begin
  568. c:=current_scanner.asmgetchar;
  569. actasmtoken:=AS_AND;
  570. end;
  571. '''' : { char }
  572. begin
  573. actasmpattern:='';
  574. repeat
  575. c:=current_scanner.asmgetchar;
  576. case c of
  577. '\' :
  578. begin
  579. { copy also the next char so \" is parsed correctly }
  580. actasmpattern:=actasmpattern+c;
  581. c:=current_scanner.asmgetchar;
  582. actasmpattern:=actasmpattern+c;
  583. end;
  584. '''' :
  585. begin
  586. c:=current_scanner.asmgetchar;
  587. break;
  588. end;
  589. #10,#13:
  590. Message(scan_f_string_exceeds_line);
  591. else
  592. actasmpattern:=actasmpattern+c;
  593. end;
  594. until false;
  595. actasmpattern:=EscapeToPascal(actasmpattern);
  596. actasmtoken:=AS_STRING;
  597. exit;
  598. end;
  599. '"' : { string }
  600. begin
  601. actasmpattern:='';
  602. repeat
  603. c:=current_scanner.asmgetchar;
  604. case c of
  605. '\' :
  606. begin
  607. { copy also the next char so \" is parsed correctly }
  608. actasmpattern:=actasmpattern+c;
  609. c:=current_scanner.asmgetchar;
  610. actasmpattern:=actasmpattern+c;
  611. end;
  612. '"' :
  613. begin
  614. c:=current_scanner.asmgetchar;
  615. break;
  616. end;
  617. #10,#13:
  618. Message(scan_f_string_exceeds_line);
  619. else
  620. actasmpattern:=actasmpattern+c;
  621. end;
  622. until false;
  623. actasmpattern:=EscapeToPascal(actasmpattern);
  624. actasmtoken:=AS_STRING;
  625. exit;
  626. end;
  627. '$' :
  628. begin
  629. handledollar;
  630. exit;
  631. end;
  632. '#' :
  633. begin
  634. actasmtoken:=AS_HASH;
  635. c:=current_scanner.asmgetchar;
  636. exit;
  637. end;
  638. '[' :
  639. begin
  640. actasmtoken:=AS_LBRACKET;
  641. c:=current_scanner.asmgetchar;
  642. exit;
  643. end;
  644. ']' :
  645. begin
  646. actasmtoken:=AS_RBRACKET;
  647. c:=current_scanner.asmgetchar;
  648. exit;
  649. end;
  650. '{' :
  651. begin
  652. {$if defined(arm) or defined(aarch64)}
  653. // the arm assembler uses { ... } for register sets
  654. // but compiler directives {$... } are still allowed
  655. c:=current_scanner.asmgetchar;
  656. if c<>'$' then
  657. actasmtoken:=AS_LSBRACKET
  658. else
  659. begin
  660. current_scanner.skipcomment(false);
  661. GetToken;
  662. end;
  663. {$else arm or aarch64}
  664. current_scanner.skipcomment(true);
  665. GetToken;
  666. {$endif arm}
  667. exit;
  668. end;
  669. {$if defined(arm) or defined(aarch64)}
  670. '}' :
  671. begin
  672. actasmtoken:=AS_RSBRACKET;
  673. c:=current_scanner.asmgetchar;
  674. exit;
  675. end;
  676. {$endif arm or aarch64}
  677. {$ifdef arm}
  678. '=' :
  679. begin
  680. actasmtoken:=AS_EQUAL;
  681. c:=current_scanner.asmgetchar;
  682. exit;
  683. end;
  684. {$endif arm}
  685. ',' :
  686. begin
  687. actasmtoken:=AS_COMMA;
  688. c:=current_scanner.asmgetchar;
  689. exit;
  690. end;
  691. '<' :
  692. begin
  693. actasmtoken:=AS_SHL;
  694. c:=current_scanner.asmgetchar;
  695. if c = '<' then
  696. c:=current_scanner.asmgetchar;
  697. exit;
  698. end;
  699. '>' :
  700. begin
  701. actasmtoken:=AS_SHL;
  702. c:=current_scanner.asmgetchar;
  703. if c = '>' then
  704. c:=current_scanner.asmgetchar;
  705. exit;
  706. end;
  707. '|' :
  708. begin
  709. actasmtoken:=AS_OR;
  710. c:=current_scanner.asmgetchar;
  711. exit;
  712. end;
  713. '^' :
  714. begin
  715. actasmtoken:=AS_XOR;
  716. c:=current_scanner.asmgetchar;
  717. exit;
  718. end;
  719. '(' :
  720. begin
  721. c:=current_scanner.asmgetchar;
  722. if c='*' then
  723. begin
  724. current_scanner.skipoldtpcomment(true);
  725. GetToken;
  726. end
  727. else
  728. actasmtoken:=AS_LPAREN;
  729. exit;
  730. end;
  731. ')' :
  732. begin
  733. actasmtoken:=AS_RPAREN;
  734. c:=current_scanner.asmgetchar;
  735. exit;
  736. end;
  737. ':' :
  738. begin
  739. actasmtoken:=AS_COLON;
  740. c:=current_scanner.asmgetchar;
  741. exit;
  742. end;
  743. '+' :
  744. begin
  745. actasmtoken:=AS_PLUS;
  746. c:=current_scanner.asmgetchar;
  747. exit;
  748. end;
  749. '-' :
  750. begin
  751. actasmtoken:=AS_MINUS;
  752. c:=current_scanner.asmgetchar;
  753. exit;
  754. end;
  755. '*' :
  756. begin
  757. actasmtoken:=AS_STAR;
  758. c:=current_scanner.asmgetchar;
  759. exit;
  760. end;
  761. '/' :
  762. begin
  763. c:=current_scanner.asmgetchar;
  764. if c='/' then
  765. begin
  766. current_scanner.skipdelphicomment;
  767. GetToken;
  768. end
  769. else
  770. actasmtoken:=AS_SLASH;
  771. exit;
  772. end;
  773. '!', '~' :
  774. begin
  775. actasmtoken:=AS_NOT;
  776. c:=current_scanner.asmgetchar;
  777. exit;
  778. end;
  779. '@' :
  780. begin
  781. actasmtoken:=AS_AT;
  782. c:=current_scanner.asmgetchar;
  783. exit;
  784. end;
  785. #13,#10:
  786. begin
  787. current_scanner.linebreak;
  788. c:=current_scanner.asmgetchar;
  789. firsttoken:=TRUE;
  790. actasmtoken:=AS_SEPARATOR;
  791. exit;
  792. end;
  793. ';' :
  794. begin
  795. c:=current_scanner.asmgetchar;
  796. firsttoken:=TRUE;
  797. actasmtoken:=AS_SEPARATOR;
  798. exit;
  799. end;
  800. else
  801. current_scanner.illegal_char(c);
  802. end;
  803. end;
  804. end;
  805. function tattreader.consume(t : tasmtoken):boolean;
  806. begin
  807. Consume:=true;
  808. if t<>actasmtoken then
  809. begin
  810. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  811. Consume:=false;
  812. end;
  813. repeat
  814. gettoken;
  815. until actasmtoken<>AS_NONE;
  816. end;
  817. procedure tattreader.RecoverConsume(allowcomma:boolean);
  818. begin
  819. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  820. begin
  821. if allowcomma and (actasmtoken=AS_COMMA) then
  822. break;
  823. Consume(actasmtoken);
  824. end;
  825. end;
  826. Procedure tattreader.BuildConstant(constsize: byte);
  827. var
  828. asmsymtyp : TAsmSymType;
  829. asmsym,
  830. expr: string;
  831. value : tcgint;
  832. Begin
  833. Repeat
  834. Case actasmtoken of
  835. AS_STRING:
  836. Begin
  837. expr:=actasmpattern;
  838. if length(expr) > 1 then
  839. Message(asmr_e_string_not_allowed_as_const);
  840. Consume(AS_STRING);
  841. Case actasmtoken of
  842. AS_COMMA: Consume(AS_COMMA);
  843. AS_END,
  844. AS_SEPARATOR: ;
  845. else
  846. Message(asmr_e_invalid_string_expression);
  847. end; { end case }
  848. ConcatString(curlist,expr);
  849. end;
  850. AS_INTNUM,
  851. AS_PLUS,
  852. AS_MINUS,
  853. AS_LPAREN,
  854. AS_TYPE,
  855. AS_SIZEOF,
  856. AS_NOT,
  857. AS_VMTOFFSET,
  858. AS_ID :
  859. Begin
  860. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  861. if asmsym<>'' then
  862. begin
  863. if constsize<>sizeof(pint) then
  864. Message(asmr_w_32bit_const_for_address);
  865. ConcatConstSymbol(curlist,asmsym,'',asmsymtyp,value,constsize,true)
  866. end
  867. else
  868. ConcatConstant(curlist,value,constsize);
  869. end;
  870. AS_COMMA:
  871. Consume(AS_COMMA);
  872. AS_END,
  873. AS_SEPARATOR:
  874. break;
  875. else
  876. begin
  877. Message(asmr_e_syn_constant);
  878. RecoverConsume(false);
  879. end
  880. end; { end case }
  881. Until false;
  882. end;
  883. Procedure tattreader.BuildRealConstant(typ : tfloattype);
  884. var
  885. expr : string;
  886. r : bestreal;
  887. code : integer;
  888. negativ : boolean;
  889. errorflag: boolean;
  890. Begin
  891. errorflag:=FALSE;
  892. Repeat
  893. negativ:=false;
  894. expr:='';
  895. if actasmtoken=AS_PLUS then
  896. Consume(AS_PLUS)
  897. else
  898. if actasmtoken=AS_MINUS then
  899. begin
  900. negativ:=true;
  901. consume(AS_MINUS);
  902. end;
  903. Case actasmtoken of
  904. AS_INTNUM:
  905. Begin
  906. expr:=actasmpattern;
  907. Consume(AS_INTNUM);
  908. if negativ then
  909. expr:='-'+expr;
  910. val(expr,r,code);
  911. if code<>0 then
  912. Begin
  913. r:=0;
  914. Message(asmr_e_invalid_float_expr);
  915. End;
  916. ConcatRealConstant(curlist,r,typ);
  917. end;
  918. AS_REALNUM:
  919. Begin
  920. expr:=actasmpattern;
  921. Consume(AS_REALNUM);
  922. { in ATT syntax you have 0d in front of the real }
  923. { should this be forced ? yes i think so, as to }
  924. { conform to gas as much as possible. }
  925. if (expr[1]='0') and (upper(expr[2])='D') then
  926. Delete(expr,1,2);
  927. if negativ then
  928. expr:='-'+expr;
  929. val(expr,r,code);
  930. if code<>0 then
  931. Begin
  932. r:=0;
  933. Message(asmr_e_invalid_float_expr);
  934. End;
  935. ConcatRealConstant(curlist,r,typ);
  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_float_expr);
  951. errorflag:=TRUE;
  952. end;
  953. end;
  954. Until false;
  955. end;
  956. Procedure tattreader.BuildStringConstant(asciiz: boolean);
  957. var
  958. expr: string;
  959. errorflag : boolean;
  960. Begin
  961. errorflag:=FALSE;
  962. Repeat
  963. Case actasmtoken of
  964. AS_STRING:
  965. Begin
  966. expr:=actasmpattern;
  967. if asciiz then
  968. expr:=expr+#0;
  969. ConcatString(curlist,expr);
  970. Consume(AS_STRING);
  971. end;
  972. AS_COMMA:
  973. begin
  974. Consume(AS_COMMA);
  975. end;
  976. AS_END,
  977. AS_SEPARATOR:
  978. begin
  979. break;
  980. end;
  981. else
  982. Begin
  983. Consume(actasmtoken);
  984. if not errorflag then
  985. Message(asmr_e_invalid_string_expression);
  986. errorflag:=TRUE;
  987. end;
  988. end;
  989. Until false;
  990. end;
  991. Function tattreader.Assemble: tlinkedlist;
  992. Var
  993. hl : tasmlabel;
  994. commname,
  995. symname,
  996. symval ,sectionname: string;
  997. lasTSec : TAsmSectiontype;
  998. l1,
  999. l2,
  1000. l3,
  1001. l4,
  1002. symofs : tcgint;
  1003. symtyp : TAsmsymtype;
  1004. section : tai_section;
  1005. secflags : TSectionFlags;
  1006. secprogbits : TSectionProgbits;
  1007. i: Integer;
  1008. Begin
  1009. Message1(asmr_d_start_reading,'GNU AS');
  1010. firsttoken:=TRUE;
  1011. { sets up all opcode and register tables in uppercase }
  1012. if not _asmsorted then
  1013. Begin
  1014. SetupTables;
  1015. _asmsorted:=TRUE;
  1016. end;
  1017. curlist:=TAsmList.Create;
  1018. { we might need to know which parameters are passed in registers }
  1019. if not parse_generic then
  1020. current_procinfo.generate_parameter_info;
  1021. lasTSec:=sec_code;
  1022. { start tokenizer }
  1023. gettoken;
  1024. { main loop }
  1025. repeat
  1026. case actasmtoken of
  1027. AS_LLABEL:
  1028. Begin
  1029. if CreateLocalLabel(actasmpattern,hl,true) then
  1030. ConcatLabel(curlist,hl);
  1031. Consume(AS_LLABEL);
  1032. end;
  1033. AS_LABEL:
  1034. Begin
  1035. if SearchLabel(upper(actasmpattern),hl,true) then
  1036. begin
  1037. if hl.is_public then
  1038. ConcatPublic(curlist,actasmpattern);
  1039. ConcatLabel(curlist,hl);
  1040. end
  1041. else
  1042. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1043. Consume(AS_LABEL);
  1044. end;
  1045. AS_DW:
  1046. Begin
  1047. Consume(AS_DW);
  1048. BuildConstant(2);
  1049. end;
  1050. AS_DATA:
  1051. Begin
  1052. new_section(curList,sec_data,lower(current_procinfo.procdef.mangledname),0);
  1053. lasTSec:=sec_data;
  1054. Consume(AS_DATA);
  1055. end;
  1056. AS_TEXT:
  1057. Begin
  1058. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1059. lasTSec:=sec_code;
  1060. Consume(AS_TEXT);
  1061. end;
  1062. AS_INIT:
  1063. Begin
  1064. new_section(curList,sec_init,lower(current_procinfo.procdef.mangledname),0);
  1065. lasTSec:=sec_init;
  1066. Consume(AS_INIT);
  1067. end;
  1068. AS_FINI:
  1069. Begin
  1070. new_section(curList,sec_fini,lower(current_procinfo.procdef.mangledname),0);
  1071. lasTSec:=sec_fini;
  1072. Consume(AS_FINI);
  1073. end;
  1074. AS_DB:
  1075. Begin
  1076. Consume(AS_DB);
  1077. BuildConstant(1);
  1078. end;
  1079. AS_DD:
  1080. Begin
  1081. Consume(AS_DD);
  1082. BuildConstant(4);
  1083. end;
  1084. AS_DC_A:
  1085. Begin
  1086. Consume(AS_DC_A);
  1087. BuildConstant(sizeof(aint));
  1088. end;
  1089. AS_DQ:
  1090. Begin
  1091. Consume(AS_DQ);
  1092. {$ifdef cpu64bitaddr}
  1093. BuildConstant(8);
  1094. {$else cpu64bitaddr}
  1095. BuildRealConstant(s64comp);
  1096. {$endif cpu64bitaddr}
  1097. end;
  1098. AS_SINGLE:
  1099. Begin
  1100. Consume(AS_SINGLE);
  1101. BuildRealConstant(s32real);
  1102. end;
  1103. AS_DOUBLE:
  1104. Begin
  1105. Consume(AS_DOUBLE);
  1106. BuildRealConstant(s64real);
  1107. end;
  1108. AS_EXTENDED:
  1109. Begin
  1110. Consume(AS_EXTENDED);
  1111. BuildRealConstant(s80real);
  1112. end;
  1113. AS_CEXTENDED:
  1114. Begin
  1115. Consume(AS_CEXTENDED);
  1116. BuildRealConstant(sc80real);
  1117. end;
  1118. AS_GLOBAL:
  1119. Begin
  1120. Consume(AS_GLOBAL);
  1121. if actasmtoken=AS_ID then
  1122. ConcatPublic(curlist,actasmpattern);
  1123. Consume(AS_ID);
  1124. if actasmtoken<>AS_SEPARATOR then
  1125. Consume(AS_SEPARATOR);
  1126. end;
  1127. AS_ALIGN:
  1128. Begin
  1129. Consume(AS_ALIGN);
  1130. l1:=BuildConstExpression(false,false);
  1131. if (target_info.system in [system_i386_GO32V2]) then
  1132. begin
  1133. l2:=1;
  1134. if (l1>=0) and (l1<=16) then
  1135. while (l1>0) do
  1136. begin
  1137. l2:=2*l2;
  1138. dec(l1);
  1139. end;
  1140. l1:=l2;
  1141. end;
  1142. ConcatAlign(curlist,l1);
  1143. Message(asmr_n_align_is_target_specific);
  1144. if actasmtoken<>AS_SEPARATOR then
  1145. Consume(AS_SEPARATOR);
  1146. end;
  1147. AS_BALIGN:
  1148. Begin
  1149. Consume(AS_BALIGN);
  1150. ConcatAlign(curlist,BuildConstExpression(false,false));
  1151. if actasmtoken<>AS_SEPARATOR then
  1152. Consume(AS_SEPARATOR);
  1153. end;
  1154. AS_P2ALIGN:
  1155. Begin
  1156. Consume(AS_P2ALIGN);
  1157. l1:=BuildConstExpression(false,false);
  1158. l2:=1;
  1159. if (l1>=0) and (l1<=16) then
  1160. while (l1>0) do
  1161. begin
  1162. l2:=2*l2;
  1163. dec(l1);
  1164. end;
  1165. l1:=l2;
  1166. if actasmtoken=AS_COMMA then
  1167. begin
  1168. Consume(AS_COMMA);
  1169. if not(actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1170. begin
  1171. l3:=BuildConstExpression(false,false);
  1172. if (l3<0) or (l3>255) then
  1173. Message(asmr_e_invalid_constant_expression);
  1174. if actasmtoken=AS_COMMA then
  1175. begin
  1176. Consume(AS_COMMA);
  1177. l4:=BuildConstExpression(false,false);
  1178. if (l4<0) or (l4>l1) then
  1179. Message(asmr_e_invalid_constant_expression);
  1180. curlist.concat(Tai_align.create_op_max(l1,l3,l4));
  1181. end
  1182. end
  1183. else if actasmtoken=AS_COMMA then
  1184. begin
  1185. Consume(AS_COMMA);
  1186. l4:=BuildConstExpression(false,false);
  1187. if (l4<0) or (l4>l1) then
  1188. Message(asmr_e_invalid_constant_expression);
  1189. curlist.concat(Tai_align.create_max(l1,l4));
  1190. end
  1191. end
  1192. else
  1193. ConcatAlign(curlist,l1);
  1194. if actasmtoken<>AS_SEPARATOR then
  1195. Consume(AS_SEPARATOR);
  1196. end;
  1197. AS_ASCIIZ:
  1198. Begin
  1199. Consume(AS_ASCIIZ);
  1200. BuildStringConstant(TRUE);
  1201. end;
  1202. AS_ASCII:
  1203. Begin
  1204. Consume(AS_ASCII);
  1205. BuildStringConstant(FALSE);
  1206. end;
  1207. AS_LCOMM:
  1208. Begin
  1209. Consume(AS_LCOMM);
  1210. commname:=actasmpattern;
  1211. Consume(AS_ID);
  1212. Consume(AS_COMMA);
  1213. symofs:=BuildConstExpression(false,false);
  1214. curList.concat(Tai_datablock.Create(commname,symofs,carraydef.getreusable(u8inttype,symofs),AT_DATA));
  1215. if actasmtoken<>AS_SEPARATOR then
  1216. Consume(AS_SEPARATOR);
  1217. end;
  1218. AS_COMM:
  1219. Begin
  1220. Consume(AS_COMM);
  1221. commname:=actasmpattern;
  1222. Consume(AS_ID);
  1223. Consume(AS_COMMA);
  1224. symofs:=BuildConstExpression(false,false);
  1225. curList.concat(Tai_datablock.Create_global(commname,symofs,carraydef.getreusable(u8inttype,symofs),AT_DATA));
  1226. if actasmtoken<>AS_SEPARATOR then
  1227. Consume(AS_SEPARATOR);
  1228. end;
  1229. AS_OPCODE:
  1230. Begin
  1231. HandleOpCode;
  1232. end;
  1233. AS_SEPARATOR:
  1234. Begin
  1235. Consume(AS_SEPARATOR);
  1236. end;
  1237. AS_RVA:
  1238. begin
  1239. { .rva generally applies to systems with COFF output format,
  1240. not just Windows. }
  1241. if not (target_info.system in systems_all_windows) then
  1242. Message1(asmr_e_unsupported_directive,token2str[AS_RVA]);
  1243. Consume(AS_RVA);
  1244. BuildRva;
  1245. end;
  1246. AS_SET:
  1247. begin
  1248. Consume(AS_SET);
  1249. BuildConstSymbolExpression(true,false,false, symofs,symname,symtyp);
  1250. Consume(AS_COMMA);
  1251. BuildConstSymbolExpression(true,false,false, symofs,symval,symtyp);
  1252. curList.concat(tai_symbolpair.create(spk_set,symname,symval));
  1253. end;
  1254. AS_WEAK:
  1255. begin
  1256. Consume(AS_WEAK);
  1257. BuildConstSymbolExpression(true,false,false, l1,symname,symtyp);
  1258. { ideally, we should look up the symbol here (or later) and
  1259. depending on whether it's external or net, generate
  1260. asd_weak_reference or asd_weak_definition -- this is different
  1261. on some targets) }
  1262. curList.concat(tai_directive.create(asd_weak_definition,symname));
  1263. end;
  1264. AS_SECTION:
  1265. begin
  1266. Consume(AS_SECTION);
  1267. sectionname:=actasmpattern;
  1268. secflags:=[];
  1269. secprogbits:=SPB_None;
  1270. Consume(AS_STRING);
  1271. if actasmtoken=AS_COMMA then
  1272. begin
  1273. Consume(AS_COMMA);
  1274. if actasmtoken=AS_STRING then
  1275. begin
  1276. for i:=1 to length(actasmpattern) do
  1277. case actasmpattern[i] of
  1278. 'a':
  1279. Include(secflags,SF_A);
  1280. 'w':
  1281. Include(secflags,SF_W);
  1282. 'x':
  1283. Include(secflags,SF_X);
  1284. else
  1285. Message(asmr_e_syntax_error);
  1286. end;
  1287. Consume(AS_STRING);
  1288. if actasmtoken=AS_COMMA then
  1289. begin
  1290. Consume(AS_COMMA);
  1291. if (actasmtoken=AS_MOD) or (actasmtoken=AS_AT) then
  1292. begin
  1293. Consume(actasmtoken);
  1294. if actasmtoken=AS_ID then
  1295. begin
  1296. case actasmpattern of
  1297. 'PROGBITS':
  1298. secprogbits:=SPB_PROGBITS;
  1299. 'NOBITS':
  1300. secprogbits:=SPB_NOBITS;
  1301. 'NOTE':
  1302. secprogbits:=SPB_NOTE;
  1303. else
  1304. Message(asmr_e_syntax_error);
  1305. end;
  1306. Consume(AS_ID);
  1307. end
  1308. else
  1309. Message(asmr_e_syntax_error);
  1310. end
  1311. else
  1312. Message(asmr_e_syntax_error);
  1313. end;
  1314. end
  1315. else
  1316. Message(asmr_e_syntax_error);
  1317. end;
  1318. //curList.concat(tai_section.create(sec_user, actasmpattern, 0));
  1319. section:=new_section(curlist, sec_user, sectionname, 0);
  1320. section.secflags:=secflags;
  1321. section.secprogbits:=secprogbits;
  1322. end;
  1323. AS_TARGET_DIRECTIVE:
  1324. HandleTargetDirective;
  1325. AS_END:
  1326. begin
  1327. break; { end assembly block }
  1328. end;
  1329. else
  1330. Begin
  1331. Message(asmr_e_syntax_error);
  1332. RecoverConsume(false);
  1333. end;
  1334. end;
  1335. until false;
  1336. { check that all referenced local labels are defined }
  1337. checklocallabels;
  1338. { are we back in the code section? }
  1339. if lasTSec<>sec_code then
  1340. begin
  1341. Message(asmr_w_assembler_code_not_returned_to_text);
  1342. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1343. end;
  1344. { Return the list in an asmnode }
  1345. assemble:=curlist;
  1346. Message1(asmr_d_finish_reading,'GNU AS');
  1347. end;
  1348. {*****************************************************************************
  1349. Parsing Helpers
  1350. *****************************************************************************}
  1351. Procedure tattreader.BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean);
  1352. { Description: This routine builds up a record offset after a AS_DOT }
  1353. { token is encountered. }
  1354. { On entry actasmtoken should be equal to AS_DOT }
  1355. var
  1356. s : string;
  1357. hastypecast: boolean;
  1358. Begin
  1359. offset:=0;
  1360. size:=0;
  1361. s:=expr;
  1362. while (actasmtoken=AS_DOT) do
  1363. begin
  1364. Consume(AS_DOT);
  1365. { a record field could have the same name as a register }
  1366. if actasmtoken in [AS_ID,AS_REGISTER] then
  1367. begin
  1368. s:=s+'.'+actasmpattern;
  1369. consume(actasmtoken)
  1370. end
  1371. else
  1372. begin
  1373. Consume(AS_ID);
  1374. RecoverConsume(true);
  1375. break;
  1376. end;
  1377. end;
  1378. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  1379. Message(asmr_e_building_record_offset);
  1380. end;
  1381. procedure tattreader.BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  1382. var
  1383. hssymtyp : TAsmSymType;
  1384. hs,tempstr,expr,mangledname : string;
  1385. parenlevel : longint;
  1386. l,k : tcgint;
  1387. errorflag : boolean;
  1388. prevtok : tasmtoken;
  1389. sym : tsym;
  1390. srsymtable : TSymtable;
  1391. hl : tasmlabel;
  1392. Begin
  1393. asmsym:='';
  1394. asmsymtyp:=AT_DATA;
  1395. value:=0;
  1396. errorflag:=FALSE;
  1397. tempstr:='';
  1398. expr:='';
  1399. parenlevel:=0;
  1400. Repeat
  1401. Case actasmtoken of
  1402. AS_LPAREN:
  1403. Begin
  1404. { Exit if ref? }
  1405. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  1406. break;
  1407. Consume(AS_LPAREN);
  1408. expr:=expr + '(';
  1409. inc(parenlevel);
  1410. end;
  1411. AS_RBRACKET:
  1412. begin
  1413. if betweenbracket then
  1414. break;
  1415. { write error only once. }
  1416. if not errorflag then
  1417. Message(asmr_e_invalid_constant_expression);
  1418. { consume tokens until we find COMMA or SEPARATOR }
  1419. Consume(actasmtoken);
  1420. errorflag:=TRUE;
  1421. end;
  1422. AS_RPAREN:
  1423. Begin
  1424. { end of ref ? }
  1425. if (parenlevel=0) and betweenbracket then
  1426. break;
  1427. Consume(AS_RPAREN);
  1428. expr:=expr + ')';
  1429. dec(parenlevel);
  1430. end;
  1431. AS_SHL:
  1432. Begin
  1433. Consume(AS_SHL);
  1434. expr:=expr + '<';
  1435. end;
  1436. AS_SHR:
  1437. Begin
  1438. Consume(AS_SHR);
  1439. expr:=expr + '>';
  1440. end;
  1441. AS_SLASH:
  1442. Begin
  1443. Consume(AS_SLASH);
  1444. expr:=expr + '/';
  1445. end;
  1446. AS_MOD:
  1447. Begin
  1448. Consume(AS_MOD);
  1449. expr:=expr + '%';
  1450. end;
  1451. AS_STAR:
  1452. Begin
  1453. Consume(AS_STAR);
  1454. expr:=expr + '*';
  1455. end;
  1456. AS_PLUS:
  1457. Begin
  1458. Consume(AS_PLUS);
  1459. expr:=expr + '+';
  1460. end;
  1461. AS_MINUS:
  1462. Begin
  1463. Consume(AS_MINUS);
  1464. expr:=expr + '-';
  1465. end;
  1466. AS_AND:
  1467. Begin
  1468. Consume(AS_AND);
  1469. expr:=expr + '&';
  1470. end;
  1471. AS_NOT:
  1472. Begin
  1473. Consume(AS_NOT);
  1474. expr:=expr + '~';
  1475. end;
  1476. AS_XOR:
  1477. Begin
  1478. Consume(AS_XOR);
  1479. expr:=expr + '^';
  1480. end;
  1481. AS_OR:
  1482. Begin
  1483. Consume(AS_OR);
  1484. expr:=expr + '|';
  1485. end;
  1486. AS_INTNUM:
  1487. Begin
  1488. expr:=expr + actasmpattern;
  1489. Consume(AS_INTNUM);
  1490. end;
  1491. AS_DOLLAR:
  1492. begin
  1493. Consume(AS_DOLLAR);
  1494. if actasmtoken<>AS_ID then
  1495. Message(asmr_e_dollar_without_identifier);
  1496. end;
  1497. AS_STRING:
  1498. Begin
  1499. l:=0;
  1500. case Length(actasmpattern) of
  1501. 1 :
  1502. l:=ord(actasmpattern[1]);
  1503. 2 :
  1504. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1505. 3 :
  1506. l:=ord(actasmpattern[3]) +
  1507. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1508. 4 :
  1509. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1510. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1511. 8 :
  1512. begin
  1513. move(actasmpattern[1],l,8);
  1514. l:=SwapEndian(l);
  1515. end;
  1516. else
  1517. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1518. end;
  1519. str(l, tempstr);
  1520. expr:=expr + tempstr;
  1521. Consume(AS_STRING);
  1522. end;
  1523. AS_SIZEOF,
  1524. AS_TYPE:
  1525. begin
  1526. l:=0;
  1527. Consume(actasmtoken);
  1528. if actasmtoken<>AS_ID then
  1529. Message(asmr_e_type_without_identifier)
  1530. else
  1531. begin
  1532. tempstr:=actasmpattern;
  1533. Consume(AS_ID);
  1534. if actasmtoken=AS_DOT then
  1535. begin
  1536. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  1537. if mangledname<>'' then
  1538. Message(asmr_e_wrong_sym_type);
  1539. end
  1540. else
  1541. begin
  1542. searchsym(tempstr,sym,srsymtable);
  1543. if assigned(sym) then
  1544. begin
  1545. case sym.typ of
  1546. staticvarsym,
  1547. localvarsym,
  1548. paravarsym :
  1549. l:=tabstractvarsym(sym).getsize;
  1550. typesym :
  1551. l:=ttypesym(sym).typedef.size;
  1552. else
  1553. Message(asmr_e_wrong_sym_type);
  1554. end;
  1555. end
  1556. else
  1557. Message1(sym_e_unknown_id,tempstr);
  1558. end;
  1559. end;
  1560. str(l, tempstr);
  1561. expr:=expr + tempstr;
  1562. end;
  1563. AS_VMTOFFSET:
  1564. begin
  1565. Consume(actasmtoken);
  1566. if actasmtoken<>AS_ID then
  1567. Message(asmr_e_type_without_identifier)
  1568. else
  1569. begin
  1570. tempstr:=actasmpattern;
  1571. consume(AS_ID);
  1572. BuildRecordOffsetSize(tempstr,k,l,mangledname,true);
  1573. if (mangledname <> '') then
  1574. Message(asmr_e_wrong_sym_type);
  1575. str(k,tempstr);
  1576. expr := expr + tempstr;
  1577. end
  1578. end;
  1579. AS_ID:
  1580. Begin
  1581. hs:='';
  1582. hssymtyp:=AT_DATA;
  1583. tempstr:=actasmpattern;
  1584. prevtok:=prevasmtoken;
  1585. consume(AS_ID);
  1586. if SearchIConstant(tempstr,l) then
  1587. begin
  1588. str(l, tempstr);
  1589. expr:=expr + tempstr;
  1590. end
  1591. else
  1592. begin
  1593. if is_locallabel(tempstr) then
  1594. begin
  1595. CreateLocalLabel(tempstr,hl,false);
  1596. hs:=hl.name;
  1597. hssymtyp:=AT_LABEL;
  1598. end
  1599. else
  1600. if SearchLabel(tempstr,hl,false) then
  1601. begin
  1602. hs:=hl.name;
  1603. hssymtyp:=AT_FUNCTION;
  1604. end
  1605. else
  1606. begin
  1607. searchsym(tempstr,sym,srsymtable);
  1608. if assigned(sym) then
  1609. begin
  1610. case sym.typ of
  1611. staticvarsym :
  1612. begin
  1613. { we always assume in asm statements that }
  1614. { that the variable is valid. }
  1615. tabstractvarsym(sym).varstate:=vs_readwritten;
  1616. inc(tabstractvarsym(sym).refs);
  1617. { variable can't be placed in a register }
  1618. tabstractvarsym(sym).varregable:=vr_none;
  1619. { and anything may happen with its address }
  1620. tabstractvarsym(sym).addr_taken:=true;
  1621. hs:=tstaticvarsym(sym).mangledname;
  1622. end;
  1623. localvarsym,
  1624. paravarsym :
  1625. Message(asmr_e_no_local_or_para_allowed);
  1626. procsym :
  1627. begin
  1628. if Tprocsym(sym).ProcdefList.Count>1 then
  1629. Message(asmr_w_calling_overload_func);
  1630. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1631. hssymtyp:=AT_FUNCTION;
  1632. end;
  1633. typesym :
  1634. begin
  1635. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1636. Message(asmr_e_wrong_sym_type);
  1637. end;
  1638. else
  1639. Message(asmr_e_wrong_sym_type);
  1640. end;
  1641. end
  1642. else
  1643. Message1(sym_e_unknown_id,tempstr);
  1644. end;
  1645. { symbol found? }
  1646. if hs<>'' then
  1647. begin
  1648. if needofs and (prevtok<>AS_DOLLAR) then
  1649. Message(asmr_e_need_dollar);
  1650. if asmsym='' then
  1651. begin
  1652. asmsym:=hs;
  1653. asmsymtyp:=hssymtyp;
  1654. end
  1655. else
  1656. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1657. if (expr='') or (expr[length(expr)]='+') then
  1658. begin
  1659. { don't remove the + if there could be a record field }
  1660. if actasmtoken<>AS_DOT then
  1661. delete(expr,length(expr),1);
  1662. end
  1663. else
  1664. Message(asmr_e_only_add_relocatable_symbol);
  1665. end;
  1666. if actasmtoken=AS_DOT then
  1667. begin
  1668. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1669. if (hs<>'') then
  1670. hssymtyp:=AT_FUNCTION
  1671. else
  1672. begin
  1673. str(l, tempstr);
  1674. expr:=expr + tempstr;
  1675. end
  1676. end
  1677. else
  1678. begin
  1679. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1680. delete(expr,length(expr),1);
  1681. end;
  1682. end;
  1683. { check if there are wrong operator used like / or mod etc. }
  1684. if (hs<>'') and
  1685. not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,
  1686. AS_LPAREN,AS_RPAREN,AS_RBRACKET,AS_END]) then
  1687. Message(asmr_e_only_add_relocatable_symbol);
  1688. end;
  1689. AS_END,
  1690. AS_SEPARATOR,
  1691. AS_COMMA:
  1692. break;
  1693. else
  1694. Begin
  1695. { write error only once. }
  1696. if not errorflag then
  1697. Message(asmr_e_invalid_constant_expression);
  1698. { consume tokens until we find COMMA or SEPARATOR }
  1699. Consume(actasmtoken);
  1700. errorflag:=TRUE;
  1701. end;
  1702. end;
  1703. Until false;
  1704. { calculate expression }
  1705. if not ErrorFlag then
  1706. value:=CalculateExpression(expr)
  1707. else
  1708. value:=0;
  1709. end;
  1710. function tattreader.BuildConstExpression(allowref,betweenbracket:boolean): tcgint;
  1711. var
  1712. l : tcgint;
  1713. hs : string;
  1714. hssymtyp : TAsmSymType;
  1715. begin
  1716. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs,hssymtyp);
  1717. if hs<>'' then
  1718. Message(asmr_e_relocatable_symbol_not_allowed);
  1719. BuildConstExpression:=l;
  1720. end;
  1721. Procedure tattreader.BuildConstantOperand(oper : toperand);
  1722. var
  1723. l : tcgint;
  1724. tempstr : string;
  1725. tempsymtyp : TAsmSymType;
  1726. begin
  1727. BuildConstSymbolExpression(false,false,true,l,tempstr,tempsymtyp);
  1728. if tempstr<>'' then
  1729. begin
  1730. oper.opr.typ:=OPR_SYMBOL;
  1731. oper.opr.symofs:=l;
  1732. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1733. end
  1734. else
  1735. begin
  1736. oper.opr.typ:=OPR_CONSTANT;
  1737. { cast properly to avoid a range check error }
  1738. {$if defined(AVR) or defined(i8086)}
  1739. oper.opr.val:=longint(l);
  1740. {$else}
  1741. oper.opr.val:=aint(l);
  1742. {$endif}
  1743. end;
  1744. end;
  1745. procedure tattreader.BuildRva;
  1746. var
  1747. asmsymtyp : TAsmSymType;
  1748. asmsym: string;
  1749. value : tcgint;
  1750. ai:tai_const;
  1751. begin
  1752. repeat
  1753. case actasmtoken of
  1754. AS_INTNUM,
  1755. AS_PLUS,
  1756. AS_MINUS,
  1757. AS_LPAREN,
  1758. AS_ID :
  1759. Begin
  1760. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  1761. if asmsym<>'' then
  1762. begin
  1763. ai:=tai_const.create_type_sym(aitconst_rva_symbol,current_asmdata.RefAsmSymbol(asmsym,asmsymtyp));
  1764. ai.value:=value;
  1765. curlist.concat(ai);
  1766. end
  1767. else
  1768. Message(asmr_e_invalid_symbol_ref);
  1769. end;
  1770. AS_COMMA:
  1771. Consume(AS_COMMA);
  1772. AS_END,
  1773. AS_SEPARATOR:
  1774. break;
  1775. else
  1776. begin
  1777. Message(asmr_e_syn_constant);
  1778. RecoverConsume(false);
  1779. end
  1780. end; { end case }
  1781. until false;
  1782. end;
  1783. end.