raatt.pas 48 KB

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