raatt.pas 53 KB

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