raatt.pas 47 KB

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