raatt.pas 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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. {$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. {$ifdef arm}
  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}
  664. current_scanner.skipcomment(true);
  665. GetToken;
  666. {$endif arm}
  667. exit;
  668. end;
  669. {$ifdef arm}
  670. '}' :
  671. begin
  672. actasmtoken:=AS_RSBRACKET;
  673. c:=current_scanner.asmgetchar;
  674. exit;
  675. end;
  676. '=' :
  677. begin
  678. actasmtoken:=AS_EQUAL;
  679. c:=current_scanner.asmgetchar;
  680. exit;
  681. end;
  682. {$endif arm}
  683. ',' :
  684. begin
  685. actasmtoken:=AS_COMMA;
  686. c:=current_scanner.asmgetchar;
  687. exit;
  688. end;
  689. '<' :
  690. begin
  691. actasmtoken:=AS_SHL;
  692. c:=current_scanner.asmgetchar;
  693. if c = '<' then
  694. c:=current_scanner.asmgetchar;
  695. exit;
  696. end;
  697. '>' :
  698. begin
  699. actasmtoken:=AS_SHL;
  700. c:=current_scanner.asmgetchar;
  701. if c = '>' then
  702. c:=current_scanner.asmgetchar;
  703. exit;
  704. end;
  705. '|' :
  706. begin
  707. actasmtoken:=AS_OR;
  708. c:=current_scanner.asmgetchar;
  709. exit;
  710. end;
  711. '^' :
  712. begin
  713. actasmtoken:=AS_XOR;
  714. c:=current_scanner.asmgetchar;
  715. exit;
  716. end;
  717. '(' :
  718. begin
  719. c:=current_scanner.asmgetchar;
  720. if c='*' then
  721. begin
  722. current_scanner.skipoldtpcomment(true);
  723. GetToken;
  724. end
  725. else
  726. actasmtoken:=AS_LPAREN;
  727. exit;
  728. end;
  729. ')' :
  730. begin
  731. actasmtoken:=AS_RPAREN;
  732. c:=current_scanner.asmgetchar;
  733. exit;
  734. end;
  735. ':' :
  736. begin
  737. actasmtoken:=AS_COLON;
  738. c:=current_scanner.asmgetchar;
  739. exit;
  740. end;
  741. '+' :
  742. begin
  743. actasmtoken:=AS_PLUS;
  744. c:=current_scanner.asmgetchar;
  745. exit;
  746. end;
  747. '-' :
  748. begin
  749. actasmtoken:=AS_MINUS;
  750. c:=current_scanner.asmgetchar;
  751. exit;
  752. end;
  753. '*' :
  754. begin
  755. actasmtoken:=AS_STAR;
  756. c:=current_scanner.asmgetchar;
  757. exit;
  758. end;
  759. '/' :
  760. begin
  761. c:=current_scanner.asmgetchar;
  762. if c='/' then
  763. begin
  764. current_scanner.skipdelphicomment;
  765. GetToken;
  766. end
  767. else
  768. actasmtoken:=AS_SLASH;
  769. exit;
  770. end;
  771. '!', '~' :
  772. begin
  773. actasmtoken:=AS_NOT;
  774. c:=current_scanner.asmgetchar;
  775. exit;
  776. end;
  777. '@' :
  778. begin
  779. actasmtoken:=AS_AT;
  780. c:=current_scanner.asmgetchar;
  781. exit;
  782. end;
  783. #13,#10:
  784. begin
  785. current_scanner.linebreak;
  786. c:=current_scanner.asmgetchar;
  787. firsttoken:=TRUE;
  788. actasmtoken:=AS_SEPARATOR;
  789. exit;
  790. end;
  791. ';' :
  792. begin
  793. c:=current_scanner.asmgetchar;
  794. firsttoken:=TRUE;
  795. actasmtoken:=AS_SEPARATOR;
  796. exit;
  797. end;
  798. else
  799. current_scanner.illegal_char(c);
  800. end;
  801. end;
  802. end;
  803. function tattreader.consume(t : tasmtoken):boolean;
  804. begin
  805. Consume:=true;
  806. if t<>actasmtoken then
  807. begin
  808. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  809. Consume:=false;
  810. end;
  811. repeat
  812. gettoken;
  813. until actasmtoken<>AS_NONE;
  814. end;
  815. procedure tattreader.RecoverConsume(allowcomma:boolean);
  816. begin
  817. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  818. begin
  819. if allowcomma and (actasmtoken=AS_COMMA) then
  820. break;
  821. Consume(actasmtoken);
  822. end;
  823. end;
  824. Procedure tattreader.BuildConstant(constsize: byte);
  825. var
  826. asmsymtyp : TAsmSymType;
  827. asmsym,
  828. expr: string;
  829. value : tcgint;
  830. Begin
  831. Repeat
  832. Case actasmtoken of
  833. AS_STRING:
  834. Begin
  835. expr:=actasmpattern;
  836. if length(expr) > 1 then
  837. Message(asmr_e_string_not_allowed_as_const);
  838. Consume(AS_STRING);
  839. Case actasmtoken of
  840. AS_COMMA: Consume(AS_COMMA);
  841. AS_END,
  842. AS_SEPARATOR: ;
  843. else
  844. Message(asmr_e_invalid_string_expression);
  845. end; { end case }
  846. ConcatString(curlist,expr);
  847. end;
  848. AS_INTNUM,
  849. AS_PLUS,
  850. AS_MINUS,
  851. AS_LPAREN,
  852. AS_TYPE,
  853. AS_SIZEOF,
  854. AS_NOT,
  855. AS_VMTOFFSET,
  856. AS_ID :
  857. Begin
  858. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  859. if asmsym<>'' then
  860. begin
  861. if constsize<>sizeof(pint) then
  862. Message(asmr_w_32bit_const_for_address);
  863. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value,constsize,true)
  864. end
  865. else
  866. ConcatConstant(curlist,value,constsize);
  867. end;
  868. AS_COMMA:
  869. Consume(AS_COMMA);
  870. AS_END,
  871. AS_SEPARATOR:
  872. break;
  873. else
  874. begin
  875. Message(asmr_e_syn_constant);
  876. RecoverConsume(false);
  877. end
  878. end; { end case }
  879. Until false;
  880. end;
  881. Procedure tattreader.BuildRealConstant(typ : tfloattype);
  882. var
  883. expr : string;
  884. r : bestreal;
  885. code : integer;
  886. negativ : boolean;
  887. errorflag: boolean;
  888. Begin
  889. errorflag:=FALSE;
  890. Repeat
  891. negativ:=false;
  892. expr:='';
  893. if actasmtoken=AS_PLUS then
  894. Consume(AS_PLUS)
  895. else
  896. if actasmtoken=AS_MINUS then
  897. begin
  898. negativ:=true;
  899. consume(AS_MINUS);
  900. end;
  901. Case actasmtoken of
  902. AS_INTNUM:
  903. Begin
  904. expr:=actasmpattern;
  905. Consume(AS_INTNUM);
  906. if negativ then
  907. expr:='-'+expr;
  908. val(expr,r,code);
  909. if code<>0 then
  910. Begin
  911. r:=0;
  912. Message(asmr_e_invalid_float_expr);
  913. End;
  914. ConcatRealConstant(curlist,r,typ);
  915. end;
  916. AS_REALNUM:
  917. Begin
  918. expr:=actasmpattern;
  919. Consume(AS_REALNUM);
  920. { in ATT syntax you have 0d in front of the real }
  921. { should this be forced ? yes i think so, as to }
  922. { conform to gas as much as possible. }
  923. if (expr[1]='0') and (upper(expr[2])='D') then
  924. Delete(expr,1,2);
  925. if negativ then
  926. expr:='-'+expr;
  927. val(expr,r,code);
  928. if code<>0 then
  929. Begin
  930. r:=0;
  931. Message(asmr_e_invalid_float_expr);
  932. End;
  933. ConcatRealConstant(curlist,r,typ);
  934. end;
  935. AS_COMMA:
  936. begin
  937. Consume(AS_COMMA);
  938. end;
  939. AS_END,
  940. AS_SEPARATOR:
  941. begin
  942. break;
  943. end;
  944. else
  945. Begin
  946. Consume(actasmtoken);
  947. if not errorflag then
  948. Message(asmr_e_invalid_float_expr);
  949. errorflag:=TRUE;
  950. end;
  951. end;
  952. Until false;
  953. end;
  954. Procedure tattreader.BuildStringConstant(asciiz: boolean);
  955. var
  956. expr: string;
  957. errorflag : boolean;
  958. Begin
  959. errorflag:=FALSE;
  960. Repeat
  961. Case actasmtoken of
  962. AS_STRING:
  963. Begin
  964. expr:=actasmpattern;
  965. if asciiz then
  966. expr:=expr+#0;
  967. ConcatString(curlist,expr);
  968. Consume(AS_STRING);
  969. end;
  970. AS_COMMA:
  971. begin
  972. Consume(AS_COMMA);
  973. end;
  974. AS_END,
  975. AS_SEPARATOR:
  976. begin
  977. break;
  978. end;
  979. else
  980. Begin
  981. Consume(actasmtoken);
  982. if not errorflag then
  983. Message(asmr_e_invalid_string_expression);
  984. errorflag:=TRUE;
  985. end;
  986. end;
  987. Until false;
  988. end;
  989. Function tattreader.Assemble: tlinkedlist;
  990. Var
  991. hl : tasmlabel;
  992. commname,
  993. symname,
  994. symval ,sectionname: string;
  995. lasTSec : TAsmSectiontype;
  996. l1,
  997. l2,
  998. symofs : tcgint;
  999. symtyp : TAsmsymtype;
  1000. section : tai_section;
  1001. secflags : TSectionFlags;
  1002. secprogbits : TSectionProgbits;
  1003. Begin
  1004. Message1(asmr_d_start_reading,'GNU AS');
  1005. firsttoken:=TRUE;
  1006. { sets up all opcode and register tables in uppercase }
  1007. if not _asmsorted then
  1008. Begin
  1009. SetupTables;
  1010. _asmsorted:=TRUE;
  1011. end;
  1012. curlist:=TAsmList.Create;
  1013. { we might need to know which parameters are passed in registers }
  1014. if not parse_generic then
  1015. current_procinfo.generate_parameter_info;
  1016. lasTSec:=sec_code;
  1017. { start tokenizer }
  1018. gettoken;
  1019. { main loop }
  1020. repeat
  1021. case actasmtoken of
  1022. AS_LLABEL:
  1023. Begin
  1024. if CreateLocalLabel(actasmpattern,hl,true) then
  1025. ConcatLabel(curlist,hl);
  1026. Consume(AS_LLABEL);
  1027. end;
  1028. AS_LABEL:
  1029. Begin
  1030. if SearchLabel(upper(actasmpattern),hl,true) then
  1031. begin
  1032. if hl.is_public then
  1033. ConcatPublic(curlist,actasmpattern);
  1034. ConcatLabel(curlist,hl);
  1035. end
  1036. else
  1037. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1038. Consume(AS_LABEL);
  1039. end;
  1040. AS_DW:
  1041. Begin
  1042. Consume(AS_DW);
  1043. BuildConstant(2);
  1044. end;
  1045. AS_DATA:
  1046. Begin
  1047. new_section(curList,sec_data,lower(current_procinfo.procdef.mangledname),0);
  1048. lasTSec:=sec_data;
  1049. Consume(AS_DATA);
  1050. end;
  1051. AS_TEXT:
  1052. Begin
  1053. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1054. lasTSec:=sec_code;
  1055. Consume(AS_TEXT);
  1056. end;
  1057. AS_INIT:
  1058. Begin
  1059. new_section(curList,sec_init,lower(current_procinfo.procdef.mangledname),0);
  1060. lasTSec:=sec_init;
  1061. Consume(AS_INIT);
  1062. end;
  1063. AS_FINI:
  1064. Begin
  1065. new_section(curList,sec_fini,lower(current_procinfo.procdef.mangledname),0);
  1066. lasTSec:=sec_fini;
  1067. Consume(AS_FINI);
  1068. end;
  1069. AS_DB:
  1070. Begin
  1071. Consume(AS_DB);
  1072. BuildConstant(1);
  1073. end;
  1074. AS_DD:
  1075. Begin
  1076. Consume(AS_DD);
  1077. BuildConstant(4);
  1078. end;
  1079. AS_DQ:
  1080. Begin
  1081. Consume(AS_DQ);
  1082. {$ifdef cpu64bitaddr}
  1083. BuildConstant(8);
  1084. {$else cpu64bitaddr}
  1085. BuildRealConstant(s64comp);
  1086. {$endif cpu64bitaddr}
  1087. end;
  1088. AS_SINGLE:
  1089. Begin
  1090. Consume(AS_SINGLE);
  1091. BuildRealConstant(s32real);
  1092. end;
  1093. AS_DOUBLE:
  1094. Begin
  1095. Consume(AS_DOUBLE);
  1096. BuildRealConstant(s64real);
  1097. end;
  1098. AS_EXTENDED:
  1099. Begin
  1100. Consume(AS_EXTENDED);
  1101. BuildRealConstant(s80real);
  1102. end;
  1103. AS_CEXTENDED:
  1104. Begin
  1105. Consume(AS_CEXTENDED);
  1106. BuildRealConstant(sc80real);
  1107. end;
  1108. AS_GLOBAL:
  1109. Begin
  1110. Consume(AS_GLOBAL);
  1111. if actasmtoken=AS_ID then
  1112. ConcatPublic(curlist,actasmpattern);
  1113. Consume(AS_ID);
  1114. if actasmtoken<>AS_SEPARATOR then
  1115. Consume(AS_SEPARATOR);
  1116. end;
  1117. AS_ALIGN:
  1118. Begin
  1119. Consume(AS_ALIGN);
  1120. l1:=BuildConstExpression(false,false);
  1121. if (target_info.system in [system_i386_GO32V2]) then
  1122. begin
  1123. l2:=1;
  1124. if (l1>=0) and (l1<=16) then
  1125. while (l1>0) do
  1126. begin
  1127. l2:=2*l2;
  1128. dec(l1);
  1129. end;
  1130. l1:=l2;
  1131. end;
  1132. ConcatAlign(curlist,l1);
  1133. Message(asmr_n_align_is_target_specific);
  1134. if actasmtoken<>AS_SEPARATOR then
  1135. Consume(AS_SEPARATOR);
  1136. end;
  1137. AS_BALIGN:
  1138. Begin
  1139. Consume(AS_BALIGN);
  1140. ConcatAlign(curlist,BuildConstExpression(false,false));
  1141. if actasmtoken<>AS_SEPARATOR then
  1142. Consume(AS_SEPARATOR);
  1143. end;
  1144. AS_P2ALIGN:
  1145. Begin
  1146. Consume(AS_P2ALIGN);
  1147. l1:=BuildConstExpression(false,false);
  1148. l2:=1;
  1149. if (l1>=0) and (l1<=16) then
  1150. while (l1>0) do
  1151. begin
  1152. l2:=2*l2;
  1153. dec(l1);
  1154. end;
  1155. l1:=l2;
  1156. ConcatAlign(curlist,l1);
  1157. if actasmtoken<>AS_SEPARATOR then
  1158. Consume(AS_SEPARATOR);
  1159. end;
  1160. AS_ASCIIZ:
  1161. Begin
  1162. Consume(AS_ASCIIZ);
  1163. BuildStringConstant(TRUE);
  1164. end;
  1165. AS_ASCII:
  1166. Begin
  1167. Consume(AS_ASCII);
  1168. BuildStringConstant(FALSE);
  1169. end;
  1170. AS_LCOMM:
  1171. Begin
  1172. Consume(AS_LCOMM);
  1173. commname:=actasmpattern;
  1174. Consume(AS_ID);
  1175. Consume(AS_COMMA);
  1176. symofs:=BuildConstExpression(false,false);
  1177. curList.concat(Tai_datablock.Create(commname,symofs,carraydef.getreusable(u8inttype,symofs),AT_DATA));
  1178. if actasmtoken<>AS_SEPARATOR then
  1179. Consume(AS_SEPARATOR);
  1180. end;
  1181. AS_COMM:
  1182. Begin
  1183. Consume(AS_COMM);
  1184. commname:=actasmpattern;
  1185. Consume(AS_ID);
  1186. Consume(AS_COMMA);
  1187. symofs:=BuildConstExpression(false,false);
  1188. curList.concat(Tai_datablock.Create_global(commname,symofs,carraydef.getreusable(u8inttype,symofs),AT_DATA));
  1189. if actasmtoken<>AS_SEPARATOR then
  1190. Consume(AS_SEPARATOR);
  1191. end;
  1192. AS_OPCODE:
  1193. Begin
  1194. HandleOpCode;
  1195. end;
  1196. AS_SEPARATOR:
  1197. Begin
  1198. Consume(AS_SEPARATOR);
  1199. end;
  1200. AS_RVA:
  1201. begin
  1202. { .rva generally applies to systems with COFF output format,
  1203. not just Windows. }
  1204. if not (target_info.system in systems_all_windows) then
  1205. Message1(asmr_e_unsupported_directive,token2str[AS_RVA]);
  1206. Consume(AS_RVA);
  1207. BuildRva;
  1208. end;
  1209. AS_SET:
  1210. begin
  1211. Consume(AS_SET);
  1212. BuildConstSymbolExpression(true,false,false, symofs,symname,symtyp);
  1213. Consume(AS_COMMA);
  1214. BuildConstSymbolExpression(true,false,false, symofs,symval,symtyp);
  1215. curList.concat(tai_symbolpair.create(spk_set,symname,symval));
  1216. end;
  1217. AS_WEAK:
  1218. begin
  1219. Consume(AS_WEAK);
  1220. BuildConstSymbolExpression(true,false,false, l1,symname,symtyp);
  1221. { ideally, we should look up the symbol here (or later) and
  1222. depending on whether it's external or net, generate
  1223. asd_weak_reference or asd_weak_definition -- this is different
  1224. on some targets) }
  1225. curList.concat(tai_directive.create(asd_weak_definition,symname));
  1226. end;
  1227. AS_SECTION:
  1228. begin
  1229. Consume(AS_SECTION);
  1230. sectionname:=actasmpattern;
  1231. secflags:=[];
  1232. secprogbits:=SPB_None;
  1233. Consume(AS_STRING);
  1234. if actasmtoken=AS_COMMA then
  1235. begin
  1236. Consume(AS_COMMA);
  1237. if actasmtoken=AS_STRING then
  1238. begin
  1239. case actasmpattern of
  1240. 'a':
  1241. Include(secflags,SF_A);
  1242. 'w':
  1243. Include(secflags,SF_W);
  1244. 'x':
  1245. Include(secflags,SF_X);
  1246. '':
  1247. ;
  1248. else
  1249. Message(asmr_e_syntax_error);
  1250. end;
  1251. Consume(AS_STRING);
  1252. if actasmtoken=AS_COMMA then
  1253. begin
  1254. Consume(AS_COMMA);
  1255. if (actasmtoken=AS_MOD) or (actasmtoken=AS_AT) then
  1256. begin
  1257. Consume(actasmtoken);
  1258. if actasmtoken=AS_ID then
  1259. begin
  1260. case actasmpattern of
  1261. 'PROGBITS':
  1262. secprogbits:=SPB_PROGBITS;
  1263. 'NOBITS':
  1264. secprogbits:=SPB_NOBITS;
  1265. 'NOTE':
  1266. secprogbits:=SPB_NOTE;
  1267. else
  1268. Message(asmr_e_syntax_error);
  1269. end;
  1270. Consume(AS_ID);
  1271. end
  1272. else
  1273. Message(asmr_e_syntax_error);
  1274. end
  1275. else
  1276. Message(asmr_e_syntax_error);
  1277. end;
  1278. end
  1279. else
  1280. Message(asmr_e_syntax_error);
  1281. end;
  1282. //curList.concat(tai_section.create(sec_user, actasmpattern, 0));
  1283. section:=new_section(curlist, sec_user, sectionname, 0);
  1284. section.secflags:=secflags;
  1285. section.secprogbits:=secprogbits;
  1286. end;
  1287. AS_TARGET_DIRECTIVE:
  1288. HandleTargetDirective;
  1289. AS_END:
  1290. begin
  1291. break; { end assembly block }
  1292. end;
  1293. else
  1294. Begin
  1295. Message(asmr_e_syntax_error);
  1296. RecoverConsume(false);
  1297. end;
  1298. end;
  1299. until false;
  1300. { check that all referenced local labels are defined }
  1301. checklocallabels;
  1302. { are we back in the code section? }
  1303. if lasTSec<>sec_code then
  1304. begin
  1305. Message(asmr_w_assembler_code_not_returned_to_text);
  1306. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1307. end;
  1308. { Return the list in an asmnode }
  1309. assemble:=curlist;
  1310. Message1(asmr_d_finish_reading,'GNU AS');
  1311. end;
  1312. {*****************************************************************************
  1313. Parsing Helpers
  1314. *****************************************************************************}
  1315. Procedure tattreader.BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean);
  1316. { Description: This routine builds up a record offset after a AS_DOT }
  1317. { token is encountered. }
  1318. { On entry actasmtoken should be equal to AS_DOT }
  1319. var
  1320. s : string;
  1321. hastypecast: boolean;
  1322. Begin
  1323. offset:=0;
  1324. size:=0;
  1325. s:=expr;
  1326. while (actasmtoken=AS_DOT) do
  1327. begin
  1328. Consume(AS_DOT);
  1329. { a record field could have the same name as a register }
  1330. if actasmtoken in [AS_ID,AS_REGISTER] then
  1331. begin
  1332. s:=s+'.'+actasmpattern;
  1333. consume(actasmtoken)
  1334. end
  1335. else
  1336. begin
  1337. Consume(AS_ID);
  1338. RecoverConsume(true);
  1339. break;
  1340. end;
  1341. end;
  1342. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  1343. Message(asmr_e_building_record_offset);
  1344. end;
  1345. procedure tattreader.BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  1346. var
  1347. hssymtyp : TAsmSymType;
  1348. hs,tempstr,expr,mangledname : string;
  1349. parenlevel : longint;
  1350. l,k : tcgint;
  1351. errorflag : boolean;
  1352. prevtok : tasmtoken;
  1353. sym : tsym;
  1354. srsymtable : TSymtable;
  1355. hl : tasmlabel;
  1356. Begin
  1357. asmsym:='';
  1358. asmsymtyp:=AT_DATA;
  1359. value:=0;
  1360. errorflag:=FALSE;
  1361. tempstr:='';
  1362. expr:='';
  1363. parenlevel:=0;
  1364. Repeat
  1365. Case actasmtoken of
  1366. AS_LPAREN:
  1367. Begin
  1368. { Exit if ref? }
  1369. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  1370. break;
  1371. Consume(AS_LPAREN);
  1372. expr:=expr + '(';
  1373. inc(parenlevel);
  1374. end;
  1375. AS_RBRACKET:
  1376. begin
  1377. if betweenbracket then
  1378. break;
  1379. { write error only once. }
  1380. if not errorflag then
  1381. Message(asmr_e_invalid_constant_expression);
  1382. { consume tokens until we find COMMA or SEPARATOR }
  1383. Consume(actasmtoken);
  1384. errorflag:=TRUE;
  1385. end;
  1386. AS_RPAREN:
  1387. Begin
  1388. { end of ref ? }
  1389. if (parenlevel=0) and betweenbracket then
  1390. break;
  1391. Consume(AS_RPAREN);
  1392. expr:=expr + ')';
  1393. dec(parenlevel);
  1394. end;
  1395. AS_SHL:
  1396. Begin
  1397. Consume(AS_SHL);
  1398. expr:=expr + '<';
  1399. end;
  1400. AS_SHR:
  1401. Begin
  1402. Consume(AS_SHR);
  1403. expr:=expr + '>';
  1404. end;
  1405. AS_SLASH:
  1406. Begin
  1407. Consume(AS_SLASH);
  1408. expr:=expr + '/';
  1409. end;
  1410. AS_MOD:
  1411. Begin
  1412. Consume(AS_MOD);
  1413. expr:=expr + '%';
  1414. end;
  1415. AS_STAR:
  1416. Begin
  1417. Consume(AS_STAR);
  1418. expr:=expr + '*';
  1419. end;
  1420. AS_PLUS:
  1421. Begin
  1422. Consume(AS_PLUS);
  1423. expr:=expr + '+';
  1424. end;
  1425. AS_MINUS:
  1426. Begin
  1427. Consume(AS_MINUS);
  1428. expr:=expr + '-';
  1429. end;
  1430. AS_AND:
  1431. Begin
  1432. Consume(AS_AND);
  1433. expr:=expr + '&';
  1434. end;
  1435. AS_NOT:
  1436. Begin
  1437. Consume(AS_NOT);
  1438. expr:=expr + '~';
  1439. end;
  1440. AS_XOR:
  1441. Begin
  1442. Consume(AS_XOR);
  1443. expr:=expr + '^';
  1444. end;
  1445. AS_OR:
  1446. Begin
  1447. Consume(AS_OR);
  1448. expr:=expr + '|';
  1449. end;
  1450. AS_INTNUM:
  1451. Begin
  1452. expr:=expr + actasmpattern;
  1453. Consume(AS_INTNUM);
  1454. end;
  1455. AS_DOLLAR:
  1456. begin
  1457. Consume(AS_DOLLAR);
  1458. if actasmtoken<>AS_ID then
  1459. Message(asmr_e_dollar_without_identifier);
  1460. end;
  1461. AS_STRING:
  1462. Begin
  1463. l:=0;
  1464. case Length(actasmpattern) of
  1465. 1 :
  1466. l:=ord(actasmpattern[1]);
  1467. 2 :
  1468. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1469. 3 :
  1470. l:=ord(actasmpattern[3]) +
  1471. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1472. 4 :
  1473. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1474. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1475. else
  1476. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1477. end;
  1478. str(l, tempstr);
  1479. expr:=expr + tempstr;
  1480. Consume(AS_STRING);
  1481. end;
  1482. AS_SIZEOF,
  1483. AS_TYPE:
  1484. begin
  1485. l:=0;
  1486. Consume(actasmtoken);
  1487. if actasmtoken<>AS_ID then
  1488. Message(asmr_e_type_without_identifier)
  1489. else
  1490. begin
  1491. tempstr:=actasmpattern;
  1492. Consume(AS_ID);
  1493. if actasmtoken=AS_DOT then
  1494. begin
  1495. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  1496. if mangledname<>'' then
  1497. Message(asmr_e_wrong_sym_type);
  1498. end
  1499. else
  1500. begin
  1501. searchsym(tempstr,sym,srsymtable);
  1502. if assigned(sym) then
  1503. begin
  1504. case sym.typ of
  1505. staticvarsym,
  1506. localvarsym,
  1507. paravarsym :
  1508. l:=tabstractvarsym(sym).getsize;
  1509. typesym :
  1510. l:=ttypesym(sym).typedef.size;
  1511. else
  1512. Message(asmr_e_wrong_sym_type);
  1513. end;
  1514. end
  1515. else
  1516. Message1(sym_e_unknown_id,tempstr);
  1517. end;
  1518. end;
  1519. str(l, tempstr);
  1520. expr:=expr + tempstr;
  1521. end;
  1522. AS_VMTOFFSET:
  1523. begin
  1524. Consume(actasmtoken);
  1525. if actasmtoken<>AS_ID then
  1526. Message(asmr_e_type_without_identifier)
  1527. else
  1528. begin
  1529. tempstr:=actasmpattern;
  1530. consume(AS_ID);
  1531. BuildRecordOffsetSize(tempstr,k,l,mangledname,true);
  1532. if (mangledname <> '') then
  1533. Message(asmr_e_wrong_sym_type);
  1534. str(k,tempstr);
  1535. expr := expr + tempstr;
  1536. end
  1537. end;
  1538. AS_ID:
  1539. Begin
  1540. hs:='';
  1541. hssymtyp:=AT_DATA;
  1542. tempstr:=actasmpattern;
  1543. prevtok:=prevasmtoken;
  1544. consume(AS_ID);
  1545. if SearchIConstant(tempstr,l) then
  1546. begin
  1547. str(l, tempstr);
  1548. expr:=expr + tempstr;
  1549. end
  1550. else
  1551. begin
  1552. if is_locallabel(tempstr) then
  1553. begin
  1554. CreateLocalLabel(tempstr,hl,false);
  1555. hs:=hl.name;
  1556. hssymtyp:=AT_LABEL;
  1557. end
  1558. else
  1559. if SearchLabel(tempstr,hl,false) then
  1560. begin
  1561. hs:=hl.name;
  1562. hssymtyp:=AT_FUNCTION;
  1563. end
  1564. else
  1565. begin
  1566. searchsym(tempstr,sym,srsymtable);
  1567. if assigned(sym) then
  1568. begin
  1569. case sym.typ of
  1570. staticvarsym :
  1571. begin
  1572. { we always assume in asm statements that }
  1573. { that the variable is valid. }
  1574. tabstractvarsym(sym).varstate:=vs_readwritten;
  1575. inc(tabstractvarsym(sym).refs);
  1576. { variable can't be placed in a register }
  1577. tabstractvarsym(sym).varregable:=vr_none;
  1578. { and anything may happen with its address }
  1579. tabstractvarsym(sym).addr_taken:=true;
  1580. hs:=tstaticvarsym(sym).mangledname;
  1581. end;
  1582. localvarsym,
  1583. paravarsym :
  1584. Message(asmr_e_no_local_or_para_allowed);
  1585. procsym :
  1586. begin
  1587. if Tprocsym(sym).ProcdefList.Count>1 then
  1588. Message(asmr_w_calling_overload_func);
  1589. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1590. hssymtyp:=AT_FUNCTION;
  1591. end;
  1592. typesym :
  1593. begin
  1594. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1595. Message(asmr_e_wrong_sym_type);
  1596. end;
  1597. else
  1598. Message(asmr_e_wrong_sym_type);
  1599. end;
  1600. end
  1601. else
  1602. Message1(sym_e_unknown_id,tempstr);
  1603. end;
  1604. { symbol found? }
  1605. if hs<>'' then
  1606. begin
  1607. if needofs and (prevtok<>AS_DOLLAR) then
  1608. Message(asmr_e_need_dollar);
  1609. if asmsym='' then
  1610. begin
  1611. asmsym:=hs;
  1612. asmsymtyp:=hssymtyp;
  1613. end
  1614. else
  1615. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1616. if (expr='') or (expr[length(expr)]='+') then
  1617. begin
  1618. { don't remove the + if there could be a record field }
  1619. if actasmtoken<>AS_DOT then
  1620. delete(expr,length(expr),1);
  1621. end
  1622. else
  1623. Message(asmr_e_only_add_relocatable_symbol);
  1624. end;
  1625. if actasmtoken=AS_DOT then
  1626. begin
  1627. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1628. if (hs<>'') then
  1629. hssymtyp:=AT_FUNCTION
  1630. else
  1631. begin
  1632. str(l, tempstr);
  1633. expr:=expr + tempstr;
  1634. end
  1635. end
  1636. else
  1637. begin
  1638. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1639. delete(expr,length(expr),1);
  1640. end;
  1641. end;
  1642. { check if there are wrong operator used like / or mod etc. }
  1643. if (hs<>'') and
  1644. not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,
  1645. AS_LPAREN,AS_RPAREN,AS_RBRACKET,AS_END]) then
  1646. Message(asmr_e_only_add_relocatable_symbol);
  1647. end;
  1648. AS_END,
  1649. AS_SEPARATOR,
  1650. AS_COMMA:
  1651. break;
  1652. else
  1653. Begin
  1654. { write error only once. }
  1655. if not errorflag then
  1656. Message(asmr_e_invalid_constant_expression);
  1657. { consume tokens until we find COMMA or SEPARATOR }
  1658. Consume(actasmtoken);
  1659. errorflag:=TRUE;
  1660. end;
  1661. end;
  1662. Until false;
  1663. { calculate expression }
  1664. if not ErrorFlag then
  1665. value:=CalculateExpression(expr)
  1666. else
  1667. value:=0;
  1668. end;
  1669. function tattreader.BuildConstExpression(allowref,betweenbracket:boolean): tcgint;
  1670. var
  1671. l : tcgint;
  1672. hs : string;
  1673. hssymtyp : TAsmSymType;
  1674. begin
  1675. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs,hssymtyp);
  1676. if hs<>'' then
  1677. Message(asmr_e_relocatable_symbol_not_allowed);
  1678. BuildConstExpression:=l;
  1679. end;
  1680. Procedure tattreader.BuildConstantOperand(oper : toperand);
  1681. var
  1682. l : tcgint;
  1683. tempstr : string;
  1684. tempsymtyp : TAsmSymType;
  1685. begin
  1686. BuildConstSymbolExpression(false,false,true,l,tempstr,tempsymtyp);
  1687. if tempstr<>'' then
  1688. begin
  1689. oper.opr.typ:=OPR_SYMBOL;
  1690. oper.opr.symofs:=l;
  1691. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1692. end
  1693. else
  1694. begin
  1695. oper.opr.typ:=OPR_CONSTANT;
  1696. { cast properly to avoid a range check error }
  1697. {$if defined(AVR) or defined(i8086)}
  1698. oper.opr.val:=longint(l);
  1699. {$else}
  1700. oper.opr.val:=aint(l);
  1701. {$endif}
  1702. end;
  1703. end;
  1704. procedure tattreader.BuildRva;
  1705. var
  1706. asmsymtyp : TAsmSymType;
  1707. asmsym: string;
  1708. value : tcgint;
  1709. ai:tai_const;
  1710. begin
  1711. repeat
  1712. case actasmtoken of
  1713. AS_INTNUM,
  1714. AS_PLUS,
  1715. AS_MINUS,
  1716. AS_LPAREN,
  1717. AS_ID :
  1718. Begin
  1719. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  1720. if asmsym<>'' then
  1721. begin
  1722. ai:=tai_const.create_type_sym(aitconst_rva_symbol,current_asmdata.RefAsmSymbol(asmsym,asmsymtyp));
  1723. ai.value:=value;
  1724. curlist.concat(ai);
  1725. end
  1726. else
  1727. Message(asmr_e_invalid_symbol_ref);
  1728. end;
  1729. AS_COMMA:
  1730. Consume(AS_COMMA);
  1731. AS_END,
  1732. AS_SEPARATOR:
  1733. break;
  1734. else
  1735. begin
  1736. Message(asmr_e_syn_constant);
  1737. RecoverConsume(false);
  1738. end
  1739. end; { end case }
  1740. until false;
  1741. end;
  1742. end.