raarmgas.pas 37 KB

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