raarmgas.pas 50 KB

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