raarmgas.pas 37 KB

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