raarmgas.pas 37 KB

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