2
0

raatt.pas 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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. { save old token and reset new token }
  177. prevasmtoken:=actasmtoken;
  178. actasmtoken:=AS_NONE;
  179. { reset }
  180. actasmpattern:='';
  181. { while space and tab , continue scan... }
  182. while c in [' ',#9] do
  183. c:=current_scanner.asmgetchar;
  184. { get token pos }
  185. if not (c in [#10,#13,'{',';']) then
  186. current_scanner.gettokenpos;
  187. { Local Label, Label, Directive, Prefix or Opcode }
  188. if firsttoken and not(c in [#10,#13,'{',';']) then
  189. begin
  190. firsttoken:=FALSE;
  191. len:=0;
  192. { directive or local label }
  193. if c = '.' then
  194. begin
  195. inc(len);
  196. actasmpattern[len]:=c;
  197. { Let us point to the next character }
  198. c:=current_scanner.asmgetchar;
  199. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  200. begin
  201. inc(len);
  202. actasmpattern[len]:=c;
  203. c:=current_scanner.asmgetchar;
  204. end;
  205. actasmpattern[0]:=chr(len);
  206. { this is a local label... }
  207. if (c=':') and is_locallabel(actasmpattern) then
  208. Begin
  209. { local variables are case sensitive }
  210. actasmtoken:=AS_LLABEL;
  211. c:=current_scanner.asmgetchar;
  212. firsttoken:=true;
  213. exit;
  214. end
  215. { must be a directive }
  216. else
  217. Begin
  218. { directives are case sensitive!! }
  219. if is_asmdirective(actasmpattern) then
  220. exit;
  221. if is_targetdirective(actasmpattern) then
  222. begin
  223. actasmtoken:=AS_TARGET_DIRECTIVE;
  224. exit;
  225. end;
  226. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  227. end;
  228. end;
  229. { only opcodes and global labels are allowed now. }
  230. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  231. begin
  232. inc(len);
  233. actasmpattern[len]:=c;
  234. c:=current_scanner.asmgetchar;
  235. end;
  236. actasmpattern[0]:=chr(len);
  237. { Label ? }
  238. if c = ':' then
  239. begin
  240. actasmtoken:=AS_LABEL;
  241. { let us point to the next character }
  242. c:=current_scanner.asmgetchar;
  243. firsttoken:=true;
  244. exit;
  245. end;
  246. {$if defined(POWERPC) or defined(POWERPC64)}
  247. { some PowerPC instructions can have the postfix -, + or .
  248. this code could be moved to is_asmopcode but I think
  249. it's better to ifdef it here (FK)
  250. }
  251. case c of
  252. '.', '-', '+':
  253. begin
  254. actasmpattern:=actasmpattern+c;
  255. c:=current_scanner.asmgetchar;
  256. end
  257. end;
  258. {$endif POWERPC}
  259. {$if defined(ARM)}
  260. {
  261. Thumb-2 instructions can have a .W postfix to indicate 32bit instructions,
  262. Also in unified syntax sizes and types are indicated with something like a .<dt> prefix for example
  263. }
  264. case c of
  265. '.':
  266. begin
  267. if len>1 then
  268. begin
  269. while c in ['A'..'Z','a'..'z','0'..'9','_','.'] do
  270. begin
  271. inc(len);
  272. actasmpattern[len]:=c;
  273. c:=current_scanner.asmgetchar;
  274. end;
  275. actasmpattern[0]:=chr(len);
  276. end;
  277. {actasmpattern:=actasmpattern+c;
  278. c:=current_scanner.asmgetchar;
  279. if upcase(c) = 'W' then
  280. begin
  281. actasmpattern:=actasmpattern+c;
  282. c:=current_scanner.asmgetchar;
  283. end
  284. else
  285. internalerror(2010122301);}
  286. end
  287. end;
  288. {$endif ARM}
  289. {$ifdef aarch64}
  290. { b.cond }
  291. case c of
  292. '.':
  293. begin
  294. repeat
  295. actasmpattern:=actasmpattern+c;
  296. c:=current_scanner.asmgetchar;
  297. until not(c in ['a'..'z','A'..'Z']);
  298. end;
  299. end;
  300. {$endif aarch64}
  301. { Opcode ? }
  302. If is_asmopcode(upper(actasmpattern)) then
  303. Begin
  304. uppervar(actasmpattern);
  305. exit;
  306. end;
  307. { End of assemblerblock ? }
  308. if upper(actasmpattern) = 'END' then
  309. begin
  310. actasmtoken:=AS_END;
  311. exit;
  312. end;
  313. message1(asmr_e_unknown_opcode,actasmpattern);
  314. actasmtoken:=AS_NONE;
  315. end
  316. else { else firsttoken }
  317. { Here we must handle all possible cases }
  318. begin
  319. case c of
  320. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  321. { - field of object/record }
  322. { - directive. }
  323. begin
  324. if (prevasmtoken in [AS_ID,AS_RPAREN]) then
  325. begin
  326. c:=current_scanner.asmgetchar;
  327. actasmtoken:=AS_DOT;
  328. exit;
  329. end;
  330. actasmpattern:=c;
  331. c:=current_scanner.asmgetchar;
  332. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  333. begin
  334. actasmpattern:=actasmpattern + c;
  335. c:=current_scanner.asmgetchar;
  336. end;
  337. if is_asmdirective(actasmpattern) then
  338. exit;
  339. if is_targetdirective(actasmpattern) then
  340. begin
  341. actasmtoken:=AS_TARGET_DIRECTIVE;
  342. exit;
  343. end;
  344. { local label references and directives }
  345. { are case sensitive }
  346. actasmtoken:=AS_ID;
  347. exit;
  348. end;
  349. { identifier, register, prefix or directive }
  350. '_','A'..'Z','a'..'z':
  351. begin
  352. len:=0;
  353. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  354. begin
  355. inc(len);
  356. actasmpattern[len]:=c;
  357. c:=current_scanner.asmgetchar;
  358. end;
  359. actasmpattern[0]:=chr(len);
  360. uppervar(actasmpattern);
  361. {$ifdef x86}
  362. { only x86 architectures have instruction prefixes }
  363. { Opcode, can only be when the previous was a prefix }
  364. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  365. Begin
  366. uppervar(actasmpattern);
  367. exit;
  368. end;
  369. {$endif x86}
  370. { check for end which is a reserved word unlike the opcodes }
  371. if actasmpattern = 'END' then
  372. Begin
  373. actasmtoken:=AS_END;
  374. exit;
  375. end;
  376. if actasmpattern = 'TYPE' then
  377. Begin
  378. actasmtoken:=AS_TYPE;
  379. exit;
  380. end;
  381. if actasmpattern = 'SIZEOF' then
  382. Begin
  383. actasmtoken:=AS_SIZEOF;
  384. exit;
  385. end;
  386. if actasmpattern = 'VMTOFFSET' then
  387. Begin
  388. actasmtoken:=AS_VMTOFFSET;
  389. exit;
  390. end;
  391. if is_register(actasmpattern) then
  392. begin
  393. actasmtoken:=AS_REGISTER;
  394. exit;
  395. end;
  396. { if next is a '.' and this is a unitsym then we also need to
  397. parse the identifier }
  398. if (c='.') then
  399. begin
  400. searchsym(actasmpattern,srsym,srsymtable);
  401. if assigned(srsym) and
  402. (srsym.typ=unitsym) and
  403. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  404. srsym.owner.iscurrentunit then
  405. begin
  406. actasmpattern:=actasmpattern+c;
  407. c:=current_scanner.asmgetchar;
  408. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  409. begin
  410. actasmpattern:=actasmpattern + upcase(c);
  411. c:=current_scanner.asmgetchar;
  412. end;
  413. end;
  414. end;
  415. actasmtoken:=AS_ID;
  416. exit;
  417. end;
  418. '%' : { register or modulo }
  419. handlepercent;
  420. '1'..'9': { integer number }
  421. begin
  422. len:=0;
  423. while c in ['0'..'9'] do
  424. Begin
  425. inc(len);
  426. actasmpattern[len]:=c;
  427. c:=current_scanner.asmgetchar;
  428. end;
  429. actasmpattern[0]:=chr(len);
  430. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  431. actasmtoken:=AS_INTNUM;
  432. exit;
  433. end;
  434. '0' : { octal,hexa,real or binary number. }
  435. begin
  436. actasmpattern:=c;
  437. c:=current_scanner.asmgetchar;
  438. case upcase(c) of
  439. 'B': { binary }
  440. Begin
  441. c:=current_scanner.asmgetchar;
  442. while c in ['0','1'] do
  443. Begin
  444. actasmpattern:=actasmpattern + c;
  445. c:=current_scanner.asmgetchar;
  446. end;
  447. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  448. actasmtoken:=AS_INTNUM;
  449. exit;
  450. end;
  451. 'D': { real }
  452. Begin
  453. c:=current_scanner.asmgetchar;
  454. { get ridd of the 0d }
  455. if (c in ['+','-']) then
  456. begin
  457. actasmpattern:=c;
  458. c:=current_scanner.asmgetchar;
  459. end
  460. else
  461. actasmpattern:='';
  462. while c in ['0'..'9'] do
  463. Begin
  464. actasmpattern:=actasmpattern + c;
  465. c:=current_scanner.asmgetchar;
  466. end;
  467. if c='.' then
  468. begin
  469. actasmpattern:=actasmpattern + c;
  470. c:=current_scanner.asmgetchar;
  471. while c in ['0'..'9'] do
  472. Begin
  473. actasmpattern:=actasmpattern + c;
  474. c:=current_scanner.asmgetchar;
  475. end;
  476. if upcase(c) = 'E' then
  477. begin
  478. actasmpattern:=actasmpattern + c;
  479. c:=current_scanner.asmgetchar;
  480. if (c in ['+','-']) then
  481. begin
  482. actasmpattern:=actasmpattern + c;
  483. c:=current_scanner.asmgetchar;
  484. end;
  485. while c in ['0'..'9'] do
  486. Begin
  487. actasmpattern:=actasmpattern + c;
  488. c:=current_scanner.asmgetchar;
  489. end;
  490. end;
  491. actasmtoken:=AS_REALNUM;
  492. exit;
  493. end
  494. else
  495. begin
  496. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  497. actasmtoken:=AS_NONE;
  498. end;
  499. end;
  500. 'X': { hexadecimal }
  501. Begin
  502. c:=current_scanner.asmgetchar;
  503. while c in ['0'..'9','a'..'f','A'..'F'] do
  504. Begin
  505. actasmpattern:=actasmpattern + c;
  506. c:=current_scanner.asmgetchar;
  507. end;
  508. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  509. actasmtoken:=AS_INTNUM;
  510. exit;
  511. end;
  512. '1'..'7': { octal }
  513. begin
  514. actasmpattern:=actasmpattern + c;
  515. while c in ['0'..'7'] do
  516. Begin
  517. actasmpattern:=actasmpattern + c;
  518. c:=current_scanner.asmgetchar;
  519. end;
  520. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  521. actasmtoken:=AS_INTNUM;
  522. exit;
  523. end;
  524. else { octal number zero value...}
  525. Begin
  526. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  527. actasmtoken:=AS_INTNUM;
  528. exit;
  529. end;
  530. end; { end case }
  531. end;
  532. '&' :
  533. begin
  534. c:=current_scanner.asmgetchar;
  535. actasmtoken:=AS_AND;
  536. end;
  537. '''' : { char }
  538. begin
  539. current_scanner.in_asm_string:=true;
  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. current_scanner.in_asm_string:=false;
  565. exit;
  566. end;
  567. '"' : { string }
  568. begin
  569. current_scanner.in_asm_string:=true;
  570. actasmpattern:='';
  571. repeat
  572. c:=current_scanner.asmgetchar;
  573. case c of
  574. '\' :
  575. begin
  576. { copy also the next char so \" is parsed correctly }
  577. actasmpattern:=actasmpattern+c;
  578. c:=current_scanner.asmgetchar;
  579. actasmpattern:=actasmpattern+c;
  580. end;
  581. '"' :
  582. begin
  583. c:=current_scanner.asmgetchar;
  584. break;
  585. end;
  586. #10,#13:
  587. Message(scan_f_string_exceeds_line);
  588. else
  589. actasmpattern:=actasmpattern+c;
  590. end;
  591. until false;
  592. actasmpattern:=EscapeToPascal(actasmpattern);
  593. actasmtoken:=AS_STRING;
  594. current_scanner.in_asm_string:=false;
  595. exit;
  596. end;
  597. '$' :
  598. begin
  599. handledollar;
  600. exit;
  601. end;
  602. '#' :
  603. begin
  604. actasmtoken:=AS_HASH;
  605. c:=current_scanner.asmgetchar;
  606. exit;
  607. end;
  608. '[' :
  609. begin
  610. actasmtoken:=AS_LBRACKET;
  611. c:=current_scanner.asmgetchar;
  612. exit;
  613. end;
  614. ']' :
  615. begin
  616. actasmtoken:=AS_RBRACKET;
  617. c:=current_scanner.asmgetchar;
  618. exit;
  619. end;
  620. {$ifdef arm}
  621. // the arm assembler uses { ... } for register sets
  622. '{' :
  623. begin
  624. actasmtoken:=AS_LSBRACKET;
  625. c:=current_scanner.asmgetchar;
  626. exit;
  627. end;
  628. '}' :
  629. begin
  630. actasmtoken:=AS_RSBRACKET;
  631. c:=current_scanner.asmgetchar;
  632. exit;
  633. end;
  634. '=' :
  635. begin
  636. actasmtoken:=AS_EQUAL;
  637. c:=current_scanner.asmgetchar;
  638. exit;
  639. end;
  640. {$endif arm}
  641. ',' :
  642. begin
  643. actasmtoken:=AS_COMMA;
  644. c:=current_scanner.asmgetchar;
  645. exit;
  646. end;
  647. '<' :
  648. begin
  649. actasmtoken:=AS_SHL;
  650. c:=current_scanner.asmgetchar;
  651. if c = '<' then
  652. c:=current_scanner.asmgetchar;
  653. exit;
  654. end;
  655. '>' :
  656. begin
  657. actasmtoken:=AS_SHL;
  658. c:=current_scanner.asmgetchar;
  659. if c = '>' then
  660. c:=current_scanner.asmgetchar;
  661. exit;
  662. end;
  663. '|' :
  664. begin
  665. actasmtoken:=AS_OR;
  666. c:=current_scanner.asmgetchar;
  667. exit;
  668. end;
  669. '^' :
  670. begin
  671. actasmtoken:=AS_XOR;
  672. c:=current_scanner.asmgetchar;
  673. exit;
  674. end;
  675. '(' :
  676. begin
  677. actasmtoken:=AS_LPAREN;
  678. c:=current_scanner.asmgetchar;
  679. exit;
  680. end;
  681. ')' :
  682. begin
  683. actasmtoken:=AS_RPAREN;
  684. c:=current_scanner.asmgetchar;
  685. exit;
  686. end;
  687. ':' :
  688. begin
  689. actasmtoken:=AS_COLON;
  690. c:=current_scanner.asmgetchar;
  691. exit;
  692. end;
  693. '+' :
  694. begin
  695. actasmtoken:=AS_PLUS;
  696. c:=current_scanner.asmgetchar;
  697. exit;
  698. end;
  699. '-' :
  700. begin
  701. actasmtoken:=AS_MINUS;
  702. c:=current_scanner.asmgetchar;
  703. exit;
  704. end;
  705. '*' :
  706. begin
  707. actasmtoken:=AS_STAR;
  708. c:=current_scanner.asmgetchar;
  709. exit;
  710. end;
  711. '/' :
  712. begin
  713. actasmtoken:=AS_SLASH;
  714. c:=current_scanner.asmgetchar;
  715. exit;
  716. end;
  717. '!' :
  718. begin
  719. actasmtoken:=AS_NOT;
  720. c:=current_scanner.asmgetchar;
  721. exit;
  722. end;
  723. '@' :
  724. begin
  725. actasmtoken:=AS_AT;
  726. c:=current_scanner.asmgetchar;
  727. exit;
  728. end;
  729. {$ifndef arm}
  730. '{',
  731. {$endif arm}
  732. #13,#10,';' :
  733. begin
  734. { the comment is read by asmgetchar }
  735. c:=current_scanner.asmgetchar;
  736. firsttoken:=TRUE;
  737. actasmtoken:=AS_SEPARATOR;
  738. exit;
  739. end;
  740. else
  741. current_scanner.illegal_char(c);
  742. end;
  743. end;
  744. end;
  745. function tattreader.consume(t : tasmtoken):boolean;
  746. begin
  747. Consume:=true;
  748. if t<>actasmtoken then
  749. begin
  750. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  751. Consume:=false;
  752. end;
  753. repeat
  754. gettoken;
  755. until actasmtoken<>AS_NONE;
  756. end;
  757. procedure tattreader.RecoverConsume(allowcomma:boolean);
  758. begin
  759. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  760. begin
  761. if allowcomma and (actasmtoken=AS_COMMA) then
  762. break;
  763. Consume(actasmtoken);
  764. end;
  765. end;
  766. Procedure tattreader.BuildConstant(constsize: byte);
  767. var
  768. asmsymtyp : TAsmSymType;
  769. asmsym,
  770. expr: string;
  771. value : aint;
  772. Begin
  773. Repeat
  774. Case actasmtoken of
  775. AS_STRING:
  776. Begin
  777. expr:=actasmpattern;
  778. if length(expr) > 1 then
  779. Message(asmr_e_string_not_allowed_as_const);
  780. Consume(AS_STRING);
  781. Case actasmtoken of
  782. AS_COMMA: Consume(AS_COMMA);
  783. AS_END,
  784. AS_SEPARATOR: ;
  785. else
  786. Message(asmr_e_invalid_string_expression);
  787. end; { end case }
  788. ConcatString(curlist,expr);
  789. end;
  790. AS_INTNUM,
  791. AS_PLUS,
  792. AS_MINUS,
  793. AS_LPAREN,
  794. AS_TYPE,
  795. AS_SIZEOF,
  796. AS_NOT,
  797. AS_VMTOFFSET,
  798. AS_ID :
  799. Begin
  800. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  801. if asmsym<>'' then
  802. begin
  803. if constsize<>sizeof(pint) then
  804. Message(asmr_w_32bit_const_for_address);
  805. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value)
  806. end
  807. else
  808. ConcatConstant(curlist,value,constsize);
  809. end;
  810. AS_COMMA:
  811. Consume(AS_COMMA);
  812. AS_END,
  813. AS_SEPARATOR:
  814. break;
  815. else
  816. begin
  817. Message(asmr_e_syn_constant);
  818. RecoverConsume(false);
  819. end
  820. end; { end case }
  821. Until false;
  822. end;
  823. Procedure tattreader.BuildRealConstant(typ : tfloattype);
  824. var
  825. expr : string;
  826. r : bestreal;
  827. code : integer;
  828. negativ : boolean;
  829. errorflag: boolean;
  830. Begin
  831. errorflag:=FALSE;
  832. Repeat
  833. negativ:=false;
  834. expr:='';
  835. if actasmtoken=AS_PLUS then
  836. Consume(AS_PLUS)
  837. else
  838. if actasmtoken=AS_MINUS then
  839. begin
  840. negativ:=true;
  841. consume(AS_MINUS);
  842. end;
  843. Case actasmtoken of
  844. AS_INTNUM:
  845. Begin
  846. expr:=actasmpattern;
  847. Consume(AS_INTNUM);
  848. if negativ then
  849. expr:='-'+expr;
  850. val(expr,r,code);
  851. if code<>0 then
  852. Begin
  853. r:=0;
  854. Message(asmr_e_invalid_float_expr);
  855. End;
  856. ConcatRealConstant(curlist,r,typ);
  857. end;
  858. AS_REALNUM:
  859. Begin
  860. expr:=actasmpattern;
  861. Consume(AS_REALNUM);
  862. { in ATT syntax you have 0d in front of the real }
  863. { should this be forced ? yes i think so, as to }
  864. { conform to gas as much as possible. }
  865. if (expr[1]='0') and (upper(expr[2])='D') then
  866. Delete(expr,1,2);
  867. if negativ then
  868. expr:='-'+expr;
  869. val(expr,r,code);
  870. if code<>0 then
  871. Begin
  872. r:=0;
  873. Message(asmr_e_invalid_float_expr);
  874. End;
  875. ConcatRealConstant(curlist,r,typ);
  876. end;
  877. AS_COMMA:
  878. begin
  879. Consume(AS_COMMA);
  880. end;
  881. AS_END,
  882. AS_SEPARATOR:
  883. begin
  884. break;
  885. end;
  886. else
  887. Begin
  888. Consume(actasmtoken);
  889. if not errorflag then
  890. Message(asmr_e_invalid_float_expr);
  891. errorflag:=TRUE;
  892. end;
  893. end;
  894. Until false;
  895. end;
  896. Procedure tattreader.BuildStringConstant(asciiz: boolean);
  897. var
  898. expr: string;
  899. errorflag : boolean;
  900. Begin
  901. errorflag:=FALSE;
  902. Repeat
  903. Case actasmtoken of
  904. AS_STRING:
  905. Begin
  906. expr:=actasmpattern;
  907. if asciiz then
  908. expr:=expr+#0;
  909. ConcatString(curlist,expr);
  910. Consume(AS_STRING);
  911. end;
  912. AS_COMMA:
  913. begin
  914. Consume(AS_COMMA);
  915. end;
  916. AS_END,
  917. AS_SEPARATOR:
  918. begin
  919. break;
  920. end;
  921. else
  922. Begin
  923. Consume(actasmtoken);
  924. if not errorflag then
  925. Message(asmr_e_invalid_string_expression);
  926. errorflag:=TRUE;
  927. end;
  928. end;
  929. Until false;
  930. end;
  931. Function tattreader.Assemble: tlinkedlist;
  932. Var
  933. hl : tasmlabel;
  934. commname,
  935. symname,
  936. symval : string;
  937. lasTSec : TAsmSectiontype;
  938. l1,
  939. l2,
  940. symofs : aint;
  941. symtyp : TAsmsymtype;
  942. Begin
  943. Message1(asmr_d_start_reading,'GNU AS');
  944. firsttoken:=TRUE;
  945. { sets up all opcode and register tables in uppercase }
  946. if not _asmsorted then
  947. Begin
  948. SetupTables;
  949. _asmsorted:=TRUE;
  950. end;
  951. curlist:=TAsmList.Create;
  952. lasTSec:=sec_code;
  953. { start tokenizer }
  954. c:=current_scanner.asmgetcharstart;
  955. gettoken;
  956. { main loop }
  957. repeat
  958. case actasmtoken of
  959. AS_LLABEL:
  960. Begin
  961. if CreateLocalLabel(actasmpattern,hl,true) then
  962. ConcatLabel(curlist,hl);
  963. Consume(AS_LLABEL);
  964. end;
  965. AS_LABEL:
  966. Begin
  967. if SearchLabel(upper(actasmpattern),hl,true) then
  968. ConcatLabel(curlist,hl)
  969. else
  970. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  971. Consume(AS_LABEL);
  972. end;
  973. AS_DW:
  974. Begin
  975. Consume(AS_DW);
  976. BuildConstant(2);
  977. end;
  978. AS_DATA:
  979. Begin
  980. new_section(curList,sec_data,lower(current_procinfo.procdef.mangledname),0);
  981. lasTSec:=sec_data;
  982. Consume(AS_DATA);
  983. end;
  984. AS_TEXT:
  985. Begin
  986. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  987. lasTSec:=sec_code;
  988. Consume(AS_TEXT);
  989. end;
  990. AS_INIT:
  991. Begin
  992. new_section(curList,sec_init,lower(current_procinfo.procdef.mangledname),0);
  993. lasTSec:=sec_init;
  994. Consume(AS_INIT);
  995. end;
  996. AS_FINI:
  997. Begin
  998. new_section(curList,sec_fini,lower(current_procinfo.procdef.mangledname),0);
  999. lasTSec:=sec_fini;
  1000. Consume(AS_FINI);
  1001. end;
  1002. AS_DB:
  1003. Begin
  1004. Consume(AS_DB);
  1005. BuildConstant(1);
  1006. end;
  1007. AS_DD:
  1008. Begin
  1009. Consume(AS_DD);
  1010. BuildConstant(4);
  1011. end;
  1012. AS_DQ:
  1013. Begin
  1014. Consume(AS_DQ);
  1015. {$ifdef cpu64bitaddr}
  1016. BuildConstant(8);
  1017. {$else cpu64bitaddr}
  1018. BuildRealConstant(s64comp);
  1019. {$endif cpu64bitaddr}
  1020. end;
  1021. AS_SINGLE:
  1022. Begin
  1023. Consume(AS_SINGLE);
  1024. BuildRealConstant(s32real);
  1025. end;
  1026. AS_DOUBLE:
  1027. Begin
  1028. Consume(AS_DOUBLE);
  1029. BuildRealConstant(s64real);
  1030. end;
  1031. AS_EXTENDED:
  1032. Begin
  1033. Consume(AS_EXTENDED);
  1034. BuildRealConstant(s80real);
  1035. end;
  1036. AS_CEXTENDED:
  1037. Begin
  1038. Consume(AS_CEXTENDED);
  1039. BuildRealConstant(sc80real);
  1040. end;
  1041. AS_GLOBAL:
  1042. Begin
  1043. Consume(AS_GLOBAL);
  1044. if actasmtoken=AS_ID then
  1045. ConcatPublic(curlist,actasmpattern);
  1046. Consume(AS_ID);
  1047. if actasmtoken<>AS_SEPARATOR then
  1048. Consume(AS_SEPARATOR);
  1049. end;
  1050. AS_ALIGN:
  1051. Begin
  1052. Consume(AS_ALIGN);
  1053. l1:=BuildConstExpression(false,false);
  1054. if (target_info.system in [system_i386_GO32V2]) then
  1055. begin
  1056. l2:=1;
  1057. if (l1>=0) and (l1<=16) then
  1058. while (l1>0) do
  1059. begin
  1060. l2:=2*l2;
  1061. dec(l1);
  1062. end;
  1063. l1:=l2;
  1064. end;
  1065. ConcatAlign(curlist,l1);
  1066. Message(asmr_n_align_is_target_specific);
  1067. if actasmtoken<>AS_SEPARATOR then
  1068. Consume(AS_SEPARATOR);
  1069. end;
  1070. AS_BALIGN:
  1071. Begin
  1072. Consume(AS_BALIGN);
  1073. ConcatAlign(curlist,BuildConstExpression(false,false));
  1074. if actasmtoken<>AS_SEPARATOR then
  1075. Consume(AS_SEPARATOR);
  1076. end;
  1077. AS_P2ALIGN:
  1078. Begin
  1079. Consume(AS_P2ALIGN);
  1080. l1:=BuildConstExpression(false,false);
  1081. l2:=1;
  1082. if (l1>=0) and (l1<=16) then
  1083. while (l1>0) do
  1084. begin
  1085. l2:=2*l2;
  1086. dec(l1);
  1087. end;
  1088. l1:=l2;
  1089. ConcatAlign(curlist,l1);
  1090. if actasmtoken<>AS_SEPARATOR then
  1091. Consume(AS_SEPARATOR);
  1092. end;
  1093. AS_ASCIIZ:
  1094. Begin
  1095. Consume(AS_ASCIIZ);
  1096. BuildStringConstant(TRUE);
  1097. end;
  1098. AS_ASCII:
  1099. Begin
  1100. Consume(AS_ASCII);
  1101. BuildStringConstant(FALSE);
  1102. end;
  1103. AS_LCOMM:
  1104. Begin
  1105. Consume(AS_LCOMM);
  1106. commname:=actasmpattern;
  1107. Consume(AS_ID);
  1108. Consume(AS_COMMA);
  1109. curList.concat(Tai_datablock.Create(commname,BuildConstExpression(false,false)));
  1110. if actasmtoken<>AS_SEPARATOR then
  1111. Consume(AS_SEPARATOR);
  1112. end;
  1113. AS_COMM:
  1114. Begin
  1115. Consume(AS_COMM);
  1116. commname:=actasmpattern;
  1117. Consume(AS_ID);
  1118. Consume(AS_COMMA);
  1119. curList.concat(Tai_datablock.Create_global(commname,BuildConstExpression(false,false)));
  1120. if actasmtoken<>AS_SEPARATOR then
  1121. Consume(AS_SEPARATOR);
  1122. end;
  1123. AS_OPCODE:
  1124. Begin
  1125. HandleOpCode;
  1126. end;
  1127. AS_SEPARATOR:
  1128. Begin
  1129. Consume(AS_SEPARATOR);
  1130. end;
  1131. AS_RVA:
  1132. begin
  1133. { .rva generally applies to systems with COFF output format,
  1134. not just Windows. }
  1135. if not (target_info.system in systems_all_windows) then
  1136. Message1(asmr_e_unsupported_directive,token2str[AS_RVA]);
  1137. Consume(AS_RVA);
  1138. BuildRva;
  1139. end;
  1140. AS_SET:
  1141. begin
  1142. Consume(AS_SET);
  1143. BuildConstSymbolExpression(true,false,false, symofs,symname,symtyp);
  1144. Consume(AS_COMMA);
  1145. BuildConstSymbolExpression(true,false,false, symofs,symval,symtyp);
  1146. curList.concat(tai_set.create(symname,symval));
  1147. end;
  1148. AS_WEAK:
  1149. begin
  1150. Consume(AS_WEAK);
  1151. BuildConstSymbolExpression(true,false,false, l1,symname,symtyp);
  1152. curList.concat(tai_weak.create(symname));
  1153. end;
  1154. AS_SECTION:
  1155. begin
  1156. Consume(AS_SECTION);
  1157. new_section(curlist, sec_user, actasmpattern, 0);
  1158. //curList.concat(tai_section.create(sec_user, actasmpattern, 0));
  1159. consume(AS_STRING);
  1160. end;
  1161. AS_TARGET_DIRECTIVE:
  1162. HandleTargetDirective;
  1163. AS_END:
  1164. begin
  1165. break; { end assembly block }
  1166. end;
  1167. else
  1168. Begin
  1169. Message(asmr_e_syntax_error);
  1170. RecoverConsume(false);
  1171. end;
  1172. end;
  1173. until false;
  1174. { check that all referenced local labels are defined }
  1175. checklocallabels;
  1176. { are we back in the code section? }
  1177. if lasTSec<>sec_code then
  1178. begin
  1179. Message(asmr_w_assembler_code_not_returned_to_text);
  1180. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1181. end;
  1182. { Return the list in an asmnode }
  1183. assemble:=curlist;
  1184. Message1(asmr_d_finish_reading,'GNU AS');
  1185. end;
  1186. {*****************************************************************************
  1187. Parsing Helpers
  1188. *****************************************************************************}
  1189. Procedure tattreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  1190. { Description: This routine builds up a record offset after a AS_DOT }
  1191. { token is encountered. }
  1192. { On entry actasmtoken should be equal to AS_DOT }
  1193. var
  1194. s : string;
  1195. Begin
  1196. offset:=0;
  1197. size:=0;
  1198. s:=expr;
  1199. while (actasmtoken=AS_DOT) do
  1200. begin
  1201. Consume(AS_DOT);
  1202. { a record field could have the same name as a register }
  1203. if actasmtoken in [AS_ID,AS_REGISTER] then
  1204. begin
  1205. s:=s+'.'+actasmpattern;
  1206. consume(actasmtoken)
  1207. end
  1208. else
  1209. begin
  1210. Consume(AS_ID);
  1211. RecoverConsume(true);
  1212. break;
  1213. end;
  1214. end;
  1215. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs) then
  1216. Message(asmr_e_building_record_offset);
  1217. end;
  1218. procedure tattreader.BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  1219. var
  1220. hssymtyp : TAsmSymType;
  1221. hs,tempstr,expr,mangledname : string;
  1222. parenlevel : longint;
  1223. l,k : aint;
  1224. errorflag : boolean;
  1225. prevtok : tasmtoken;
  1226. sym : tsym;
  1227. srsymtable : TSymtable;
  1228. hl : tasmlabel;
  1229. Begin
  1230. asmsym:='';
  1231. asmsymtyp:=AT_DATA;
  1232. value:=0;
  1233. errorflag:=FALSE;
  1234. tempstr:='';
  1235. expr:='';
  1236. parenlevel:=0;
  1237. Repeat
  1238. Case actasmtoken of
  1239. AS_LPAREN:
  1240. Begin
  1241. { Exit if ref? }
  1242. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  1243. break;
  1244. Consume(AS_LPAREN);
  1245. expr:=expr + '(';
  1246. inc(parenlevel);
  1247. end;
  1248. AS_RBRACKET:
  1249. begin
  1250. if betweenbracket then
  1251. break;
  1252. { write error only once. }
  1253. if not errorflag then
  1254. Message(asmr_e_invalid_constant_expression);
  1255. { consume tokens until we find COMMA or SEPARATOR }
  1256. Consume(actasmtoken);
  1257. errorflag:=TRUE;
  1258. end;
  1259. AS_RPAREN:
  1260. Begin
  1261. { end of ref ? }
  1262. if (parenlevel=0) and betweenbracket then
  1263. break;
  1264. Consume(AS_RPAREN);
  1265. expr:=expr + ')';
  1266. dec(parenlevel);
  1267. end;
  1268. AS_SHL:
  1269. Begin
  1270. Consume(AS_SHL);
  1271. expr:=expr + '<';
  1272. end;
  1273. AS_SHR:
  1274. Begin
  1275. Consume(AS_SHR);
  1276. expr:=expr + '>';
  1277. end;
  1278. AS_SLASH:
  1279. Begin
  1280. Consume(AS_SLASH);
  1281. expr:=expr + '/';
  1282. end;
  1283. AS_MOD:
  1284. Begin
  1285. Consume(AS_MOD);
  1286. expr:=expr + '%';
  1287. end;
  1288. AS_STAR:
  1289. Begin
  1290. Consume(AS_STAR);
  1291. expr:=expr + '*';
  1292. end;
  1293. AS_PLUS:
  1294. Begin
  1295. Consume(AS_PLUS);
  1296. expr:=expr + '+';
  1297. end;
  1298. AS_MINUS:
  1299. Begin
  1300. Consume(AS_MINUS);
  1301. expr:=expr + '-';
  1302. end;
  1303. AS_AND:
  1304. Begin
  1305. Consume(AS_AND);
  1306. expr:=expr + '&';
  1307. end;
  1308. AS_NOT:
  1309. Begin
  1310. Consume(AS_NOT);
  1311. expr:=expr + '~';
  1312. end;
  1313. AS_XOR:
  1314. Begin
  1315. Consume(AS_XOR);
  1316. expr:=expr + '^';
  1317. end;
  1318. AS_OR:
  1319. Begin
  1320. Consume(AS_OR);
  1321. expr:=expr + '|';
  1322. end;
  1323. AS_INTNUM:
  1324. Begin
  1325. expr:=expr + actasmpattern;
  1326. Consume(AS_INTNUM);
  1327. end;
  1328. AS_DOLLAR:
  1329. begin
  1330. Consume(AS_DOLLAR);
  1331. if actasmtoken<>AS_ID then
  1332. Message(asmr_e_dollar_without_identifier);
  1333. end;
  1334. AS_STRING:
  1335. Begin
  1336. l:=0;
  1337. case Length(actasmpattern) of
  1338. 1 :
  1339. l:=ord(actasmpattern[1]);
  1340. 2 :
  1341. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1342. 3 :
  1343. l:=ord(actasmpattern[3]) +
  1344. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1345. 4 :
  1346. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1347. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1348. else
  1349. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1350. end;
  1351. str(l, tempstr);
  1352. expr:=expr + tempstr;
  1353. Consume(AS_STRING);
  1354. end;
  1355. AS_SIZEOF,
  1356. AS_TYPE:
  1357. begin
  1358. l:=0;
  1359. Consume(actasmtoken);
  1360. if actasmtoken<>AS_ID then
  1361. Message(asmr_e_type_without_identifier)
  1362. else
  1363. begin
  1364. tempstr:=actasmpattern;
  1365. Consume(AS_ID);
  1366. if actasmtoken=AS_DOT then
  1367. begin
  1368. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  1369. if mangledname<>'' then
  1370. Message(asmr_e_wrong_sym_type);
  1371. end
  1372. else
  1373. begin
  1374. searchsym(tempstr,sym,srsymtable);
  1375. if assigned(sym) then
  1376. begin
  1377. case sym.typ of
  1378. staticvarsym,
  1379. localvarsym,
  1380. paravarsym :
  1381. l:=tabstractvarsym(sym).getsize;
  1382. typesym :
  1383. l:=ttypesym(sym).typedef.size;
  1384. else
  1385. Message(asmr_e_wrong_sym_type);
  1386. end;
  1387. end
  1388. else
  1389. Message1(sym_e_unknown_id,tempstr);
  1390. end;
  1391. end;
  1392. str(l, tempstr);
  1393. expr:=expr + tempstr;
  1394. end;
  1395. AS_VMTOFFSET:
  1396. begin
  1397. Consume(actasmtoken);
  1398. if actasmtoken<>AS_ID then
  1399. Message(asmr_e_type_without_identifier)
  1400. else
  1401. begin
  1402. tempstr:=actasmpattern;
  1403. consume(AS_ID);
  1404. BuildRecordOffsetSize(tempstr,k,l,mangledname,true);
  1405. if (mangledname <> '') then
  1406. Message(asmr_e_wrong_sym_type);
  1407. str(k,tempstr);
  1408. expr := expr + tempstr;
  1409. end
  1410. end;
  1411. AS_ID:
  1412. Begin
  1413. hs:='';
  1414. hssymtyp:=AT_DATA;
  1415. tempstr:=actasmpattern;
  1416. prevtok:=prevasmtoken;
  1417. consume(AS_ID);
  1418. if SearchIConstant(tempstr,l) then
  1419. begin
  1420. str(l, tempstr);
  1421. expr:=expr + tempstr;
  1422. end
  1423. else
  1424. begin
  1425. if is_locallabel(tempstr) then
  1426. begin
  1427. CreateLocalLabel(tempstr,hl,false);
  1428. hs:=hl.name;
  1429. hssymtyp:=AT_LABEL;
  1430. end
  1431. else
  1432. if SearchLabel(tempstr,hl,false) then
  1433. begin
  1434. hs:=hl.name;
  1435. hssymtyp:=AT_FUNCTION;
  1436. end
  1437. else
  1438. begin
  1439. searchsym(tempstr,sym,srsymtable);
  1440. if assigned(sym) then
  1441. begin
  1442. case sym.typ of
  1443. staticvarsym :
  1444. begin
  1445. { we always assume in asm statements that }
  1446. { that the variable is valid. }
  1447. tabstractvarsym(sym).varstate:=vs_readwritten;
  1448. inc(tabstractvarsym(sym).refs);
  1449. { variable can't be placed in a register }
  1450. tabstractvarsym(sym).varregable:=vr_none;
  1451. { and anything may happen with its address }
  1452. tabstractvarsym(sym).addr_taken:=true;
  1453. hs:=tstaticvarsym(sym).mangledname;
  1454. end;
  1455. localvarsym,
  1456. paravarsym :
  1457. Message(asmr_e_no_local_or_para_allowed);
  1458. procsym :
  1459. begin
  1460. if Tprocsym(sym).ProcdefList.Count>1 then
  1461. Message(asmr_w_calling_overload_func);
  1462. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1463. hssymtyp:=AT_FUNCTION;
  1464. end;
  1465. typesym :
  1466. begin
  1467. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1468. Message(asmr_e_wrong_sym_type);
  1469. end;
  1470. else
  1471. Message(asmr_e_wrong_sym_type);
  1472. end;
  1473. end
  1474. else
  1475. Message1(sym_e_unknown_id,tempstr);
  1476. end;
  1477. { symbol found? }
  1478. if hs<>'' then
  1479. begin
  1480. if needofs and (prevtok<>AS_DOLLAR) then
  1481. Message(asmr_e_need_dollar);
  1482. if asmsym='' then
  1483. begin
  1484. asmsym:=hs;
  1485. asmsymtyp:=hssymtyp;
  1486. end
  1487. else
  1488. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1489. if (expr='') or (expr[length(expr)]='+') then
  1490. begin
  1491. { don't remove the + if there could be a record field }
  1492. if actasmtoken<>AS_DOT then
  1493. delete(expr,length(expr),1);
  1494. end
  1495. else
  1496. Message(asmr_e_only_add_relocatable_symbol);
  1497. end;
  1498. if actasmtoken=AS_DOT then
  1499. begin
  1500. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1501. if (hs<>'') then
  1502. hssymtyp:=AT_FUNCTION
  1503. else
  1504. begin
  1505. str(l, tempstr);
  1506. expr:=expr + tempstr;
  1507. end
  1508. end
  1509. else
  1510. begin
  1511. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1512. delete(expr,length(expr),1);
  1513. end;
  1514. end;
  1515. { check if there are wrong operator used like / or mod etc. }
  1516. if (hs<>'') and
  1517. not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,
  1518. AS_LPAREN,AS_RPAREN,AS_RBRACKET,AS_END]) then
  1519. Message(asmr_e_only_add_relocatable_symbol);
  1520. end;
  1521. AS_END,
  1522. AS_SEPARATOR,
  1523. AS_COMMA:
  1524. break;
  1525. else
  1526. Begin
  1527. { write error only once. }
  1528. if not errorflag then
  1529. Message(asmr_e_invalid_constant_expression);
  1530. { consume tokens until we find COMMA or SEPARATOR }
  1531. Consume(actasmtoken);
  1532. errorflag:=TRUE;
  1533. end;
  1534. end;
  1535. Until false;
  1536. { calculate expression }
  1537. if not ErrorFlag then
  1538. value:=CalculateExpression(expr)
  1539. else
  1540. value:=0;
  1541. end;
  1542. function tattreader.BuildConstExpression(allowref,betweenbracket:boolean): aint;
  1543. var
  1544. l : aint;
  1545. hs : string;
  1546. hssymtyp : TAsmSymType;
  1547. begin
  1548. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs,hssymtyp);
  1549. if hs<>'' then
  1550. Message(asmr_e_relocatable_symbol_not_allowed);
  1551. BuildConstExpression:=l;
  1552. end;
  1553. Procedure tattreader.BuildConstantOperand(oper : toperand);
  1554. var
  1555. l : aint;
  1556. tempstr : string;
  1557. tempsymtyp : TAsmSymType;
  1558. begin
  1559. BuildConstSymbolExpression(false,false,true,l,tempstr,tempsymtyp);
  1560. if tempstr<>'' then
  1561. begin
  1562. oper.opr.typ:=OPR_SYMBOL;
  1563. oper.opr.symofs:=l;
  1564. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr);
  1565. end
  1566. else
  1567. begin
  1568. oper.opr.typ:=OPR_CONSTANT;
  1569. oper.opr.val:=l;
  1570. end;
  1571. end;
  1572. procedure tattreader.BuildRva;
  1573. var
  1574. asmsymtyp : TAsmSymType;
  1575. asmsym: string;
  1576. value : aint;
  1577. ai:tai_const;
  1578. begin
  1579. repeat
  1580. case actasmtoken of
  1581. AS_INTNUM,
  1582. AS_PLUS,
  1583. AS_MINUS,
  1584. AS_LPAREN,
  1585. AS_ID :
  1586. Begin
  1587. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  1588. if asmsym<>'' then
  1589. begin
  1590. ai:=tai_const.create_type_sym(aitconst_rva_symbol,current_asmdata.RefAsmSymbol(asmsym));
  1591. ai.value:=value;
  1592. curlist.concat(ai);
  1593. end
  1594. else
  1595. Message(asmr_e_invalid_symbol_ref);
  1596. end;
  1597. AS_COMMA:
  1598. Consume(AS_COMMA);
  1599. AS_END,
  1600. AS_SEPARATOR:
  1601. break;
  1602. else
  1603. begin
  1604. Message(asmr_e_syn_constant);
  1605. RecoverConsume(false);
  1606. end
  1607. end; { end case }
  1608. until false;
  1609. end;
  1610. end.