raavrgas.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. {
  2. Copyright (c) 1998-2008 by Carl Eric Codere and Peter Vreman
  3. Does the parsing for the ARM GNU AS 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 raavrgas;
  18. {$i fpcdefs.inc}
  19. Interface
  20. uses
  21. raatt,raavr,
  22. cpubase;
  23. type
  24. tavrattreader = class(tattreader)
  25. function is_asmopcode(const s: string):boolean;override;
  26. function is_register(const s:string):boolean;override;
  27. procedure handleopcode;override;
  28. procedure BuildReference(oper : tavroperand);
  29. procedure BuildOperand(oper : tavroperand);
  30. procedure BuildOpCode(instr : tavrinstruction);
  31. procedure ReadSym(oper : tavroperand);
  32. procedure ConvertCalljmp(instr : tavrinstruction);
  33. end;
  34. Implementation
  35. uses
  36. { helpers }
  37. cutils,
  38. { global }
  39. globtype,globals,verbose,
  40. systems,
  41. { aasm }
  42. cpuinfo,aasmbase,aasmtai,aasmdata,aasmcpu,
  43. { symtable }
  44. symconst,symbase,symtype,symsym,symtable,
  45. { parser }
  46. scanner,
  47. procinfo,
  48. itcpugas,
  49. rabase,rautils,
  50. cgbase,cgobj
  51. ;
  52. function tavrattreader.is_register(const s:string):boolean;
  53. type
  54. treg2str = record
  55. name : string[2];
  56. reg : tregister;
  57. end;
  58. {
  59. const
  60. extraregs : array[0..19] of treg2str = (
  61. (name: 'X'; reg : NR_Z),
  62. (name: 'Y'; reg : NR_R1),
  63. (name: 'Z'; reg : NR_R2),
  64. );
  65. }
  66. var
  67. i : longint;
  68. begin
  69. result:=inherited is_register(s);
  70. { reg found?
  71. possible aliases are always 2 char
  72. }
  73. if result or (length(s)<>2) then
  74. exit;
  75. {
  76. for i:=low(extraregs) to high(extraregs) do
  77. begin
  78. if s=extraregs[i].name then
  79. begin
  80. actasmregister:=extraregs[i].reg;
  81. result:=true;
  82. actasmtoken:=AS_REGISTER;
  83. exit;
  84. end;
  85. end;
  86. }
  87. end;
  88. procedure tavrattreader.ReadSym(oper : tavroperand);
  89. var
  90. tempstr, mangledname : string;
  91. typesize,l,k : aint;
  92. begin
  93. tempstr:=actasmpattern;
  94. Consume(AS_ID);
  95. { typecasting? }
  96. if (actasmtoken=AS_LPAREN) and
  97. SearchType(tempstr,typesize) then
  98. begin
  99. oper.hastype:=true;
  100. Consume(AS_LPAREN);
  101. BuildOperand(oper);
  102. Consume(AS_RPAREN);
  103. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  104. oper.SetSize(typesize,true);
  105. end
  106. else
  107. if not oper.SetupVar(tempstr,false) then
  108. Message1(sym_e_unknown_id,tempstr);
  109. { record.field ? }
  110. if actasmtoken=AS_DOT then
  111. begin
  112. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  113. if (mangledname<>'') then
  114. Message(asmr_e_invalid_reference_syntax);
  115. inc(oper.opr.ref.offset,l);
  116. end;
  117. end;
  118. Procedure tavrattreader.BuildReference(oper : tavroperand);
  119. procedure Consume_RBracket;
  120. begin
  121. if actasmtoken<>AS_RBRACKET then
  122. Begin
  123. Message(asmr_e_invalid_reference_syntax);
  124. RecoverConsume(true);
  125. end
  126. else
  127. begin
  128. Consume(AS_RBRACKET);
  129. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
  130. Begin
  131. Message(asmr_e_invalid_reference_syntax);
  132. RecoverConsume(true);
  133. end;
  134. end;
  135. end;
  136. procedure read_index;
  137. begin
  138. Consume(AS_COMMA);
  139. if actasmtoken=AS_REGISTER then
  140. Begin
  141. oper.opr.ref.index:=actasmregister;
  142. Consume(AS_REGISTER);
  143. end
  144. else if actasmtoken=AS_HASH then
  145. begin
  146. Consume(AS_HASH);
  147. inc(oper.opr.ref.offset,BuildConstExpression(false,true));
  148. end;
  149. end;
  150. begin
  151. Consume(AS_LBRACKET);
  152. if actasmtoken=AS_REGISTER then
  153. begin
  154. oper.opr.ref.base:=actasmregister;
  155. Consume(AS_REGISTER);
  156. { can either be a register or a right parenthesis }
  157. { (reg) }
  158. if actasmtoken=AS_RBRACKET then
  159. Begin
  160. Consume_RBracket;
  161. oper.opr.ref.addressmode:=AM_POSTINDEXED;
  162. if actasmtoken=AS_COMMA then
  163. read_index;
  164. exit;
  165. end;
  166. if actasmtoken=AS_COMMA then
  167. begin
  168. read_index;
  169. Consume_RBracket;
  170. end;
  171. if actasmtoken=AS_NOT then
  172. begin
  173. consume(AS_NOT);
  174. oper.opr.ref.addressmode:=AM_PREINDEXED;
  175. end;
  176. end {end case }
  177. else
  178. Begin
  179. Message(asmr_e_invalid_reference_syntax);
  180. RecoverConsume(false);
  181. end;
  182. end;
  183. Procedure tavrattreader.BuildOperand(oper : tavroperand);
  184. var
  185. expr : string;
  186. typesize,l : aint;
  187. procedure AddLabelOperand(hl:tasmlabel);
  188. begin
  189. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) { and
  190. is_calljmp(actopcode) } then
  191. begin
  192. oper.opr.typ:=OPR_SYMBOL;
  193. oper.opr.symbol:=hl;
  194. end
  195. else
  196. begin
  197. oper.InitRef;
  198. oper.opr.ref.symbol:=hl;
  199. end;
  200. end;
  201. procedure MaybeRecordOffset;
  202. var
  203. mangledname: string;
  204. hasdot : boolean;
  205. l,
  206. toffset,
  207. tsize : aint;
  208. begin
  209. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  210. exit;
  211. l:=0;
  212. hasdot:=(actasmtoken=AS_DOT);
  213. if hasdot then
  214. begin
  215. if expr<>'' then
  216. begin
  217. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  218. if (oper.opr.typ<>OPR_CONSTANT) and
  219. (mangledname<>'') then
  220. Message(asmr_e_wrong_sym_type);
  221. inc(l,toffset);
  222. oper.SetSize(tsize,true);
  223. end;
  224. end;
  225. if actasmtoken in [AS_PLUS,AS_MINUS] then
  226. inc(l,BuildConstExpression(true,false));
  227. case oper.opr.typ of
  228. OPR_LOCAL :
  229. begin
  230. { don't allow direct access to fields of parameters, because that
  231. will generate buggy code. Allow it only for explicit typecasting }
  232. if hasdot and
  233. (not oper.hastype) and
  234. (tabstractnormalvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  235. (current_procinfo.procdef.proccalloption<>pocall_register) then
  236. Message(asmr_e_cannot_access_field_directly_for_parameters);
  237. inc(oper.opr.localsymofs,l)
  238. end;
  239. OPR_CONSTANT :
  240. inc(oper.opr.val,l);
  241. OPR_REFERENCE :
  242. if (mangledname<>'') then
  243. begin
  244. if (oper.opr.val<>0) then
  245. Message(asmr_e_wrong_sym_type);
  246. oper.opr.typ:=OPR_SYMBOL;
  247. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname);
  248. end
  249. else
  250. inc(oper.opr.val,l);
  251. OPR_SYMBOL:
  252. Message(asmr_e_invalid_symbol_ref);
  253. else
  254. internalerror(200309221);
  255. end;
  256. end;
  257. function MaybeBuildReference:boolean;
  258. { Try to create a reference, if not a reference is found then false
  259. is returned }
  260. begin
  261. MaybeBuildReference:=true;
  262. case actasmtoken of
  263. AS_INTNUM,
  264. AS_MINUS,
  265. AS_PLUS:
  266. Begin
  267. oper.opr.ref.offset:=BuildConstExpression(True,False);
  268. if actasmtoken<>AS_LPAREN then
  269. Message(asmr_e_invalid_reference_syntax)
  270. else
  271. BuildReference(oper);
  272. end;
  273. AS_LPAREN:
  274. BuildReference(oper);
  275. AS_ID: { only a variable is allowed ... }
  276. Begin
  277. ReadSym(oper);
  278. case actasmtoken of
  279. AS_END,
  280. AS_SEPARATOR,
  281. AS_COMMA: ;
  282. AS_LPAREN:
  283. BuildReference(oper);
  284. else
  285. Begin
  286. Message(asmr_e_invalid_reference_syntax);
  287. Consume(actasmtoken);
  288. end;
  289. end; {end case }
  290. end;
  291. else
  292. MaybeBuildReference:=false;
  293. end; { end case }
  294. end;
  295. var
  296. tempreg : tregister;
  297. ireg : tsuperregister;
  298. hl : tasmlabel;
  299. ofs : longint;
  300. registerset : tcpuregisterset;
  301. Begin
  302. expr:='';
  303. case actasmtoken of
  304. AS_LBRACKET: { Memory reference or constant expression }
  305. Begin
  306. oper.InitRef;
  307. BuildReference(oper);
  308. end;
  309. AS_HASH: { Constant expression }
  310. Begin
  311. Consume(AS_HASH);
  312. BuildConstantOperand(oper);
  313. end;
  314. (*
  315. AS_INTNUM,
  316. AS_MINUS,
  317. AS_PLUS:
  318. Begin
  319. { Constant memory offset }
  320. { This must absolutely be followed by ( }
  321. oper.InitRef;
  322. oper.opr.ref.offset:=BuildConstExpression(True,False);
  323. if actasmtoken<>AS_LPAREN then
  324. begin
  325. ofs:=oper.opr.ref.offset;
  326. BuildConstantOperand(oper);
  327. inc(oper.opr.val,ofs);
  328. end
  329. else
  330. BuildReference(oper);
  331. end;
  332. *)
  333. AS_ID: { A constant expression, or a Variable ref. }
  334. Begin
  335. { Local Label ? }
  336. if is_locallabel(actasmpattern) then
  337. begin
  338. CreateLocalLabel(actasmpattern,hl,false);
  339. Consume(AS_ID);
  340. AddLabelOperand(hl);
  341. end
  342. else
  343. { Check for label }
  344. if SearchLabel(actasmpattern,hl,false) then
  345. begin
  346. Consume(AS_ID);
  347. AddLabelOperand(hl);
  348. end
  349. else
  350. { probably a variable or normal expression }
  351. { or a procedure (such as in CALL ID) }
  352. Begin
  353. { is it a constant ? }
  354. if SearchIConstant(actasmpattern,l) then
  355. Begin
  356. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  357. Message(asmr_e_invalid_operand_type);
  358. BuildConstantOperand(oper);
  359. end
  360. else
  361. begin
  362. expr:=actasmpattern;
  363. Consume(AS_ID);
  364. { typecasting? }
  365. if (actasmtoken=AS_LPAREN) and
  366. SearchType(expr,typesize) then
  367. begin
  368. oper.hastype:=true;
  369. Consume(AS_LPAREN);
  370. BuildOperand(oper);
  371. Consume(AS_RPAREN);
  372. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  373. oper.SetSize(typesize,true);
  374. end
  375. else
  376. begin
  377. if not(oper.SetupVar(expr,false)) then
  378. Begin
  379. { look for special symbols ... }
  380. if expr= '__HIGH' then
  381. begin
  382. consume(AS_LPAREN);
  383. if not oper.setupvar('high'+actasmpattern,false) then
  384. Message1(sym_e_unknown_id,'high'+actasmpattern);
  385. consume(AS_ID);
  386. consume(AS_RPAREN);
  387. end
  388. else
  389. if expr = '__RESULT' then
  390. oper.SetUpResult
  391. else
  392. if expr = '__SELF' then
  393. oper.SetupSelf
  394. else
  395. if expr = '__OLDEBP' then
  396. oper.SetupOldEBP
  397. else
  398. Message1(sym_e_unknown_id,expr);
  399. end;
  400. end;
  401. end;
  402. if actasmtoken=AS_DOT then
  403. MaybeRecordOffset;
  404. { add a constant expression? }
  405. if (actasmtoken=AS_PLUS) then
  406. begin
  407. l:=BuildConstExpression(true,false);
  408. case oper.opr.typ of
  409. OPR_CONSTANT :
  410. inc(oper.opr.val,l);
  411. OPR_LOCAL :
  412. inc(oper.opr.localsymofs,l);
  413. OPR_REFERENCE :
  414. inc(oper.opr.ref.offset,l);
  415. else
  416. internalerror(200309202);
  417. end;
  418. end
  419. end;
  420. { Do we have a indexing reference, then parse it also }
  421. if actasmtoken=AS_LPAREN then
  422. BuildReference(oper);
  423. end;
  424. { Register, a variable reference or a constant reference }
  425. AS_REGISTER:
  426. Begin
  427. { save the type of register used. }
  428. tempreg:=actasmregister;
  429. Consume(AS_REGISTER);
  430. if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  431. Begin
  432. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  433. Message(asmr_e_invalid_operand_type);
  434. oper.opr.typ:=OPR_REGISTER;
  435. oper.opr.reg:=tempreg;
  436. end
  437. else
  438. Message(asmr_e_syn_operand);
  439. end;
  440. AS_END,
  441. AS_SEPARATOR,
  442. AS_COMMA: ;
  443. else
  444. Begin
  445. Message(asmr_e_syn_operand);
  446. Consume(actasmtoken);
  447. end;
  448. end; { end case }
  449. end;
  450. {*****************************************************************************
  451. tavrattreader
  452. *****************************************************************************}
  453. procedure tavrattreader.BuildOpCode(instr : tavrinstruction);
  454. var
  455. operandnum : longint;
  456. Begin
  457. { opcode }
  458. if (actasmtoken<>AS_OPCODE) then
  459. Begin
  460. Message(asmr_e_invalid_or_missing_opcode);
  461. RecoverConsume(true);
  462. exit;
  463. end;
  464. { Fill the instr object with the current state }
  465. with instr do
  466. begin
  467. Opcode:=ActOpcode;
  468. condition:=ActCondition;
  469. end;
  470. { We are reading operands, so opcode will be an AS_ID }
  471. operandnum:=1;
  472. Consume(AS_OPCODE);
  473. { Zero operand opcode ? }
  474. if actasmtoken in [AS_SEPARATOR,AS_END] then
  475. begin
  476. operandnum:=0;
  477. exit;
  478. end;
  479. { Read the operands }
  480. repeat
  481. case actasmtoken of
  482. AS_COMMA: { Operand delimiter }
  483. Begin
  484. if operandnum>Max_Operands then
  485. Message(asmr_e_too_many_operands)
  486. else
  487. Inc(operandnum);
  488. Consume(AS_COMMA);
  489. end;
  490. AS_SEPARATOR,
  491. AS_END : { End of asm operands for this opcode }
  492. begin
  493. break;
  494. end;
  495. else
  496. BuildOperand(instr.Operands[operandnum] as tavroperand);
  497. end; { end case }
  498. until false;
  499. instr.Ops:=operandnum;
  500. end;
  501. function tavrattreader.is_asmopcode(const s: string):boolean;
  502. const
  503. { sorted by length so longer postfixes will match first }
  504. postfix2strsorted : array[1..19] of string[2] = (
  505. 'EP','SB','BT','SH',
  506. 'IA','IB','DA','DB','FD','FA','ED','EA',
  507. 'B','D','E','P','T','H','S');
  508. var
  509. len,
  510. j,
  511. sufidx : longint;
  512. hs : string;
  513. maxlen : longint;
  514. icond : tasmcond;
  515. Begin
  516. { making s a value parameter would break other assembler readers }
  517. hs:=s;
  518. is_asmopcode:=false;
  519. { clear op code }
  520. actopcode:=A_None;
  521. actcondition:=C_None;
  522. { first, handle B else BLS is read wrong }
  523. if ((copy(hs,1,2)='BR') and (length(hs)=4)) then
  524. begin
  525. for icond:=low(tasmcond) to high(tasmcond) do
  526. begin
  527. if copy(hs,2,3)=uppercond2str[icond] then
  528. begin
  529. actopcode:=A_BRxx;
  530. actasmtoken:=AS_OPCODE;
  531. actcondition:=icond;
  532. is_asmopcode:=true;
  533. exit;
  534. end;
  535. end;
  536. end;
  537. maxlen:=max(length(hs),5);
  538. actopcode:=A_NONE;
  539. for j:=maxlen downto 1 do
  540. begin
  541. actopcode:=tasmop(PtrInt(iasmops.Find(copy(hs,1,j))));
  542. if actopcode<>A_NONE then
  543. begin
  544. actasmtoken:=AS_OPCODE;
  545. { strip op code }
  546. delete(hs,1,j);
  547. break;
  548. end;
  549. end;
  550. if actopcode=A_NONE then
  551. exit;
  552. { search for condition, conditions are always 2 chars }
  553. if length(hs)>1 then
  554. begin
  555. for icond:=low(tasmcond) to high(tasmcond) do
  556. begin
  557. if copy(hs,1,2)=uppercond2str[icond] then
  558. begin
  559. actcondition:=icond;
  560. { strip condition }
  561. delete(hs,1,2);
  562. break;
  563. end;
  564. end;
  565. end;
  566. { if we stripped all postfixes, it's a valid opcode }
  567. is_asmopcode:=length(hs)=0;
  568. end;
  569. procedure tavrattreader.ConvertCalljmp(instr : tavrinstruction);
  570. var
  571. newopr : toprrec;
  572. begin
  573. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  574. begin
  575. newopr.typ:=OPR_SYMBOL;
  576. newopr.symbol:=instr.Operands[1].opr.ref.symbol;
  577. newopr.symofs:=instr.Operands[1].opr.ref.offset;
  578. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  579. (instr.Operands[1].opr.ref.index<>NR_NO) then
  580. Message(asmr_e_syn_operand);
  581. instr.Operands[1].opr:=newopr;
  582. end;
  583. end;
  584. procedure tavrattreader.handleopcode;
  585. var
  586. instr : tavrinstruction;
  587. begin
  588. instr:=tavrinstruction.Create(tavroperand);
  589. BuildOpcode(instr);
  590. { if is_calljmp(instr.opcode) then
  591. ConvertCalljmp(instr); }
  592. {
  593. instr.AddReferenceSizes;
  594. instr.SetInstructionOpsize;
  595. instr.CheckOperandSizes;
  596. }
  597. instr.ConcatInstruction(curlist);
  598. instr.Free;
  599. end;
  600. {*****************************************************************************
  601. Initialize
  602. *****************************************************************************}
  603. const
  604. asmmode_avr_att_info : tasmmodeinfo =
  605. (
  606. id : asmmode_avr_gas;
  607. idtxt : 'GAS';
  608. casmreader : tavrattreader;
  609. );
  610. asmmode_avr_standard_info : tasmmodeinfo =
  611. (
  612. id : asmmode_standard;
  613. idtxt : 'STANDARD';
  614. casmreader : tavrattreader;
  615. );
  616. initialization
  617. RegisterAsmMode(asmmode_avr_att_info);
  618. RegisterAsmMode(asmmode_avr_standard_info);
  619. end.