raarmgas.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. {
  2. Copyright (c) 1998-2002 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 raarmgas;
  18. {$i fpcdefs.inc}
  19. Interface
  20. uses
  21. raatt,raarm,
  22. cpubase;
  23. type
  24. tarmattreader = class(tattreader)
  25. actoppostfix : TOpPostfix;
  26. function is_asmopcode(const s: string):boolean;override;
  27. function is_register(const s:string):boolean;override;
  28. procedure handleopcode;override;
  29. procedure BuildReference(oper : tarmoperand);
  30. procedure BuildOperand(oper : tarmoperand);
  31. function TryBuildShifterOp(oper : tarmoperand) : boolean;
  32. procedure BuildOpCode(instr : tarminstruction);
  33. procedure ReadSym(oper : tarmoperand);
  34. procedure ConvertCalljmp(instr : tarminstruction);
  35. end;
  36. Implementation
  37. uses
  38. { helpers }
  39. cutils,
  40. { global }
  41. globtype,globals,verbose,
  42. systems,
  43. { aasm }
  44. cpuinfo,aasmbase,aasmtai,aasmdata,aasmcpu,
  45. { symtable }
  46. symconst,symbase,symtype,symsym,symtable,
  47. { parser }
  48. scanner,
  49. procinfo,
  50. itcpugas,
  51. rabase,rautils,
  52. cgbase,cgobj
  53. ;
  54. function tarmattreader.is_register(const s:string):boolean;
  55. type
  56. treg2str = record
  57. name : string[2];
  58. reg : tregister;
  59. end;
  60. const
  61. extraregs : array[0..19] of treg2str = (
  62. (name: 'A1'; reg : NR_R0),
  63. (name: 'A2'; reg : NR_R1),
  64. (name: 'A3'; reg : NR_R2),
  65. (name: 'A4'; reg : NR_R3),
  66. (name: 'V1'; reg : NR_R4),
  67. (name: 'V2'; reg : NR_R5),
  68. (name: 'V3'; reg : NR_R6),
  69. (name: 'V4'; reg : NR_R7),
  70. (name: 'V5'; reg : NR_R8),
  71. (name: 'V6'; reg : NR_R9),
  72. (name: 'V7'; reg : NR_R10),
  73. (name: 'V8'; reg : NR_R11),
  74. (name: 'WR'; reg : NR_R7),
  75. (name: 'SB'; reg : NR_R9),
  76. (name: 'SL'; reg : NR_R10),
  77. (name: 'FP'; reg : NR_R11),
  78. (name: 'IP'; reg : NR_R12),
  79. (name: 'SP'; reg : NR_R13),
  80. (name: 'LR'; reg : NR_R14),
  81. (name: 'PC'; reg : NR_R15));
  82. var
  83. i : longint;
  84. begin
  85. result:=inherited is_register(s);
  86. { reg found?
  87. possible aliases are always 2 char
  88. }
  89. if result or (length(s)<>2) then
  90. exit;
  91. for i:=low(extraregs) to high(extraregs) do
  92. begin
  93. if s=extraregs[i].name then
  94. begin
  95. actasmregister:=extraregs[i].reg;
  96. result:=true;
  97. actasmtoken:=AS_REGISTER;
  98. exit;
  99. end;
  100. end;
  101. end;
  102. procedure tarmattreader.ReadSym(oper : tarmoperand);
  103. var
  104. tempstr, mangledname : string;
  105. typesize,l,k : longint;
  106. begin
  107. tempstr:=actasmpattern;
  108. Consume(AS_ID);
  109. { typecasting? }
  110. if (actasmtoken=AS_LPAREN) and
  111. SearchType(tempstr,typesize) then
  112. begin
  113. oper.hastype:=true;
  114. Consume(AS_LPAREN);
  115. BuildOperand(oper);
  116. Consume(AS_RPAREN);
  117. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  118. oper.SetSize(typesize,true);
  119. end
  120. else
  121. if not oper.SetupVar(tempstr,false) then
  122. Message1(sym_e_unknown_id,tempstr);
  123. { record.field ? }
  124. if actasmtoken=AS_DOT then
  125. begin
  126. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  127. if (mangledname<>'') then
  128. Message(asmr_e_invalid_reference_syntax);
  129. inc(oper.opr.ref.offset,l);
  130. end;
  131. end;
  132. Procedure tarmattreader.BuildReference(oper : tarmoperand);
  133. procedure do_error;
  134. begin
  135. Message(asmr_e_invalid_reference_syntax);
  136. RecoverConsume(false);
  137. end;
  138. procedure test_end(require_rbracket : boolean);
  139. begin
  140. if require_rbracket then begin
  141. if not(actasmtoken=AS_RBRACKET) then
  142. begin
  143. do_error;
  144. exit;
  145. end
  146. else
  147. Consume(AS_RBRACKET);
  148. if (actasmtoken=AS_NOT) then
  149. begin
  150. oper.opr.ref.addressmode:=AM_PREINDEXED;
  151. Consume(AS_NOT);
  152. end;
  153. end;
  154. if not(actasmtoken in [AS_SEPARATOR,AS_end]) then
  155. do_error
  156. else
  157. begin
  158. {$IFDEF debugasmreader}
  159. writeln('TEST_end_FINAL_OK. Created the following ref:');
  160. writeln('oper.opr.ref.shiftimm=',oper.opr.ref.shiftimm);
  161. writeln('oper.opr.ref.shiftmode=',ord(oper.opr.ref.shiftmode));
  162. writeln('oper.opr.ref.index=',ord(oper.opr.ref.index));
  163. writeln('oper.opr.ref.base=',ord(oper.opr.ref.base));
  164. writeln('oper.opr.ref.signindex=',ord(oper.opr.ref.signindex));
  165. writeln('oper.opr.ref.addressmode=',ord(oper.opr.ref.addressmode));
  166. writeln;
  167. {$endIF debugasmreader}
  168. end;
  169. end;
  170. function is_shifter_ref_operation(var a : tshiftmode) : boolean;
  171. begin
  172. a := SM_NONE;
  173. if (actasmpattern='LSL') then
  174. a := SM_LSL
  175. else if (actasmpattern='LSR') then
  176. a := SM_LSR
  177. else if (actasmpattern='ASR') then
  178. a := SM_ASR
  179. else if (actasmpattern='ROR') then
  180. a := SM_ROR
  181. else if (actasmpattern='RRX') then
  182. a := SM_RRX;
  183. is_shifter_ref_operation := not(a=SM_NONE);
  184. end;
  185. procedure read_index_shift(require_rbracket : boolean);
  186. begin
  187. case actasmtoken of
  188. AS_COMMA :
  189. begin
  190. Consume(AS_COMMA);
  191. if not(actasmtoken=AS_ID) then
  192. do_error;
  193. if is_shifter_ref_operation(oper.opr.ref.shiftmode) then
  194. begin
  195. Consume(AS_ID);
  196. if not(oper.opr.ref.shiftmode=SM_RRX) then
  197. begin
  198. if not(actasmtoken=AS_HASH) then
  199. do_error;
  200. Consume(AS_HASH);
  201. oper.opr.ref.shiftimm := BuildConstExpression(false,true);
  202. if (oper.opr.ref.shiftimm<0) or (oper.opr.ref.shiftimm>32) then
  203. do_error;
  204. test_end(require_rbracket);
  205. end;
  206. end
  207. else
  208. begin
  209. do_error;
  210. exit;
  211. end;
  212. end;
  213. AS_RBRACKET :
  214. if require_rbracket then
  215. test_end(require_rbracket)
  216. else
  217. begin
  218. do_error;
  219. exit;
  220. end;
  221. AS_SEPARATOR,AS_END :
  222. if not require_rbracket then
  223. test_end(false)
  224. else
  225. do_error;
  226. else
  227. begin
  228. do_error;
  229. exit;
  230. end;
  231. end;
  232. end;
  233. procedure read_index(require_rbracket : boolean);
  234. var
  235. i : longint;
  236. w : word;
  237. recname : string;
  238. o_int,s_int : aint;
  239. begin
  240. case actasmtoken of
  241. AS_REGISTER :
  242. begin
  243. oper.opr.ref.index:=actasmregister;
  244. Consume(AS_REGISTER);
  245. read_index_shift(require_rbracket);
  246. exit;
  247. end;
  248. AS_PLUS,AS_MINUS :
  249. begin
  250. if actasmtoken=AS_PLUS then
  251. begin
  252. Consume(AS_PLUS);
  253. end
  254. else
  255. begin
  256. oper.opr.ref.signindex := -1;
  257. Consume(AS_MINUS);
  258. end;
  259. if actasmtoken=AS_REGISTER then
  260. begin
  261. oper.opr.ref.index:=actasmregister;
  262. Consume(AS_REGISTER);
  263. read_index_shift(require_rbracket);
  264. exit;
  265. end
  266. else
  267. begin
  268. do_error;
  269. exit;
  270. end;
  271. test_end(require_rbracket);
  272. exit;
  273. end;
  274. AS_HASH : // constant
  275. begin
  276. Consume(AS_HASH);
  277. o_int := BuildConstExpression(false,true);
  278. if (o_int>4095) or (o_int<-4095) then
  279. begin
  280. Message(asmr_e_constant_out_of_bounds);
  281. RecoverConsume(false);
  282. exit;
  283. end
  284. else
  285. begin
  286. inc(oper.opr.ref.offset,o_int);
  287. test_end(require_rbracket);
  288. exit;
  289. end;
  290. end;
  291. AS_ID :
  292. begin
  293. recname := actasmpattern;
  294. Consume(AS_ID);
  295. BuildRecordOffsetSize(recname,o_int,s_int,recname,false);
  296. if (o_int>4095)or(o_int<-4095) then
  297. begin
  298. Message(asmr_e_constant_out_of_bounds);
  299. RecoverConsume(false);
  300. exit;
  301. end
  302. else
  303. begin
  304. inc(oper.opr.ref.offset,o_int);
  305. test_end(require_rbracket);
  306. exit;
  307. end;
  308. end;
  309. AS_AT:
  310. begin
  311. do_error;
  312. exit;
  313. end;
  314. AS_DOT : // local label
  315. begin
  316. oper.opr.ref.signindex := BuildConstExpression(true,false);
  317. test_end(require_rbracket);
  318. exit;
  319. end;
  320. AS_RBRACKET :
  321. begin
  322. if require_rbracket then
  323. begin
  324. test_end(require_rbracket);
  325. exit;
  326. end
  327. else
  328. begin
  329. do_error; // unexpected rbracket
  330. exit;
  331. end;
  332. end;
  333. AS_SEPARATOR,AS_end :
  334. begin
  335. if not require_rbracket then
  336. begin
  337. test_end(false);
  338. exit;
  339. end
  340. else
  341. begin
  342. do_error;
  343. exit;
  344. end;
  345. end;
  346. else
  347. begin
  348. // unexpected token
  349. do_error;
  350. exit;
  351. end;
  352. end; // case
  353. end;
  354. procedure try_prepostindexed;
  355. begin
  356. Consume(AS_RBRACKET);
  357. case actasmtoken of
  358. AS_COMMA :
  359. begin // post-indexed
  360. Consume(AS_COMMA);
  361. oper.opr.ref.addressmode:=AM_POSTINDEXED;
  362. read_index(false);
  363. exit;
  364. end;
  365. AS_NOT :
  366. begin // pre-indexed
  367. Consume(AS_NOT);
  368. oper.opr.ref.addressmode:=AM_PREINDEXED;
  369. test_end(false);
  370. exit;
  371. end;
  372. else
  373. begin
  374. test_end(false);
  375. exit;
  376. end;
  377. end; // case
  378. end;
  379. var
  380. lab : TASMLABEL;
  381. begin
  382. Consume(AS_LBRACKET);
  383. oper.opr.ref.addressmode:=AM_OFFSET; // assume "neither PRE nor POST inc"
  384. if actasmtoken=AS_REGISTER then
  385. begin
  386. oper.opr.ref.base:=actasmregister;
  387. Consume(AS_REGISTER);
  388. case actasmtoken of
  389. AS_RBRACKET :
  390. begin
  391. try_prepostindexed;
  392. exit;
  393. end;
  394. AS_COMMA :
  395. begin
  396. Consume(AS_COMMA);
  397. read_index(true);
  398. exit;
  399. end;
  400. else
  401. begin
  402. Message(asmr_e_invalid_reference_syntax);
  403. RecoverConsume(false);
  404. end;
  405. end;
  406. end
  407. else
  408. {
  409. if base isn't a register, r15=PC is implied base, so it must be a local label.
  410. pascal constants don't make sense, because implied r15
  411. record offsets probably don't make sense, too (a record offset of code?)
  412. TODO: However, we could make the Stackpointer implied.
  413. }
  414. Begin
  415. case actasmtoken of
  416. AS_ID :
  417. begin
  418. if is_locallabel(actasmpattern) then
  419. begin
  420. CreateLocalLabel(actasmpattern,lab,false);
  421. oper.opr.ref.symbol := lab;
  422. Consume(AS_ID);
  423. test_end(true);
  424. exit;
  425. end
  426. else
  427. begin
  428. // TODO: Stackpointer implied,
  429. Message(asmr_e_invalid_reference_syntax);
  430. RecoverConsume(false);
  431. exit;
  432. end;
  433. end;
  434. else
  435. begin // elsecase
  436. Message(asmr_e_invalid_reference_syntax);
  437. RecoverConsume(false);
  438. exit;
  439. end;
  440. end;
  441. end;
  442. end;
  443. function tarmattreader.TryBuildShifterOp(oper : tarmoperand) : boolean;
  444. procedure handlepara(sm : tshiftmode);
  445. begin
  446. consume(AS_ID);
  447. fillchar(oper.opr,sizeof(oper.opr),0);
  448. oper.opr.typ:=OPR_SHIFTEROP;
  449. oper.opr.shifterop.shiftmode:=sm;
  450. if sm<>SM_RRX then
  451. begin
  452. case actasmtoken of
  453. AS_REGISTER:
  454. begin
  455. oper.opr.shifterop.rs:=actasmregister;
  456. consume(AS_REGISTER);
  457. end;
  458. AS_HASH:
  459. begin
  460. consume(AS_HASH);
  461. oper.opr.shifterop.shiftimm:=BuildConstExpression(false,false);
  462. end;
  463. else
  464. Message(asmr_e_illegal_shifterop_syntax);
  465. end;
  466. end;
  467. end;
  468. begin
  469. result:=true;
  470. if (actasmtoken=AS_ID) then
  471. begin
  472. if (actasmpattern='LSL') then
  473. handlepara(SM_LSL)
  474. else if (actasmpattern='LSR') then
  475. handlepara(SM_LSR)
  476. else if (actasmpattern='ASR') then
  477. handlepara(SM_ASR)
  478. else if (actasmpattern='ROR') then
  479. handlepara(SM_ROR)
  480. else if (actasmpattern='RRX') then
  481. handlepara(SM_ROR)
  482. else
  483. result:=false;
  484. end
  485. else
  486. result:=false;
  487. end;
  488. Procedure tarmattreader.BuildOperand(oper : tarmoperand);
  489. var
  490. expr : string;
  491. typesize,l : longint;
  492. procedure AddLabelOperand(hl:tasmlabel);
  493. begin
  494. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  495. is_calljmp(actopcode) then
  496. begin
  497. oper.opr.typ:=OPR_SYMBOL;
  498. oper.opr.symbol:=hl;
  499. end
  500. else
  501. begin
  502. oper.InitRef;
  503. oper.opr.ref.symbol:=hl;
  504. end;
  505. end;
  506. procedure MaybeRecordOffset;
  507. var
  508. mangledname: string;
  509. hasdot : boolean;
  510. l,
  511. toffset,
  512. tsize : longint;
  513. begin
  514. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  515. exit;
  516. l:=0;
  517. hasdot:=(actasmtoken=AS_DOT);
  518. if hasdot then
  519. begin
  520. if expr<>'' then
  521. begin
  522. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  523. if (oper.opr.typ<>OPR_CONSTANT) and
  524. (mangledname<>'') then
  525. Message(asmr_e_wrong_sym_type);
  526. inc(l,toffset);
  527. oper.SetSize(tsize,true);
  528. end;
  529. end;
  530. if actasmtoken in [AS_PLUS,AS_MINUS] then
  531. inc(l,BuildConstExpression(true,false));
  532. case oper.opr.typ of
  533. OPR_LOCAL :
  534. begin
  535. { don't allow direct access to fields of parameters, because that
  536. will generate buggy code. Allow it only for explicit typecasting }
  537. if hasdot and
  538. (not oper.hastype) and
  539. (tabstractnormalvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  540. (current_procinfo.procdef.proccalloption<>pocall_register) then
  541. Message(asmr_e_cannot_access_field_directly_for_parameters);
  542. inc(oper.opr.localsymofs,l)
  543. end;
  544. OPR_CONSTANT :
  545. inc(oper.opr.val,l);
  546. OPR_REFERENCE :
  547. if (mangledname<>'') then
  548. begin
  549. if (oper.opr.val<>0) then
  550. Message(asmr_e_wrong_sym_type);
  551. oper.opr.typ:=OPR_SYMBOL;
  552. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname);
  553. end
  554. else
  555. inc(oper.opr.val,l);
  556. OPR_SYMBOL:
  557. Message(asmr_e_invalid_symbol_ref);
  558. else
  559. internalerror(200309221);
  560. end;
  561. end;
  562. function MaybeBuildReference:boolean;
  563. { Try to create a reference, if not a reference is found then false
  564. is returned }
  565. begin
  566. MaybeBuildReference:=true;
  567. case actasmtoken of
  568. AS_INTNUM,
  569. AS_MINUS,
  570. AS_PLUS:
  571. Begin
  572. oper.opr.ref.offset:=BuildConstExpression(True,False);
  573. if actasmtoken<>AS_LPAREN then
  574. Message(asmr_e_invalid_reference_syntax)
  575. else
  576. BuildReference(oper);
  577. end;
  578. AS_LPAREN:
  579. BuildReference(oper);
  580. AS_ID: { only a variable is allowed ... }
  581. Begin
  582. ReadSym(oper);
  583. case actasmtoken of
  584. AS_end,
  585. AS_SEPARATOR,
  586. AS_COMMA: ;
  587. AS_LPAREN:
  588. BuildReference(oper);
  589. else
  590. Begin
  591. Message(asmr_e_invalid_reference_syntax);
  592. Consume(actasmtoken);
  593. end;
  594. end; {end case }
  595. end;
  596. else
  597. MaybeBuildReference:=false;
  598. end; { end case }
  599. end;
  600. var
  601. tempreg : tregister;
  602. ireg : tsuperregister;
  603. hl : tasmlabel;
  604. ofs : longint;
  605. registerset : tcpuregisterset;
  606. Begin
  607. expr:='';
  608. case actasmtoken of
  609. AS_LBRACKET: { Memory reference or constant expression }
  610. Begin
  611. oper.InitRef;
  612. BuildReference(oper);
  613. end;
  614. AS_HASH: { Constant expression }
  615. Begin
  616. Consume(AS_HASH);
  617. BuildConstantOperand(oper);
  618. end;
  619. (*
  620. AS_INTNUM,
  621. AS_MINUS,
  622. AS_PLUS:
  623. Begin
  624. { Constant memory offset }
  625. { This must absolutely be followed by ( }
  626. oper.InitRef;
  627. oper.opr.ref.offset:=BuildConstExpression(True,False);
  628. if actasmtoken<>AS_LPAREN then
  629. begin
  630. ofs:=oper.opr.ref.offset;
  631. BuildConstantOperand(oper);
  632. inc(oper.opr.val,ofs);
  633. end
  634. else
  635. BuildReference(oper);
  636. end;
  637. *)
  638. AS_ID: { A constant expression, or a Variable ref. }
  639. Begin
  640. { Local Label ? }
  641. if is_locallabel(actasmpattern) then
  642. begin
  643. CreateLocalLabel(actasmpattern,hl,false);
  644. Consume(AS_ID);
  645. AddLabelOperand(hl);
  646. end
  647. else
  648. { Check for label }
  649. if SearchLabel(actasmpattern,hl,false) then
  650. begin
  651. Consume(AS_ID);
  652. AddLabelOperand(hl);
  653. end
  654. else
  655. { probably a variable or normal expression }
  656. { or a procedure (such as in CALL ID) }
  657. Begin
  658. { is it a constant ? }
  659. if SearchIConstant(actasmpattern,l) then
  660. Begin
  661. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  662. Message(asmr_e_invalid_operand_type);
  663. BuildConstantOperand(oper);
  664. end
  665. else
  666. begin
  667. expr:=actasmpattern;
  668. Consume(AS_ID);
  669. { typecasting? }
  670. if (actasmtoken=AS_LPAREN) and
  671. SearchType(expr,typesize) then
  672. begin
  673. oper.hastype:=true;
  674. Consume(AS_LPAREN);
  675. BuildOperand(oper);
  676. Consume(AS_RPAREN);
  677. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  678. oper.SetSize(typesize,true);
  679. end
  680. else
  681. begin
  682. if not(oper.SetupVar(expr,false)) then
  683. Begin
  684. { look for special symbols ... }
  685. if expr= '__HIGH' then
  686. begin
  687. consume(AS_LPAREN);
  688. if not oper.setupvar('high'+actasmpattern,false) then
  689. Message1(sym_e_unknown_id,'high'+actasmpattern);
  690. consume(AS_ID);
  691. consume(AS_RPAREN);
  692. end
  693. else
  694. if expr = '__RESULT' then
  695. oper.SetUpResult
  696. else
  697. if expr = '__SELF' then
  698. oper.SetupSelf
  699. else
  700. if expr = '__OLDEBP' then
  701. oper.SetupOldEBP
  702. else
  703. Message1(sym_e_unknown_id,expr);
  704. end;
  705. end;
  706. end;
  707. if actasmtoken=AS_DOT then
  708. MaybeRecordOffset;
  709. { add a constant expression? }
  710. if (actasmtoken=AS_PLUS) then
  711. begin
  712. l:=BuildConstExpression(true,false);
  713. case oper.opr.typ of
  714. OPR_CONSTANT :
  715. inc(oper.opr.val,l);
  716. OPR_LOCAL :
  717. inc(oper.opr.localsymofs,l);
  718. OPR_REFERENCE :
  719. inc(oper.opr.ref.offset,l);
  720. else
  721. internalerror(200309202);
  722. end;
  723. end
  724. end;
  725. { Do we have a indexing reference, then parse it also }
  726. if actasmtoken=AS_LPAREN then
  727. BuildReference(oper);
  728. end;
  729. { Register, a variable reference or a constant reference }
  730. AS_REGISTER:
  731. Begin
  732. { save the type of register used. }
  733. tempreg:=actasmregister;
  734. Consume(AS_REGISTER);
  735. if (actasmtoken in [AS_end,AS_SEPARATOR,AS_COMMA]) then
  736. Begin
  737. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  738. Message(asmr_e_invalid_operand_type);
  739. oper.opr.typ:=OPR_REGISTER;
  740. oper.opr.reg:=tempreg;
  741. end
  742. else if (actasmtoken=AS_NOT) and (actopcode in [A_LDM,A_STM]) then
  743. begin
  744. consume(AS_NOT);
  745. oper.opr.typ:=OPR_REFERENCE;
  746. oper.opr.ref.addressmode:=AM_PREINDEXED;
  747. oper.opr.ref.index:=tempreg;
  748. end
  749. else
  750. Message(asmr_e_syn_operand);
  751. end;
  752. { Registerset }
  753. AS_LSBRACKET:
  754. begin
  755. consume(AS_LSBRACKET);
  756. registerset:=[];
  757. while true do
  758. begin
  759. if actasmtoken=AS_REGISTER then
  760. begin
  761. include(registerset,getsupreg(actasmregister));
  762. tempreg:=actasmregister;
  763. consume(AS_REGISTER);
  764. if actasmtoken=AS_MINUS then
  765. begin
  766. consume(AS_MINUS);
  767. for ireg:=getsupreg(tempreg) to getsupreg(actasmregister) do
  768. include(registerset,ireg);
  769. consume(AS_REGISTER);
  770. end;
  771. end
  772. else
  773. consume(AS_REGISTER);
  774. if actasmtoken=AS_COMMA then
  775. consume(AS_COMMA)
  776. else
  777. break;
  778. end;
  779. consume(AS_RSBRACKET);
  780. oper.opr.typ:=OPR_REGSET;
  781. oper.opr.regset:=registerset;
  782. end;
  783. AS_end,
  784. AS_SEPARATOR,
  785. AS_COMMA: ;
  786. else
  787. Begin
  788. Message(asmr_e_syn_operand);
  789. Consume(actasmtoken);
  790. end;
  791. end; { end case }
  792. end;
  793. {*****************************************************************************
  794. tarmattreader
  795. *****************************************************************************}
  796. procedure tarmattreader.BuildOpCode(instr : tarminstruction);
  797. var
  798. operandnum : longint;
  799. Begin
  800. { opcode }
  801. if (actasmtoken<>AS_OPCODE) then
  802. Begin
  803. Message(asmr_e_invalid_or_missing_opcode);
  804. RecoverConsume(true);
  805. exit;
  806. end;
  807. { Fill the instr object with the current state }
  808. with instr do
  809. begin
  810. Opcode:=ActOpcode;
  811. condition:=ActCondition;
  812. oppostfix:=actoppostfix;
  813. end;
  814. { We are reading operands, so opcode will be an AS_ID }
  815. operandnum:=1;
  816. Consume(AS_OPCODE);
  817. { Zero operand opcode ? }
  818. if actasmtoken in [AS_SEPARATOR,AS_end] then
  819. begin
  820. operandnum:=0;
  821. exit;
  822. end;
  823. { Read the operands }
  824. repeat
  825. case actasmtoken of
  826. AS_COMMA: { Operand delimiter }
  827. Begin
  828. if ((instr.opcode=A_MOV) and (operandnum=2)) or
  829. ((operandnum=3) and not(instr.opcode in [A_UMLAL,A_UMULL,A_SMLAL,A_SMULL,A_MLA])) then
  830. begin
  831. Consume(AS_COMMA);
  832. if not(TryBuildShifterOp(instr.Operands[operandnum+1] as tarmoperand)) then
  833. Message(asmr_e_illegal_shifterop_syntax);
  834. Inc(operandnum);
  835. end
  836. else
  837. begin
  838. if operandnum>Max_Operands then
  839. Message(asmr_e_too_many_operands)
  840. else
  841. Inc(operandnum);
  842. Consume(AS_COMMA);
  843. end;
  844. end;
  845. AS_SEPARATOR,
  846. AS_end : { End of asm operands for this opcode }
  847. begin
  848. break;
  849. end;
  850. else
  851. BuildOperand(instr.Operands[operandnum] as tarmoperand);
  852. end; { end case }
  853. until false;
  854. instr.Ops:=operandnum;
  855. end;
  856. function tarmattreader.is_asmopcode(const s: string):boolean;
  857. const
  858. { sorted by length so longer postfixes will match first }
  859. postfix2strsorted : array[1..19] of string[2] = (
  860. 'EP','SB','BT','SH',
  861. 'IA','IB','DA','DB','FD','FA','ED','EA',
  862. 'B','D','E','P','T','H','S');
  863. postfixsorted : array[1..19] of TOpPostfix = (
  864. PF_EP,PF_SB,PF_BT,PF_SH,
  865. PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA,
  866. PF_B,PF_D,PF_E,PF_P,PF_T,PF_H,PF_S);
  867. var
  868. len,
  869. j,
  870. sufidx : longint;
  871. hs : string;
  872. maxlen : longint;
  873. icond : tasmcond;
  874. Begin
  875. { making s a value parameter would break other assembler readers }
  876. hs:=s;
  877. is_asmopcode:=false;
  878. { clear op code }
  879. actopcode:=A_None;
  880. actcondition:=C_None;
  881. { first, handle B else BLS is read wrong }
  882. if ((hs[1]='B') and (length(hs)=3)) then
  883. begin
  884. for icond:=low(tasmcond) to high(tasmcond) do
  885. begin
  886. if copy(hs,2,3)=uppercond2str[icond] then
  887. begin
  888. actopcode:=A_B;
  889. actasmtoken:=AS_OPCODE;
  890. actcondition:=icond;
  891. is_asmopcode:=true;
  892. exit;
  893. end;
  894. end;
  895. end;
  896. maxlen:=max(length(hs),5);
  897. actopcode:=A_NONE;
  898. for j:=maxlen downto 1 do
  899. begin
  900. actopcode:=tasmop(PtrInt(iasmops.Find(copy(hs,1,j))));
  901. if actopcode<>A_NONE then
  902. begin
  903. actasmtoken:=AS_OPCODE;
  904. { strip op code }
  905. delete(hs,1,j);
  906. break;
  907. end;
  908. end;
  909. if actopcode=A_NONE then
  910. exit;
  911. { search for condition, conditions are always 2 chars }
  912. if length(hs)>1 then
  913. begin
  914. for icond:=low(tasmcond) to high(tasmcond) do
  915. begin
  916. if copy(hs,1,2)=uppercond2str[icond] then
  917. begin
  918. actcondition:=icond;
  919. { strip condition }
  920. delete(hs,1,2);
  921. break;
  922. end;
  923. end;
  924. end;
  925. { check for postfix }
  926. if length(hs)>0 then
  927. begin
  928. for j:=low(postfixsorted) to high(postfixsorted) do
  929. begin
  930. if copy(hs,1,length(postfix2strsorted[j]))=postfix2strsorted[j] then
  931. begin
  932. actoppostfix:=postfixsorted[j];
  933. { strip postfix }
  934. delete(hs,1,length(postfix2strsorted[j]));
  935. break;
  936. end;
  937. end;
  938. end;
  939. { if we stripped all postfixes, it's a valid opcode }
  940. is_asmopcode:=length(hs)=0;
  941. end;
  942. procedure tarmattreader.ConvertCalljmp(instr : tarminstruction);
  943. var
  944. newopr : toprrec;
  945. begin
  946. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  947. begin
  948. newopr.typ:=OPR_SYMBOL;
  949. newopr.symbol:=instr.Operands[1].opr.ref.symbol;
  950. newopr.symofs:=instr.Operands[1].opr.ref.offset;
  951. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  952. (instr.Operands[1].opr.ref.index<>NR_NO) then
  953. Message(asmr_e_syn_operand);
  954. instr.Operands[1].opr:=newopr;
  955. end;
  956. end;
  957. procedure tarmattreader.handleopcode;
  958. var
  959. instr : tarminstruction;
  960. begin
  961. instr:=TarmInstruction.Create(TarmOperand);
  962. BuildOpcode(instr);
  963. if is_calljmp(instr.opcode) then
  964. ConvertCalljmp(instr);
  965. {
  966. instr.AddReferenceSizes;
  967. instr.SetInstructionOpsize;
  968. instr.CheckOperandSizes;
  969. }
  970. instr.ConcatInstruction(curlist);
  971. instr.Free;
  972. actoppostfix:=PF_None;
  973. end;
  974. {*****************************************************************************
  975. Initialize
  976. *****************************************************************************}
  977. const
  978. asmmode_arm_att_info : tasmmodeinfo =
  979. (
  980. id : asmmode_arm_gas;
  981. idtxt : 'GAS';
  982. casmreader : tarmattreader;
  983. );
  984. asmmode_arm_standard_info : tasmmodeinfo =
  985. (
  986. id : asmmode_standard;
  987. idtxt : 'STANDARD';
  988. casmreader : tarmattreader;
  989. );
  990. initialization
  991. RegisterAsmMode(asmmode_arm_att_info);
  992. RegisterAsmMode(asmmode_arm_standard_info);
  993. end.