raatt.pas 54 KB

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