raarmgas.pas 43 KB

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