raatt.pas 59 KB

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