raatt.pas 50 KB

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