raatt.pas 54 KB

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