raatt.pas 50 KB

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