2
0

raatt.pas 63 KB

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