raavrgas.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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_RParen;
  120. begin
  121. if actasmtoken<>AS_RPAREN then
  122. Begin
  123. Message(asmr_e_invalid_reference_syntax);
  124. RecoverConsume(true);
  125. end
  126. else
  127. begin
  128. Consume(AS_RPAREN);
  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_LPAREN);
  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_LPAREN then
  159. Begin
  160. Consume_RParen;
  161. exit;
  162. end;
  163. if actasmtoken=AS_PLUS then
  164. begin
  165. consume(AS_PLUS);
  166. oper.opr.ref.addressmode:=AM_POSTINCREMENT;
  167. end;
  168. end {end case }
  169. else
  170. Begin
  171. Message(asmr_e_invalid_reference_syntax);
  172. RecoverConsume(false);
  173. end;
  174. end;
  175. Procedure tavrattreader.BuildOperand(oper : tavroperand);
  176. var
  177. expr : string;
  178. typesize,l : aint;
  179. procedure AddLabelOperand(hl:tasmlabel);
  180. begin
  181. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) { and
  182. is_calljmp(actopcode) } then
  183. begin
  184. oper.opr.typ:=OPR_SYMBOL;
  185. oper.opr.symbol:=hl;
  186. end
  187. else
  188. begin
  189. oper.InitRef;
  190. oper.opr.ref.symbol:=hl;
  191. end;
  192. end;
  193. procedure MaybeRecordOffset;
  194. var
  195. mangledname: string;
  196. hasdot : boolean;
  197. l,
  198. toffset,
  199. tsize : aint;
  200. begin
  201. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  202. exit;
  203. l:=0;
  204. hasdot:=(actasmtoken=AS_DOT);
  205. if hasdot then
  206. begin
  207. if expr<>'' then
  208. begin
  209. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  210. if (oper.opr.typ<>OPR_CONSTANT) and
  211. (mangledname<>'') then
  212. Message(asmr_e_wrong_sym_type);
  213. inc(l,toffset);
  214. oper.SetSize(tsize,true);
  215. end;
  216. end;
  217. if actasmtoken in [AS_PLUS,AS_MINUS] then
  218. inc(l,BuildConstExpression(true,false));
  219. case oper.opr.typ of
  220. OPR_LOCAL :
  221. begin
  222. { don't allow direct access to fields of parameters, because that
  223. will generate buggy code. Allow it only for explicit typecasting }
  224. if hasdot and
  225. (not oper.hastype) and
  226. (tabstractnormalvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  227. (current_procinfo.procdef.proccalloption<>pocall_register) then
  228. Message(asmr_e_cannot_access_field_directly_for_parameters);
  229. inc(oper.opr.localsymofs,l)
  230. end;
  231. OPR_CONSTANT :
  232. inc(oper.opr.val,l);
  233. OPR_REFERENCE :
  234. if (mangledname<>'') then
  235. begin
  236. if (oper.opr.val<>0) then
  237. Message(asmr_e_wrong_sym_type);
  238. oper.opr.typ:=OPR_SYMBOL;
  239. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname);
  240. end
  241. else
  242. inc(oper.opr.val,l);
  243. OPR_SYMBOL:
  244. Message(asmr_e_invalid_symbol_ref);
  245. else
  246. internalerror(200309221);
  247. end;
  248. end;
  249. function MaybeBuildReference:boolean;
  250. { Try to create a reference, if not a reference is found then false
  251. is returned }
  252. begin
  253. MaybeBuildReference:=true;
  254. case actasmtoken of
  255. AS_INTNUM,
  256. AS_MINUS,
  257. AS_PLUS:
  258. Begin
  259. oper.opr.ref.offset:=BuildConstExpression(True,False);
  260. if actasmtoken<>AS_LPAREN then
  261. Message(asmr_e_invalid_reference_syntax)
  262. else
  263. BuildReference(oper);
  264. end;
  265. AS_LPAREN:
  266. BuildReference(oper);
  267. AS_ID: { only a variable is allowed ... }
  268. Begin
  269. ReadSym(oper);
  270. case actasmtoken of
  271. AS_END,
  272. AS_SEPARATOR,
  273. AS_COMMA: ;
  274. AS_LPAREN:
  275. BuildReference(oper);
  276. else
  277. Begin
  278. Message(asmr_e_invalid_reference_syntax);
  279. Consume(actasmtoken);
  280. end;
  281. end; {end case }
  282. end;
  283. else
  284. MaybeBuildReference:=false;
  285. end; { end case }
  286. end;
  287. var
  288. tempreg : tregister;
  289. ireg : tsuperregister;
  290. hl : tasmlabel;
  291. ofs : longint;
  292. registerset : tcpuregisterset;
  293. Begin
  294. expr:='';
  295. case actasmtoken of
  296. AS_LBRACKET: { Memory reference or constant expression }
  297. Begin
  298. oper.InitRef;
  299. BuildReference(oper);
  300. end;
  301. AS_INTNUM,
  302. AS_MINUS,
  303. AS_PLUS:
  304. Begin
  305. { Constant memory offset }
  306. { This must absolutely be followed by ( }
  307. oper.InitRef;
  308. oper.opr.ref.offset:=BuildConstExpression(True,False);
  309. { absolute memory addresss? }
  310. if actopcode in [A_LDS,A_STS] then
  311. BuildReference(oper)
  312. else
  313. begin
  314. ofs:=oper.opr.ref.offset;
  315. BuildConstantOperand(oper);
  316. inc(oper.opr.val,ofs);
  317. end;
  318. end;
  319. AS_ID: { A constant expression, or a Variable ref. }
  320. Begin
  321. { Local Label ? }
  322. if is_locallabel(actasmpattern) then
  323. begin
  324. CreateLocalLabel(actasmpattern,hl,false);
  325. Consume(AS_ID);
  326. AddLabelOperand(hl);
  327. end
  328. else
  329. { Check for label }
  330. if SearchLabel(actasmpattern,hl,false) then
  331. begin
  332. Consume(AS_ID);
  333. AddLabelOperand(hl);
  334. end
  335. else
  336. { probably a variable or normal expression }
  337. { or a procedure (such as in CALL ID) }
  338. Begin
  339. { is it a constant ? }
  340. if SearchIConstant(actasmpattern,l) then
  341. Begin
  342. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  343. Message(asmr_e_invalid_operand_type);
  344. BuildConstantOperand(oper);
  345. end
  346. else
  347. begin
  348. expr:=actasmpattern;
  349. Consume(AS_ID);
  350. { typecasting? }
  351. if (actasmtoken=AS_LPAREN) and
  352. SearchType(expr,typesize) then
  353. begin
  354. oper.hastype:=true;
  355. Consume(AS_LPAREN);
  356. BuildOperand(oper);
  357. Consume(AS_RPAREN);
  358. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  359. oper.SetSize(typesize,true);
  360. end
  361. else
  362. begin
  363. if not(oper.SetupVar(expr,false)) then
  364. Begin
  365. { look for special symbols ... }
  366. if expr= '__HIGH' then
  367. begin
  368. consume(AS_LPAREN);
  369. if not oper.setupvar('high'+actasmpattern,false) then
  370. Message1(sym_e_unknown_id,'high'+actasmpattern);
  371. consume(AS_ID);
  372. consume(AS_RPAREN);
  373. end
  374. else
  375. if expr = '__RESULT' then
  376. oper.SetUpResult
  377. else
  378. if expr = '__SELF' then
  379. oper.SetupSelf
  380. else
  381. if expr = '__OLDEBP' then
  382. oper.SetupOldEBP
  383. else
  384. Message1(sym_e_unknown_id,expr);
  385. end;
  386. end;
  387. end;
  388. if actasmtoken=AS_DOT then
  389. MaybeRecordOffset;
  390. { add a constant expression? }
  391. if (actasmtoken=AS_PLUS) then
  392. begin
  393. l:=BuildConstExpression(true,false);
  394. case oper.opr.typ of
  395. OPR_CONSTANT :
  396. inc(oper.opr.val,l);
  397. OPR_LOCAL :
  398. inc(oper.opr.localsymofs,l);
  399. OPR_REFERENCE :
  400. inc(oper.opr.ref.offset,l);
  401. else
  402. internalerror(200309202);
  403. end;
  404. end
  405. end;
  406. { Do we have a indexing reference, then parse it also }
  407. if actasmtoken=AS_LPAREN then
  408. BuildReference(oper);
  409. end;
  410. { Register, a variable reference or a constant reference }
  411. AS_REGISTER:
  412. Begin
  413. { save the type of register used. }
  414. tempreg:=actasmregister;
  415. Consume(AS_REGISTER);
  416. if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  417. Begin
  418. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  419. Message(asmr_e_invalid_operand_type);
  420. oper.opr.typ:=OPR_REGISTER;
  421. oper.opr.reg:=tempreg;
  422. end
  423. else
  424. Message(asmr_e_syn_operand);
  425. end;
  426. AS_END,
  427. AS_SEPARATOR,
  428. AS_COMMA: ;
  429. else
  430. Begin
  431. Message(asmr_e_syn_operand);
  432. Consume(actasmtoken);
  433. end;
  434. end; { end case }
  435. end;
  436. {*****************************************************************************
  437. tavrattreader
  438. *****************************************************************************}
  439. procedure tavrattreader.BuildOpCode(instr : tavrinstruction);
  440. var
  441. operandnum : longint;
  442. Begin
  443. { opcode }
  444. if (actasmtoken<>AS_OPCODE) then
  445. Begin
  446. Message(asmr_e_invalid_or_missing_opcode);
  447. RecoverConsume(true);
  448. exit;
  449. end;
  450. { Fill the instr object with the current state }
  451. with instr do
  452. begin
  453. Opcode:=ActOpcode;
  454. condition:=ActCondition;
  455. end;
  456. { We are reading operands, so opcode will be an AS_ID }
  457. operandnum:=1;
  458. Consume(AS_OPCODE);
  459. { Zero operand opcode ? }
  460. if actasmtoken in [AS_SEPARATOR,AS_END] then
  461. begin
  462. operandnum:=0;
  463. exit;
  464. end;
  465. { Read the operands }
  466. repeat
  467. case actasmtoken of
  468. AS_COMMA: { Operand delimiter }
  469. Begin
  470. if operandnum>Max_Operands then
  471. Message(asmr_e_too_many_operands)
  472. else
  473. Inc(operandnum);
  474. Consume(AS_COMMA);
  475. end;
  476. AS_SEPARATOR,
  477. AS_END : { End of asm operands for this opcode }
  478. begin
  479. break;
  480. end;
  481. else
  482. BuildOperand(instr.Operands[operandnum] as tavroperand);
  483. end; { end case }
  484. until false;
  485. instr.Ops:=operandnum;
  486. end;
  487. function tavrattreader.is_asmopcode(const s: string):boolean;
  488. const
  489. { sorted by length so longer postfixes will match first }
  490. postfix2strsorted : array[1..19] of string[2] = (
  491. 'EP','SB','BT','SH',
  492. 'IA','IB','DA','DB','FD','FA','ED','EA',
  493. 'B','D','E','P','T','H','S');
  494. var
  495. len,
  496. j,
  497. sufidx : longint;
  498. hs : string;
  499. maxlen : longint;
  500. icond : tasmcond;
  501. Begin
  502. { making s a value parameter would break other assembler readers }
  503. hs:=s;
  504. is_asmopcode:=false;
  505. { clear op code }
  506. actopcode:=A_None;
  507. actcondition:=C_None;
  508. { first, handle B else BLS is read wrong }
  509. if ((copy(hs,1,2)='BR') and (length(hs)=4)) then
  510. begin
  511. for icond:=low(tasmcond) to high(tasmcond) do
  512. begin
  513. if copy(hs,2,3)=uppercond2str[icond] then
  514. begin
  515. actopcode:=A_BRxx;
  516. actasmtoken:=AS_OPCODE;
  517. actcondition:=icond;
  518. is_asmopcode:=true;
  519. exit;
  520. end;
  521. end;
  522. end;
  523. maxlen:=max(length(hs),5);
  524. actopcode:=A_NONE;
  525. for j:=maxlen downto 1 do
  526. begin
  527. actopcode:=tasmop(PtrInt(iasmops.Find(copy(hs,1,j))));
  528. if actopcode<>A_NONE then
  529. begin
  530. actasmtoken:=AS_OPCODE;
  531. { strip op code }
  532. delete(hs,1,j);
  533. break;
  534. end;
  535. end;
  536. if actopcode=A_NONE then
  537. exit;
  538. { search for condition, conditions are always 2 chars }
  539. if length(hs)>1 then
  540. begin
  541. for icond:=low(tasmcond) to high(tasmcond) do
  542. begin
  543. if copy(hs,1,2)=uppercond2str[icond] then
  544. begin
  545. actcondition:=icond;
  546. { strip condition }
  547. delete(hs,1,2);
  548. break;
  549. end;
  550. end;
  551. end;
  552. { if we stripped all postfixes, it's a valid opcode }
  553. is_asmopcode:=length(hs)=0;
  554. end;
  555. procedure tavrattreader.ConvertCalljmp(instr : tavrinstruction);
  556. var
  557. newopr : toprrec;
  558. begin
  559. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  560. begin
  561. newopr.typ:=OPR_SYMBOL;
  562. newopr.symbol:=instr.Operands[1].opr.ref.symbol;
  563. newopr.symofs:=instr.Operands[1].opr.ref.offset;
  564. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  565. (instr.Operands[1].opr.ref.index<>NR_NO) then
  566. Message(asmr_e_syn_operand);
  567. instr.Operands[1].opr:=newopr;
  568. end;
  569. end;
  570. procedure tavrattreader.handleopcode;
  571. var
  572. instr : tavrinstruction;
  573. begin
  574. instr:=tavrinstruction.Create(tavroperand);
  575. BuildOpcode(instr);
  576. { if is_calljmp(instr.opcode) then
  577. ConvertCalljmp(instr); }
  578. {
  579. instr.AddReferenceSizes;
  580. instr.SetInstructionOpsize;
  581. instr.CheckOperandSizes;
  582. }
  583. instr.ConcatInstruction(curlist);
  584. instr.Free;
  585. end;
  586. {*****************************************************************************
  587. Initialize
  588. *****************************************************************************}
  589. const
  590. asmmode_avr_att_info : tasmmodeinfo =
  591. (
  592. id : asmmode_avr_gas;
  593. idtxt : 'GAS';
  594. casmreader : tavrattreader;
  595. );
  596. asmmode_avr_standard_info : tasmmodeinfo =
  597. (
  598. id : asmmode_standard;
  599. idtxt : 'STANDARD';
  600. casmreader : tavrattreader;
  601. );
  602. initialization
  603. RegisterAsmMode(asmmode_avr_att_info);
  604. RegisterAsmMode(asmmode_avr_standard_info);
  605. end.