raatt.pas 45 KB

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