raatt.pas 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  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_LO,AS_HI,
  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','%','<<','>>','!','&','|','^','~','@','lo','hi',
  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:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  86. procedure BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  87. function BuildConstExpression(allowref,betweenbracket:boolean): aint;
  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,
  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 : aint;
  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)
  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 : string;
  962. lasTSec : TAsmSectiontype;
  963. l1,
  964. l2,
  965. symofs : aint;
  966. symtyp : TAsmsymtype;
  967. Begin
  968. Message1(asmr_d_start_reading,'GNU AS');
  969. firsttoken:=TRUE;
  970. { sets up all opcode and register tables in uppercase }
  971. if not _asmsorted then
  972. Begin
  973. SetupTables;
  974. _asmsorted:=TRUE;
  975. end;
  976. curlist:=TAsmList.Create;
  977. lasTSec:=sec_code;
  978. { start tokenizer }
  979. gettoken;
  980. { main loop }
  981. repeat
  982. case actasmtoken of
  983. AS_LLABEL:
  984. Begin
  985. if CreateLocalLabel(actasmpattern,hl,true) then
  986. ConcatLabel(curlist,hl);
  987. Consume(AS_LLABEL);
  988. end;
  989. AS_LABEL:
  990. Begin
  991. if SearchLabel(upper(actasmpattern),hl,true) then
  992. ConcatLabel(curlist,hl)
  993. else
  994. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  995. Consume(AS_LABEL);
  996. end;
  997. AS_DW:
  998. Begin
  999. Consume(AS_DW);
  1000. BuildConstant(2);
  1001. end;
  1002. AS_DATA:
  1003. Begin
  1004. new_section(curList,sec_data,lower(current_procinfo.procdef.mangledname),0);
  1005. lasTSec:=sec_data;
  1006. Consume(AS_DATA);
  1007. end;
  1008. AS_TEXT:
  1009. Begin
  1010. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1011. lasTSec:=sec_code;
  1012. Consume(AS_TEXT);
  1013. end;
  1014. AS_INIT:
  1015. Begin
  1016. new_section(curList,sec_init,lower(current_procinfo.procdef.mangledname),0);
  1017. lasTSec:=sec_init;
  1018. Consume(AS_INIT);
  1019. end;
  1020. AS_FINI:
  1021. Begin
  1022. new_section(curList,sec_fini,lower(current_procinfo.procdef.mangledname),0);
  1023. lasTSec:=sec_fini;
  1024. Consume(AS_FINI);
  1025. end;
  1026. AS_DB:
  1027. Begin
  1028. Consume(AS_DB);
  1029. BuildConstant(1);
  1030. end;
  1031. AS_DD:
  1032. Begin
  1033. Consume(AS_DD);
  1034. BuildConstant(4);
  1035. end;
  1036. AS_DQ:
  1037. Begin
  1038. Consume(AS_DQ);
  1039. {$ifdef cpu64bitaddr}
  1040. BuildConstant(8);
  1041. {$else cpu64bitaddr}
  1042. BuildRealConstant(s64comp);
  1043. {$endif cpu64bitaddr}
  1044. end;
  1045. AS_SINGLE:
  1046. Begin
  1047. Consume(AS_SINGLE);
  1048. BuildRealConstant(s32real);
  1049. end;
  1050. AS_DOUBLE:
  1051. Begin
  1052. Consume(AS_DOUBLE);
  1053. BuildRealConstant(s64real);
  1054. end;
  1055. AS_EXTENDED:
  1056. Begin
  1057. Consume(AS_EXTENDED);
  1058. BuildRealConstant(s80real);
  1059. end;
  1060. AS_CEXTENDED:
  1061. Begin
  1062. Consume(AS_CEXTENDED);
  1063. BuildRealConstant(sc80real);
  1064. end;
  1065. AS_GLOBAL:
  1066. Begin
  1067. Consume(AS_GLOBAL);
  1068. if actasmtoken=AS_ID then
  1069. ConcatPublic(curlist,actasmpattern);
  1070. Consume(AS_ID);
  1071. if actasmtoken<>AS_SEPARATOR then
  1072. Consume(AS_SEPARATOR);
  1073. end;
  1074. AS_ALIGN:
  1075. Begin
  1076. Consume(AS_ALIGN);
  1077. l1:=BuildConstExpression(false,false);
  1078. if (target_info.system in [system_i386_GO32V2]) then
  1079. begin
  1080. l2:=1;
  1081. if (l1>=0) and (l1<=16) then
  1082. while (l1>0) do
  1083. begin
  1084. l2:=2*l2;
  1085. dec(l1);
  1086. end;
  1087. l1:=l2;
  1088. end;
  1089. ConcatAlign(curlist,l1);
  1090. Message(asmr_n_align_is_target_specific);
  1091. if actasmtoken<>AS_SEPARATOR then
  1092. Consume(AS_SEPARATOR);
  1093. end;
  1094. AS_BALIGN:
  1095. Begin
  1096. Consume(AS_BALIGN);
  1097. ConcatAlign(curlist,BuildConstExpression(false,false));
  1098. if actasmtoken<>AS_SEPARATOR then
  1099. Consume(AS_SEPARATOR);
  1100. end;
  1101. AS_P2ALIGN:
  1102. Begin
  1103. Consume(AS_P2ALIGN);
  1104. l1:=BuildConstExpression(false,false);
  1105. l2:=1;
  1106. if (l1>=0) and (l1<=16) then
  1107. while (l1>0) do
  1108. begin
  1109. l2:=2*l2;
  1110. dec(l1);
  1111. end;
  1112. l1:=l2;
  1113. ConcatAlign(curlist,l1);
  1114. if actasmtoken<>AS_SEPARATOR then
  1115. Consume(AS_SEPARATOR);
  1116. end;
  1117. AS_ASCIIZ:
  1118. Begin
  1119. Consume(AS_ASCIIZ);
  1120. BuildStringConstant(TRUE);
  1121. end;
  1122. AS_ASCII:
  1123. Begin
  1124. Consume(AS_ASCII);
  1125. BuildStringConstant(FALSE);
  1126. end;
  1127. AS_LCOMM:
  1128. Begin
  1129. Consume(AS_LCOMM);
  1130. commname:=actasmpattern;
  1131. Consume(AS_ID);
  1132. Consume(AS_COMMA);
  1133. curList.concat(Tai_datablock.Create(commname,BuildConstExpression(false,false)));
  1134. if actasmtoken<>AS_SEPARATOR then
  1135. Consume(AS_SEPARATOR);
  1136. end;
  1137. AS_COMM:
  1138. Begin
  1139. Consume(AS_COMM);
  1140. commname:=actasmpattern;
  1141. Consume(AS_ID);
  1142. Consume(AS_COMMA);
  1143. curList.concat(Tai_datablock.Create_global(commname,BuildConstExpression(false,false)));
  1144. if actasmtoken<>AS_SEPARATOR then
  1145. Consume(AS_SEPARATOR);
  1146. end;
  1147. AS_OPCODE:
  1148. Begin
  1149. HandleOpCode;
  1150. end;
  1151. AS_SEPARATOR:
  1152. Begin
  1153. Consume(AS_SEPARATOR);
  1154. end;
  1155. AS_RVA:
  1156. begin
  1157. { .rva generally applies to systems with COFF output format,
  1158. not just Windows. }
  1159. if not (target_info.system in systems_all_windows) then
  1160. Message1(asmr_e_unsupported_directive,token2str[AS_RVA]);
  1161. Consume(AS_RVA);
  1162. BuildRva;
  1163. end;
  1164. AS_SET:
  1165. begin
  1166. Consume(AS_SET);
  1167. BuildConstSymbolExpression(true,false,false, symofs,symname,symtyp);
  1168. Consume(AS_COMMA);
  1169. BuildConstSymbolExpression(true,false,false, symofs,symval,symtyp);
  1170. curList.concat(tai_symbolpair.create(spk_set,symname,symval));
  1171. end;
  1172. AS_WEAK:
  1173. begin
  1174. Consume(AS_WEAK);
  1175. BuildConstSymbolExpression(true,false,false, l1,symname,symtyp);
  1176. { ideally, we should look up the symbol here (or later) and
  1177. depending on whether it's external or net, generate
  1178. asd_weak_reference or asd_weak_definition -- this is different
  1179. on some targets) }
  1180. curList.concat(tai_directive.create(asd_weak_definition,symname));
  1181. end;
  1182. AS_SECTION:
  1183. begin
  1184. Consume(AS_SECTION);
  1185. new_section(curlist, sec_user, actasmpattern, 0);
  1186. //curList.concat(tai_section.create(sec_user, actasmpattern, 0));
  1187. consume(AS_STRING);
  1188. end;
  1189. AS_TARGET_DIRECTIVE:
  1190. HandleTargetDirective;
  1191. AS_END:
  1192. begin
  1193. break; { end assembly block }
  1194. end;
  1195. else
  1196. Begin
  1197. Message(asmr_e_syntax_error);
  1198. RecoverConsume(false);
  1199. end;
  1200. end;
  1201. until false;
  1202. { check that all referenced local labels are defined }
  1203. checklocallabels;
  1204. { are we back in the code section? }
  1205. if lasTSec<>sec_code then
  1206. begin
  1207. Message(asmr_w_assembler_code_not_returned_to_text);
  1208. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1209. end;
  1210. { Return the list in an asmnode }
  1211. assemble:=curlist;
  1212. Message1(asmr_d_finish_reading,'GNU AS');
  1213. end;
  1214. {*****************************************************************************
  1215. Parsing Helpers
  1216. *****************************************************************************}
  1217. Procedure tattreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  1218. { Description: This routine builds up a record offset after a AS_DOT }
  1219. { token is encountered. }
  1220. { On entry actasmtoken should be equal to AS_DOT }
  1221. var
  1222. s : string;
  1223. Begin
  1224. offset:=0;
  1225. size:=0;
  1226. s:=expr;
  1227. while (actasmtoken=AS_DOT) do
  1228. begin
  1229. Consume(AS_DOT);
  1230. { a record field could have the same name as a register }
  1231. if actasmtoken in [AS_ID,AS_REGISTER] then
  1232. begin
  1233. s:=s+'.'+actasmpattern;
  1234. consume(actasmtoken)
  1235. end
  1236. else
  1237. begin
  1238. Consume(AS_ID);
  1239. RecoverConsume(true);
  1240. break;
  1241. end;
  1242. end;
  1243. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs) then
  1244. Message(asmr_e_building_record_offset);
  1245. end;
  1246. procedure tattreader.BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  1247. var
  1248. hssymtyp : TAsmSymType;
  1249. hs,tempstr,expr,mangledname : string;
  1250. parenlevel : longint;
  1251. l,k : aint;
  1252. errorflag : boolean;
  1253. prevtok : tasmtoken;
  1254. sym : tsym;
  1255. srsymtable : TSymtable;
  1256. hl : tasmlabel;
  1257. Begin
  1258. asmsym:='';
  1259. asmsymtyp:=AT_DATA;
  1260. value:=0;
  1261. errorflag:=FALSE;
  1262. tempstr:='';
  1263. expr:='';
  1264. parenlevel:=0;
  1265. Repeat
  1266. Case actasmtoken of
  1267. AS_LPAREN:
  1268. Begin
  1269. { Exit if ref? }
  1270. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  1271. break;
  1272. Consume(AS_LPAREN);
  1273. expr:=expr + '(';
  1274. inc(parenlevel);
  1275. end;
  1276. AS_RBRACKET:
  1277. begin
  1278. if betweenbracket then
  1279. break;
  1280. { write error only once. }
  1281. if not errorflag then
  1282. Message(asmr_e_invalid_constant_expression);
  1283. { consume tokens until we find COMMA or SEPARATOR }
  1284. Consume(actasmtoken);
  1285. errorflag:=TRUE;
  1286. end;
  1287. AS_RPAREN:
  1288. Begin
  1289. { end of ref ? }
  1290. if (parenlevel=0) and betweenbracket then
  1291. break;
  1292. Consume(AS_RPAREN);
  1293. expr:=expr + ')';
  1294. dec(parenlevel);
  1295. end;
  1296. AS_SHL:
  1297. Begin
  1298. Consume(AS_SHL);
  1299. expr:=expr + '<';
  1300. end;
  1301. AS_SHR:
  1302. Begin
  1303. Consume(AS_SHR);
  1304. expr:=expr + '>';
  1305. end;
  1306. AS_SLASH:
  1307. Begin
  1308. Consume(AS_SLASH);
  1309. expr:=expr + '/';
  1310. end;
  1311. AS_MOD:
  1312. Begin
  1313. Consume(AS_MOD);
  1314. expr:=expr + '%';
  1315. end;
  1316. AS_STAR:
  1317. Begin
  1318. Consume(AS_STAR);
  1319. expr:=expr + '*';
  1320. end;
  1321. AS_PLUS:
  1322. Begin
  1323. Consume(AS_PLUS);
  1324. expr:=expr + '+';
  1325. end;
  1326. AS_MINUS:
  1327. Begin
  1328. Consume(AS_MINUS);
  1329. expr:=expr + '-';
  1330. end;
  1331. AS_AND:
  1332. Begin
  1333. Consume(AS_AND);
  1334. expr:=expr + '&';
  1335. end;
  1336. AS_NOT:
  1337. Begin
  1338. Consume(AS_NOT);
  1339. expr:=expr + '~';
  1340. end;
  1341. AS_XOR:
  1342. Begin
  1343. Consume(AS_XOR);
  1344. expr:=expr + '^';
  1345. end;
  1346. AS_OR:
  1347. Begin
  1348. Consume(AS_OR);
  1349. expr:=expr + '|';
  1350. end;
  1351. AS_INTNUM:
  1352. Begin
  1353. expr:=expr + actasmpattern;
  1354. Consume(AS_INTNUM);
  1355. end;
  1356. AS_DOLLAR:
  1357. begin
  1358. Consume(AS_DOLLAR);
  1359. if actasmtoken<>AS_ID then
  1360. Message(asmr_e_dollar_without_identifier);
  1361. end;
  1362. AS_STRING:
  1363. Begin
  1364. l:=0;
  1365. case Length(actasmpattern) of
  1366. 1 :
  1367. l:=ord(actasmpattern[1]);
  1368. 2 :
  1369. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1370. 3 :
  1371. l:=ord(actasmpattern[3]) +
  1372. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1373. 4 :
  1374. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1375. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1376. else
  1377. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1378. end;
  1379. str(l, tempstr);
  1380. expr:=expr + tempstr;
  1381. Consume(AS_STRING);
  1382. end;
  1383. AS_SIZEOF,
  1384. AS_TYPE:
  1385. begin
  1386. l:=0;
  1387. Consume(actasmtoken);
  1388. if actasmtoken<>AS_ID then
  1389. Message(asmr_e_type_without_identifier)
  1390. else
  1391. begin
  1392. tempstr:=actasmpattern;
  1393. Consume(AS_ID);
  1394. if actasmtoken=AS_DOT then
  1395. begin
  1396. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  1397. if mangledname<>'' then
  1398. Message(asmr_e_wrong_sym_type);
  1399. end
  1400. else
  1401. begin
  1402. searchsym(tempstr,sym,srsymtable);
  1403. if assigned(sym) then
  1404. begin
  1405. case sym.typ of
  1406. staticvarsym,
  1407. localvarsym,
  1408. paravarsym :
  1409. l:=tabstractvarsym(sym).getsize;
  1410. typesym :
  1411. l:=ttypesym(sym).typedef.size;
  1412. else
  1413. Message(asmr_e_wrong_sym_type);
  1414. end;
  1415. end
  1416. else
  1417. Message1(sym_e_unknown_id,tempstr);
  1418. end;
  1419. end;
  1420. str(l, tempstr);
  1421. expr:=expr + tempstr;
  1422. end;
  1423. AS_VMTOFFSET:
  1424. begin
  1425. Consume(actasmtoken);
  1426. if actasmtoken<>AS_ID then
  1427. Message(asmr_e_type_without_identifier)
  1428. else
  1429. begin
  1430. tempstr:=actasmpattern;
  1431. consume(AS_ID);
  1432. BuildRecordOffsetSize(tempstr,k,l,mangledname,true);
  1433. if (mangledname <> '') then
  1434. Message(asmr_e_wrong_sym_type);
  1435. str(k,tempstr);
  1436. expr := expr + tempstr;
  1437. end
  1438. end;
  1439. AS_ID:
  1440. Begin
  1441. hs:='';
  1442. hssymtyp:=AT_DATA;
  1443. tempstr:=actasmpattern;
  1444. prevtok:=prevasmtoken;
  1445. consume(AS_ID);
  1446. if SearchIConstant(tempstr,l) then
  1447. begin
  1448. str(l, tempstr);
  1449. expr:=expr + tempstr;
  1450. end
  1451. else
  1452. begin
  1453. if is_locallabel(tempstr) then
  1454. begin
  1455. CreateLocalLabel(tempstr,hl,false);
  1456. hs:=hl.name;
  1457. hssymtyp:=AT_LABEL;
  1458. end
  1459. else
  1460. if SearchLabel(tempstr,hl,false) then
  1461. begin
  1462. hs:=hl.name;
  1463. hssymtyp:=AT_FUNCTION;
  1464. end
  1465. else
  1466. begin
  1467. searchsym(tempstr,sym,srsymtable);
  1468. if assigned(sym) then
  1469. begin
  1470. case sym.typ of
  1471. staticvarsym :
  1472. begin
  1473. { we always assume in asm statements that }
  1474. { that the variable is valid. }
  1475. tabstractvarsym(sym).varstate:=vs_readwritten;
  1476. inc(tabstractvarsym(sym).refs);
  1477. { variable can't be placed in a register }
  1478. tabstractvarsym(sym).varregable:=vr_none;
  1479. { and anything may happen with its address }
  1480. tabstractvarsym(sym).addr_taken:=true;
  1481. hs:=tstaticvarsym(sym).mangledname;
  1482. end;
  1483. localvarsym,
  1484. paravarsym :
  1485. Message(asmr_e_no_local_or_para_allowed);
  1486. procsym :
  1487. begin
  1488. if Tprocsym(sym).ProcdefList.Count>1 then
  1489. Message(asmr_w_calling_overload_func);
  1490. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1491. hssymtyp:=AT_FUNCTION;
  1492. end;
  1493. typesym :
  1494. begin
  1495. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1496. Message(asmr_e_wrong_sym_type);
  1497. end;
  1498. else
  1499. Message(asmr_e_wrong_sym_type);
  1500. end;
  1501. end
  1502. else
  1503. Message1(sym_e_unknown_id,tempstr);
  1504. end;
  1505. { symbol found? }
  1506. if hs<>'' then
  1507. begin
  1508. if needofs and (prevtok<>AS_DOLLAR) then
  1509. Message(asmr_e_need_dollar);
  1510. if asmsym='' then
  1511. begin
  1512. asmsym:=hs;
  1513. asmsymtyp:=hssymtyp;
  1514. end
  1515. else
  1516. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1517. if (expr='') or (expr[length(expr)]='+') then
  1518. begin
  1519. { don't remove the + if there could be a record field }
  1520. if actasmtoken<>AS_DOT then
  1521. delete(expr,length(expr),1);
  1522. end
  1523. else
  1524. Message(asmr_e_only_add_relocatable_symbol);
  1525. end;
  1526. if actasmtoken=AS_DOT then
  1527. begin
  1528. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1529. if (hs<>'') then
  1530. hssymtyp:=AT_FUNCTION
  1531. else
  1532. begin
  1533. str(l, tempstr);
  1534. expr:=expr + tempstr;
  1535. end
  1536. end
  1537. else
  1538. begin
  1539. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1540. delete(expr,length(expr),1);
  1541. end;
  1542. end;
  1543. { check if there are wrong operator used like / or mod etc. }
  1544. if (hs<>'') and
  1545. not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,
  1546. AS_LPAREN,AS_RPAREN,AS_RBRACKET,AS_END]) then
  1547. Message(asmr_e_only_add_relocatable_symbol);
  1548. end;
  1549. AS_END,
  1550. AS_SEPARATOR,
  1551. AS_COMMA:
  1552. break;
  1553. else
  1554. Begin
  1555. { write error only once. }
  1556. if not errorflag then
  1557. Message(asmr_e_invalid_constant_expression);
  1558. { consume tokens until we find COMMA or SEPARATOR }
  1559. Consume(actasmtoken);
  1560. errorflag:=TRUE;
  1561. end;
  1562. end;
  1563. Until false;
  1564. { calculate expression }
  1565. if not ErrorFlag then
  1566. value:=CalculateExpression(expr)
  1567. else
  1568. value:=0;
  1569. end;
  1570. function tattreader.BuildConstExpression(allowref,betweenbracket:boolean): aint;
  1571. var
  1572. l : aint;
  1573. hs : string;
  1574. hssymtyp : TAsmSymType;
  1575. begin
  1576. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs,hssymtyp);
  1577. if hs<>'' then
  1578. Message(asmr_e_relocatable_symbol_not_allowed);
  1579. BuildConstExpression:=l;
  1580. end;
  1581. Procedure tattreader.BuildConstantOperand(oper : toperand);
  1582. var
  1583. l : aint;
  1584. tempstr : string;
  1585. tempsymtyp : TAsmSymType;
  1586. begin
  1587. BuildConstSymbolExpression(false,false,true,l,tempstr,tempsymtyp);
  1588. if tempstr<>'' then
  1589. begin
  1590. oper.opr.typ:=OPR_SYMBOL;
  1591. oper.opr.symofs:=l;
  1592. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr);
  1593. end
  1594. else
  1595. begin
  1596. oper.opr.typ:=OPR_CONSTANT;
  1597. oper.opr.val:=l;
  1598. end;
  1599. end;
  1600. procedure tattreader.BuildRva;
  1601. var
  1602. asmsymtyp : TAsmSymType;
  1603. asmsym: string;
  1604. value : aint;
  1605. ai:tai_const;
  1606. begin
  1607. repeat
  1608. case actasmtoken of
  1609. AS_INTNUM,
  1610. AS_PLUS,
  1611. AS_MINUS,
  1612. AS_LPAREN,
  1613. AS_ID :
  1614. Begin
  1615. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  1616. if asmsym<>'' then
  1617. begin
  1618. ai:=tai_const.create_type_sym(aitconst_rva_symbol,current_asmdata.RefAsmSymbol(asmsym));
  1619. ai.value:=value;
  1620. curlist.concat(ai);
  1621. end
  1622. else
  1623. Message(asmr_e_invalid_symbol_ref);
  1624. end;
  1625. AS_COMMA:
  1626. Consume(AS_COMMA);
  1627. AS_END,
  1628. AS_SEPARATOR:
  1629. break;
  1630. else
  1631. begin
  1632. Message(asmr_e_syn_constant);
  1633. RecoverConsume(false);
  1634. end
  1635. end; { end case }
  1636. until false;
  1637. end;
  1638. end.