raatt.pas 54 KB

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