raatt.pas 54 KB

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