raatt.pas 61 KB

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