raatt.pas 61 KB

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