racpugas.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Copyright (c) 2014 by Jonas Maebe
  4. Does the parsing for the AArch64 GNU AS styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit racpugas;
  19. {$i fpcdefs.inc}
  20. Interface
  21. uses
  22. raatt,racpu,
  23. cpubase;
  24. type
  25. taarch64attreader = class(tattreader)
  26. actoppostfix : TOpPostfix;
  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: taarch64operand; is64bit: boolean);
  31. procedure BuildOperand(oper: taarch64operand; is64bit: boolean);
  32. function TryBuildShifterOp(instr: taarch64instruction; opnr: longint) : boolean;
  33. procedure BuildOpCode(instr: taarch64instruction);
  34. procedure ReadSym(oper: taarch64operand; is64bit: boolean);
  35. procedure ConvertCalljmp(instr: taarch64instruction);
  36. function ToConditionCode(const hs: string; is_operand: boolean): tasmcond;
  37. end;
  38. Implementation
  39. uses
  40. { helpers }
  41. cutils,
  42. { global }
  43. globtype,verbose,
  44. systems,aasmbase,aasmtai,aasmdata,aasmcpu,
  45. { symtable }
  46. symconst,symsym,symdef,
  47. procinfo,
  48. rabase,rautils,
  49. cgbase,cgutils,paramgr;
  50. function taarch64attreader.is_register(const s:string):boolean;
  51. type
  52. treg2str = record
  53. name : string[3];
  54. reg : tregister;
  55. end;
  56. const
  57. extraregs : array[0..3] of treg2str = (
  58. (name: 'FP' ; reg: NR_FP),
  59. (name: 'LR' ; reg: NR_LR),
  60. (name: 'IP0'; reg: NR_IP0),
  61. (name: 'IP1'; reg: NR_IP1));
  62. var
  63. i : longint;
  64. begin
  65. result:=inherited is_register(s);
  66. { reg found?
  67. possible aliases are always 2 or 3 chars
  68. }
  69. if result or not(length(s) in [2,3]) then
  70. exit;
  71. for i:=low(extraregs) to high(extraregs) do
  72. begin
  73. if s=extraregs[i].name then
  74. begin
  75. actasmregister:=extraregs[i].reg;
  76. result:=true;
  77. actasmtoken:=AS_REGISTER;
  78. exit;
  79. end;
  80. end;
  81. end;
  82. procedure taarch64attreader.ReadSym(oper: taarch64operand; is64bit: boolean);
  83. var
  84. tempstr, mangledname : string;
  85. typesize,l,k: aint;
  86. begin
  87. tempstr:=actasmpattern;
  88. Consume(AS_ID);
  89. { typecasting? }
  90. if (actasmtoken=AS_LPAREN) and
  91. SearchType(tempstr,typesize) then
  92. begin
  93. oper.hastype:=true;
  94. Consume(AS_LPAREN);
  95. BuildOperand(oper,is64bit);
  96. Consume(AS_RPAREN);
  97. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  98. oper.SetSize(typesize,true);
  99. end
  100. else
  101. if not oper.SetupVar(tempstr,false) then
  102. Message1(sym_e_unknown_id,tempstr);
  103. { record.field ? }
  104. if actasmtoken=AS_DOT then
  105. begin
  106. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  107. if (mangledname<>'') then
  108. Message(asmr_e_invalid_reference_syntax);
  109. inc(oper.opr.ref.offset,l);
  110. end;
  111. end;
  112. Procedure taarch64attreader.BuildReference(oper: taarch64operand; is64bit: boolean);
  113. procedure do_error;
  114. begin
  115. Message(asmr_e_invalid_reference_syntax);
  116. RecoverConsume(false);
  117. end;
  118. procedure test_end(require_rbracket : boolean);
  119. begin
  120. if require_rbracket then begin
  121. if not(actasmtoken=AS_RBRACKET) then
  122. begin
  123. do_error;
  124. exit;
  125. end
  126. else
  127. Consume(AS_RBRACKET);
  128. if (actasmtoken=AS_NOT) then
  129. begin
  130. oper.opr.ref.addressmode:=AM_PREINDEXED;
  131. Consume(AS_NOT);
  132. end;
  133. end;
  134. if not(actasmtoken in [AS_SEPARATOR,AS_end]) then
  135. do_error
  136. else
  137. begin
  138. {$IFDEF debugasmreader}
  139. writeln('TEST_end_FINAL_OK. Created the following ref:');
  140. writeln('oper.opr.ref.shiftimm=',oper.opr.ref.shiftimm);
  141. writeln('oper.opr.ref.shiftmode=',ord(oper.opr.ref.shiftmode));
  142. writeln('oper.opr.ref.index=',ord(oper.opr.ref.index));
  143. writeln('oper.opr.ref.base=',ord(oper.opr.ref.base));
  144. writeln('oper.opr.ref.signindex=',ord(oper.opr.ref.signindex));
  145. writeln('oper.opr.ref.addressmode=',ord(oper.opr.ref.addressmode));
  146. writeln;
  147. {$endIF debugasmreader}
  148. end;
  149. end;
  150. function is_shifter_ref_operation(var a : tshiftmode) : boolean;
  151. begin
  152. a:=SM_NONE;
  153. if (actasmpattern='LSL') then
  154. a:=SM_LSL
  155. else if (actasmpattern='UXTW') then
  156. a:=SM_UXTW
  157. else if (actasmpattern='SXTW') then
  158. a:=SM_SXTW
  159. else if (actasmpattern='SXTX') then
  160. a:=SM_SXTX;
  161. is_shifter_ref_operation:=not(a=SM_NONE);
  162. end;
  163. procedure read_index_shift(require_rbracket : boolean);
  164. var
  165. shift: aint;
  166. begin
  167. case actasmtoken of
  168. AS_COMMA :
  169. begin
  170. Consume(AS_COMMA);
  171. if not(actasmtoken=AS_ID) then
  172. do_error;
  173. if is_shifter_ref_operation(oper.opr.ref.shiftmode) then
  174. begin
  175. Consume(actasmtoken);
  176. if actasmtoken=AS_HASH then
  177. begin
  178. Consume(AS_HASH);
  179. shift:=BuildConstExpression(false,true);
  180. if not(shift in [0,2+ord(is64bit)]) then
  181. do_error;
  182. oper.opr.ref.shiftimm:=shift;
  183. test_end(require_rbracket);
  184. end;
  185. end
  186. else
  187. begin
  188. do_error;
  189. exit;
  190. end;
  191. end;
  192. AS_RBRACKET :
  193. if require_rbracket then
  194. test_end(require_rbracket)
  195. else
  196. begin
  197. do_error;
  198. exit;
  199. end;
  200. AS_SEPARATOR,AS_END :
  201. if not require_rbracket then
  202. test_end(false)
  203. else
  204. do_error;
  205. else
  206. begin
  207. do_error;
  208. exit;
  209. end;
  210. end;
  211. end;
  212. procedure read_index(require_rbracket : boolean);
  213. var
  214. recname : string;
  215. o_int,s_int : aint;
  216. begin
  217. case actasmtoken of
  218. AS_REGISTER :
  219. begin
  220. if getsupreg(actasmregister)=RS_XZR then
  221. Message1(asmr_e_invalid_ref_register,actasmpattern);
  222. oper.opr.ref.index:=actasmregister;
  223. Consume(AS_REGISTER);
  224. read_index_shift(require_rbracket);
  225. exit;
  226. end;
  227. AS_HASH : // constant
  228. begin
  229. Consume(AS_HASH);
  230. (*
  231. if actasmtoken=AS_COLON then
  232. begin
  233. consume(AS_COLON);
  234. { GNU-style lower 12 bits of address of non-GOT-based
  235. access }
  236. if (actasmpattern='LO12') then
  237. begin
  238. consume(actasmtoken);
  239. consume(AS_COLON);
  240. if not oper.SetupVar(actasmpattern,false) then
  241. begin
  242. do_error;
  243. exit
  244. end;
  245. consume(AS_ID);
  246. oper.opr.ref.refaddr:=addr_??? (not gotpageoffset);
  247. end
  248. else
  249. begin
  250. do_error;
  251. exit
  252. end;
  253. end
  254. else
  255. *)
  256. begin
  257. o_int:=BuildConstExpression(false,true);
  258. inc(oper.opr.ref.offset,o_int);
  259. end;
  260. test_end(require_rbracket);
  261. exit;
  262. end;
  263. AS_ID :
  264. begin
  265. recname:=actasmpattern;
  266. Consume(AS_ID);
  267. { Apple-style got page offset }
  268. if actasmtoken=AS_AT then
  269. begin
  270. if not oper.SetupVar(recname,false) then
  271. begin
  272. do_error;
  273. exit
  274. end;
  275. consume(AS_AT);
  276. if actasmpattern='GOTPAGEOFF' then
  277. begin
  278. consume(actasmtoken);
  279. oper.opr.ref.refaddr:=addr_gotpageoffset;
  280. end
  281. else if actasmpattern='PAGEOFF' then
  282. begin
  283. consume(actasmtoken);
  284. oper.opr.ref.refaddr:=addr_pageoffset;
  285. end
  286. else
  287. begin
  288. do_error;
  289. exit
  290. end;
  291. end
  292. else
  293. begin
  294. BuildRecordOffsetSize(recname,o_int,s_int,recname,false);
  295. inc(oper.opr.ref.offset,o_int);
  296. end;
  297. test_end(require_rbracket);
  298. exit;
  299. end;
  300. AS_AT:
  301. begin
  302. do_error;
  303. exit;
  304. end;
  305. AS_RBRACKET :
  306. begin
  307. if require_rbracket then
  308. begin
  309. test_end(require_rbracket);
  310. exit;
  311. end
  312. else
  313. begin
  314. do_error; // unexpected rbracket
  315. exit;
  316. end;
  317. end;
  318. AS_SEPARATOR,AS_end :
  319. begin
  320. if not require_rbracket then
  321. begin
  322. test_end(false);
  323. exit;
  324. end
  325. else
  326. begin
  327. do_error;
  328. exit;
  329. end;
  330. end;
  331. else
  332. begin
  333. // unexpected token
  334. do_error;
  335. exit;
  336. end;
  337. end; // case
  338. end;
  339. procedure try_prepostindexed;
  340. begin
  341. Consume(AS_RBRACKET);
  342. case actasmtoken of
  343. AS_COMMA :
  344. begin // post-indexed
  345. Consume(AS_COMMA);
  346. oper.opr.ref.addressmode:=AM_POSTINDEXED;
  347. read_index(false);
  348. exit;
  349. end;
  350. AS_NOT :
  351. begin // pre-indexed
  352. Consume(AS_NOT);
  353. oper.opr.ref.addressmode:=AM_PREINDEXED;
  354. test_end(false);
  355. exit;
  356. end;
  357. else
  358. begin
  359. test_end(false);
  360. exit;
  361. end;
  362. end; // case
  363. end;
  364. begin
  365. Consume(AS_LBRACKET);
  366. oper.opr.ref.addressmode:=AM_OFFSET; // assume "neither PRE nor POST inc"
  367. if actasmtoken=AS_REGISTER then
  368. begin
  369. if getsupreg(actasmregister)=RS_XZR then
  370. Message1(asmr_e_invalid_ref_register,actasmpattern);
  371. oper.opr.ref.base:=actasmregister;
  372. Consume(AS_REGISTER);
  373. case actasmtoken of
  374. AS_RBRACKET :
  375. begin
  376. try_prepostindexed;
  377. exit;
  378. end;
  379. AS_COMMA :
  380. begin
  381. Consume(AS_COMMA);
  382. read_index(true);
  383. exit;
  384. end;
  385. else
  386. begin
  387. Message(asmr_e_invalid_reference_syntax);
  388. RecoverConsume(false);
  389. end;
  390. end;
  391. end
  392. else
  393. Begin
  394. case actasmtoken of
  395. AS_ID :
  396. begin
  397. { TODO: local variables and parameters }
  398. Message(asmr_e_invalid_reference_syntax);
  399. RecoverConsume(false);
  400. exit;
  401. end;
  402. else
  403. begin // elsecase
  404. Message(asmr_e_invalid_reference_syntax);
  405. RecoverConsume(false);
  406. exit;
  407. end;
  408. end;
  409. end;
  410. end;
  411. function taarch64attreader.TryBuildShifterOp(instr: taarch64instruction; opnr: longint): boolean;
  412. procedure handlepara(sm : tshiftmode);
  413. begin
  414. consume(AS_ID);
  415. fillchar(instr.operands[opnr].opr,sizeof(instr.operands[opnr].opr),0);
  416. instr.operands[opnr].opr.typ:=OPR_SHIFTEROP;
  417. instr.operands[opnr].opr.shifterop.shiftmode:=sm;
  418. if (sm=SM_LSL) or
  419. (actasmtoken=AS_HASH) then
  420. begin
  421. consume(AS_HASH);
  422. instr.operands[opnr].opr.shifterop.shiftimm:=BuildConstExpression(false,false);
  423. end;
  424. end;
  425. const
  426. shiftmode2str: array[SM_LSL..SM_SXTX] of string[4] =
  427. ('LSL','LSR','ASR',
  428. 'UXTB','UXTH','UXTW','UXTX',
  429. 'SXTB','SXTH','SXTW','SXTX');
  430. var
  431. sm: tshiftmode;
  432. i: longint;
  433. usessp,
  434. useszr: boolean;
  435. begin
  436. result:=false;
  437. if (actasmtoken=AS_ID) then
  438. begin
  439. for sm:=low(shiftmode2str) to high(shiftmode2str) do
  440. if actasmpattern=shiftmode2str[sm] then
  441. begin
  442. handlepara(sm);
  443. if instr.operands[1].opr.typ=OPR_REGISTER then
  444. begin
  445. { the possible shifter ops depend on whether this
  446. instruction uses sp and/or zr }
  447. usessp:=false;
  448. useszr:=false;
  449. for i:=low(instr.operands) to pred(opnr) do
  450. begin
  451. if (instr.operands[i].opr.typ=OPR_REGISTER) then
  452. case getsupreg(instr.operands[i].opr.reg) of
  453. RS_XZR:
  454. useszr:=true;
  455. RS_SP:
  456. usessp:=true;
  457. end;
  458. end;
  459. result:=valid_shifter_operand(instr.opcode,useszr,usessp,instr.Is64bit,sm,instr.operands[opnr].opr.shifterop.shiftimm);
  460. if result then
  461. instr.Ops:=opnr;
  462. end;
  463. break;
  464. end;
  465. end;
  466. end;
  467. function taarch64attreader.ToConditionCode(const hs: string; is_operand: boolean): tasmcond;
  468. begin
  469. case actopcode of
  470. A_CSEL,A_CSINC,A_CSINV,A_CSNEG,A_CSET,A_CSETM,
  471. A_CINC,A_CINV,A_CNEG,A_CCMN,A_CCMP,
  472. A_B:
  473. begin
  474. { search for condition, conditions are always 2 chars }
  475. if (is_operand<>(actopcode=A_B)) and
  476. (length(hs)>1) then
  477. begin
  478. { workaround for DFA bug }
  479. result:=low(tasmcond);
  480. for result:=low(tasmcond) to high(tasmcond) do
  481. begin
  482. if hs=uppercond2str[result] then
  483. exit;
  484. end;
  485. end;
  486. end;
  487. end;
  488. result:=C_None;;
  489. end;
  490. Procedure taarch64attreader.BuildOperand(oper: taarch64operand; is64bit: boolean);
  491. var
  492. expr: string;
  493. typesize, l: aint;
  494. procedure MaybeAddGotAddrMode;
  495. begin
  496. if actasmtoken=AS_AT then
  497. begin
  498. consume(AS_AT);
  499. if actasmpattern='GOTPAGE' then
  500. oper.opr.ref.refaddr:=addr_gotpage
  501. else if actasmpattern='GOTPAGEOFF' then
  502. oper.opr.ref.refaddr:=addr_gotpageoffset
  503. else if actasmpattern='PAGE' then
  504. oper.opr.ref.refaddr:=addr_page
  505. else if actasmpattern='PAGEOFF' then
  506. oper.opr.ref.refaddr:=addr_pageoffset
  507. else
  508. Message(asmr_e_expr_illegal);
  509. consume(actasmtoken);
  510. end
  511. else
  512. oper.opr.ref.refaddr:=addr_pic;
  513. end;
  514. procedure AddLabelOperand(hl:tasmlabel);
  515. begin
  516. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  517. is_calljmp(actopcode) then
  518. begin
  519. oper.opr.typ:=OPR_SYMBOL;
  520. oper.opr.symbol:=hl;
  521. end
  522. else if (actopcode=A_ADR) or
  523. (actopcode=A_ADRP) or
  524. (actopcode=A_LDR) then
  525. begin
  526. oper.InitRef;
  527. MaybeAddGotAddrMode;
  528. oper.opr.ref.symbol:=hl;
  529. if (actasmtoken in [AS_PLUS, AS_MINUS]) then
  530. begin
  531. l:=BuildConstExpression(true,false);
  532. oper.opr.ref.offset:=l;
  533. end;
  534. end;
  535. end;
  536. procedure MaybeRecordOffset;
  537. var
  538. mangledname: string;
  539. hasdot : boolean;
  540. l,
  541. toffset,
  542. tsize : aint;
  543. begin
  544. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  545. exit;
  546. l:=0;
  547. mangledname:='';
  548. hasdot:=(actasmtoken=AS_DOT);
  549. if hasdot then
  550. begin
  551. if expr<>'' then
  552. begin
  553. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  554. if (oper.opr.typ<>OPR_CONSTANT) and
  555. (mangledname<>'') then
  556. Message(asmr_e_wrong_sym_type);
  557. inc(l,toffset);
  558. oper.SetSize(tsize,true);
  559. end;
  560. end;
  561. if actasmtoken in [AS_PLUS,AS_MINUS] then
  562. inc(l,BuildConstExpression(true,false));
  563. case oper.opr.typ of
  564. OPR_LOCAL :
  565. begin
  566. { don't allow direct access to fields of parameters, because that
  567. will generate buggy code. Allow it only for explicit typecasting }
  568. if hasdot and
  569. (not oper.hastype) then
  570. checklocalsubscript(oper.opr.localsym);
  571. inc(oper.opr.localsymofs,l)
  572. end;
  573. OPR_CONSTANT :
  574. inc(oper.opr.val,l);
  575. OPR_REFERENCE :
  576. if (mangledname<>'') then
  577. begin
  578. if (oper.opr.val<>0) then
  579. Message(asmr_e_wrong_sym_type);
  580. oper.opr.typ:=OPR_SYMBOL;
  581. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname,AT_FUNCTION);
  582. end
  583. else
  584. inc(oper.opr.val,l);
  585. OPR_SYMBOL:
  586. Message(asmr_e_invalid_symbol_ref);
  587. else
  588. internalerror(200309221);
  589. end;
  590. end;
  591. function MaybeBuildReference(is64bit: boolean):boolean;
  592. { Try to create a reference, if not a reference is found then false
  593. is returned }
  594. begin
  595. MaybeBuildReference:=true;
  596. case actasmtoken of
  597. AS_INTNUM,
  598. AS_MINUS,
  599. AS_PLUS:
  600. Begin
  601. oper.opr.ref.offset:=BuildConstExpression(True,False);
  602. if actasmtoken<>AS_LPAREN then
  603. Message(asmr_e_invalid_reference_syntax)
  604. else
  605. BuildReference(oper,is64bit);
  606. end;
  607. AS_LPAREN:
  608. BuildReference(oper,is64bit);
  609. AS_ID: { only a variable is allowed ... }
  610. Begin
  611. ReadSym(oper,is64bit);
  612. case actasmtoken of
  613. AS_end,
  614. AS_SEPARATOR,
  615. AS_COMMA: ;
  616. AS_LPAREN:
  617. BuildReference(oper,is64bit);
  618. else
  619. Begin
  620. Message(asmr_e_invalid_reference_syntax);
  621. Consume(actasmtoken);
  622. end;
  623. end; {end case }
  624. end;
  625. else
  626. MaybeBuildReference:=false;
  627. end; { end case }
  628. end;
  629. var
  630. tempreg: tregister;
  631. hl: tasmlabel;
  632. icond: tasmcond;
  633. Begin
  634. expr:='';
  635. case actasmtoken of
  636. AS_LBRACKET: { Memory reference or constant expression }
  637. Begin
  638. oper.InitRef;
  639. BuildReference(oper,is64bit);
  640. end;
  641. AS_HASH: { Constant expression }
  642. Begin
  643. Consume(AS_HASH);
  644. BuildConstantOperand(oper);
  645. end;
  646. (*
  647. AS_INTNUM,
  648. AS_MINUS,
  649. AS_PLUS:
  650. Begin
  651. { Constant memory offset }
  652. { This must absolutely be followed by ( }
  653. oper.InitRef;
  654. oper.opr.ref.offset:=BuildConstExpression(True,False);
  655. if actasmtoken<>AS_LPAREN then
  656. begin
  657. ofs:=oper.opr.ref.offset;
  658. BuildConstantOperand(oper);
  659. inc(oper.opr.val,ofs);
  660. end
  661. else
  662. BuildReference(oper,is64bit);
  663. end;
  664. *)
  665. AS_ID: { A constant expression, or a Variable ref. }
  666. Begin
  667. { Condition code? }
  668. icond:=ToConditionCode(actasmpattern,true);
  669. if icond<>C_None then
  670. begin
  671. oper.opr.typ:=OPR_COND;
  672. oper.opr.cc:=icond;
  673. consume(AS_ID);
  674. end
  675. else
  676. { Local Label ? }
  677. if is_locallabel(actasmpattern) then
  678. begin
  679. CreateLocalLabel(actasmpattern,hl,false);
  680. Consume(AS_ID);
  681. AddLabelOperand(hl);
  682. end
  683. else
  684. { Check for label }
  685. if SearchLabel(actasmpattern,hl,false) then
  686. begin
  687. Consume(AS_ID);
  688. AddLabelOperand(hl);
  689. end
  690. else
  691. { probably a variable or normal expression }
  692. { or a procedure (such as in CALL ID) }
  693. begin
  694. { is it a constant ? }
  695. if SearchIConstant(actasmpattern,l) then
  696. begin
  697. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  698. Message(asmr_e_invalid_operand_type);
  699. BuildConstantOperand(oper);
  700. end
  701. else
  702. begin
  703. expr:=actasmpattern;
  704. Consume(AS_ID);
  705. { typecasting? }
  706. if (actasmtoken=AS_LPAREN) and
  707. SearchType(expr,typesize) then
  708. begin
  709. oper.hastype:=true;
  710. Consume(AS_LPAREN);
  711. BuildOperand(oper,is64bit);
  712. Consume(AS_RPAREN);
  713. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  714. oper.SetSize(typesize,true);
  715. end
  716. else
  717. begin
  718. if not(oper.SetupVar(expr,false)) then
  719. Begin
  720. { look for special symbols ... }
  721. if expr= '__HIGH' then
  722. begin
  723. consume(AS_LPAREN);
  724. if not oper.setupvar('high'+actasmpattern,false) then
  725. Message1(sym_e_unknown_id,'high'+actasmpattern);
  726. consume(AS_ID);
  727. consume(AS_RPAREN);
  728. end
  729. else
  730. if expr = '__RESULT' then
  731. oper.SetUpResult
  732. else
  733. if expr = '__SELF' then
  734. oper.SetupSelf
  735. else
  736. if expr = '__OLDEBP' then
  737. oper.SetupOldEBP
  738. else
  739. Message1(sym_e_unknown_id,expr);
  740. end
  741. else if oper.opr.typ<>OPR_LOCAL then
  742. begin
  743. oper.InitRef;
  744. MaybeAddGotAddrMode;
  745. end;
  746. end;
  747. end;
  748. if actasmtoken=AS_DOT then
  749. MaybeRecordOffset;
  750. { add a constant expression? }
  751. if (actasmtoken=AS_PLUS) then
  752. begin
  753. l:=BuildConstExpression(true,false);
  754. case oper.opr.typ of
  755. OPR_CONSTANT :
  756. inc(oper.opr.val,l);
  757. OPR_LOCAL :
  758. inc(oper.opr.localsymofs,l);
  759. OPR_REFERENCE :
  760. inc(oper.opr.ref.offset,l);
  761. else
  762. internalerror(200309202);
  763. end;
  764. end
  765. end;
  766. { Do we have a indexing reference, then parse it also }
  767. if actasmtoken=AS_LPAREN then
  768. BuildReference(oper,is64bit);
  769. end;
  770. { Register, a variable reference or a constant reference }
  771. AS_REGISTER:
  772. Begin
  773. { save the type of register used. }
  774. tempreg:=actasmregister;
  775. Consume(AS_REGISTER);
  776. if (actasmtoken in [AS_end,AS_SEPARATOR,AS_COMMA]) then
  777. Begin
  778. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  779. Message(asmr_e_invalid_operand_type);
  780. oper.opr.typ:=OPR_REGISTER;
  781. oper.opr.reg:=tempreg;
  782. end
  783. else
  784. Message(asmr_e_syn_operand);
  785. end;
  786. AS_end,
  787. AS_SEPARATOR,
  788. AS_COMMA: ;
  789. else
  790. Begin
  791. Message(asmr_e_syn_operand);
  792. Consume(actasmtoken);
  793. end;
  794. end; { end case }
  795. end;
  796. {*****************************************************************************
  797. taarch64attreader
  798. *****************************************************************************}
  799. procedure taarch64attreader.BuildOpCode(instr: taarch64instruction);
  800. var
  801. operandnum : longint;
  802. Begin
  803. { opcode }
  804. if (actasmtoken<>AS_OPCODE) then
  805. Begin
  806. Message(asmr_e_invalid_or_missing_opcode);
  807. RecoverConsume(true);
  808. exit;
  809. end;
  810. { Fill the instr object with the current state }
  811. with instr do
  812. begin
  813. Opcode:=ActOpcode;
  814. condition:=ActCondition;
  815. oppostfix:=actoppostfix;
  816. end;
  817. Consume(AS_OPCODE);
  818. { We are reading operands, so opcode will be an AS_ID }
  819. operandnum:=1;
  820. { Zero operand opcode ? }
  821. if actasmtoken in [AS_SEPARATOR,AS_end] then
  822. begin
  823. instr.Ops:=0;
  824. exit;
  825. end;
  826. { Read the operands }
  827. repeat
  828. case actasmtoken of
  829. AS_COMMA: { Operand delimiter }
  830. Begin
  831. { operandnum and not operandnum+1, because tinstruction is
  832. one-based and taicpu is zero-based)
  833. }
  834. if can_be_shifter_operand(instr.opcode,operandnum) then
  835. begin
  836. Consume(AS_COMMA);
  837. if not TryBuildShifterOp(instr,operandnum+1) then
  838. Message(asmr_e_illegal_shifterop_syntax);
  839. Inc(operandnum);
  840. end
  841. else
  842. begin
  843. if operandnum>Max_Operands then
  844. Message(asmr_e_too_many_operands)
  845. else
  846. Inc(operandnum);
  847. Consume(AS_COMMA);
  848. end;
  849. end;
  850. AS_SEPARATOR,
  851. AS_end : { End of asm operands for this opcode }
  852. begin
  853. break;
  854. end;
  855. else
  856. begin
  857. BuildOperand(taarch64operand(instr.operands[operandnum]),instr.Is64bit);
  858. instr.Ops:=operandnum;
  859. if instr.operands[operandnum].opr.typ=OPR_REFERENCE then
  860. if simple_ref_type(instr.opcode,instr.cgsize,instr.oppostfix,instr.operands[operandnum].opr.ref)<>sr_simple then
  861. Message(asmr_e_invalid_reference_syntax);
  862. ;
  863. end;
  864. end; { end case }
  865. until false;
  866. end;
  867. function taarch64attreader.is_asmopcode(const s: string):boolean;
  868. const
  869. { sorted by length so longer postfixes will match first }
  870. postfix2strsorted : array[1..7] of string[3] = (
  871. 'SB','SH','SW',
  872. 'B','H','W',
  873. 'S');
  874. postfixsorted : array[1..7] of TOpPostfix = (
  875. PF_SB,PF_SH,PF_SW,
  876. PF_B,PF_H,PF_W,
  877. PF_S);
  878. var
  879. j : longint;
  880. hs : string;
  881. maxlen : longint;
  882. icond : tasmcond;
  883. Begin
  884. { making s a value parameter would break other assembler readers }
  885. hs:=s;
  886. is_asmopcode:=false;
  887. { clear opcode }
  888. actopcode:=A_None;
  889. actcondition:=C_None;
  890. { b.cond ? }
  891. if (length(hs)=4) and
  892. (hs[1]='B') and
  893. (hs[2]='.') then
  894. begin
  895. actopcode:=A_B;
  896. actasmtoken:=AS_OPCODE;
  897. actcondition:=ToConditionCode(copy(hs,3,length(actasmpattern)-2),false);
  898. if actcondition<>C_None then
  899. is_asmopcode:=true;
  900. exit;
  901. end;
  902. maxlen:=max(length(hs),7);
  903. actopcode:=A_NONE;
  904. for j:=maxlen downto 1 do
  905. begin
  906. actopcode:=tasmop(PtrUInt(iasmops.Find(copy(hs,1,j))));
  907. if actopcode<>A_NONE then
  908. begin
  909. actasmtoken:=AS_OPCODE;
  910. { strip op code }
  911. delete(hs,1,j);
  912. break;
  913. end;
  914. end;
  915. if actopcode=A_NONE then
  916. exit;
  917. { check for postfix }
  918. if length(hs)>0 then
  919. begin
  920. for j:=low(postfixsorted) to high(postfixsorted) do
  921. begin
  922. if copy(hs,1,length(postfix2strsorted[j]))=postfix2strsorted[j] then
  923. begin
  924. actoppostfix:=postfixsorted[j];
  925. { strip postfix }
  926. delete(hs,1,length(postfix2strsorted[j]));
  927. break;
  928. end;
  929. end;
  930. end;
  931. { if we stripped all postfixes, it's a valid opcode }
  932. is_asmopcode:=length(hs)=0;
  933. end;
  934. procedure taarch64attreader.ConvertCalljmp(instr: taarch64instruction);
  935. var
  936. newopr : toprrec;
  937. begin
  938. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  939. begin
  940. newopr.typ:=OPR_SYMBOL;
  941. newopr.symbol:=instr.Operands[1].opr.ref.symbol;
  942. newopr.symofs:=instr.Operands[1].opr.ref.offset;
  943. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  944. (instr.Operands[1].opr.ref.index<>NR_NO) or
  945. (instr.Operands[1].opr.ref.refaddr<>addr_pic) then
  946. Message(asmr_e_syn_operand);
  947. instr.Operands[1].opr:=newopr;
  948. end;
  949. end;
  950. procedure taarch64attreader.handleopcode;
  951. var
  952. instr: taarch64instruction;
  953. begin
  954. instr:=taarch64instruction.Create(taarch64operand);
  955. BuildOpcode(instr);
  956. if is_calljmp(instr.opcode) then
  957. ConvertCalljmp(instr);
  958. {
  959. instr.AddReferenceSizes;
  960. instr.SetInstructionOpsize;
  961. instr.CheckOperandSizes;
  962. }
  963. instr.ConcatInstruction(curlist);
  964. instr.Free;
  965. actoppostfix:=PF_None;
  966. end;
  967. {*****************************************************************************
  968. Initialize
  969. *****************************************************************************}
  970. const
  971. asmmode_arm_att_info : tasmmodeinfo =
  972. (
  973. id : asmmode_arm_gas;
  974. idtxt : 'GAS';
  975. casmreader : taarch64attreader;
  976. );
  977. asmmode_arm_standard_info : tasmmodeinfo =
  978. (
  979. id : asmmode_standard;
  980. idtxt : 'STANDARD';
  981. casmreader : taarch64attreader;
  982. );
  983. initialization
  984. RegisterAsmMode(asmmode_arm_att_info);
  985. RegisterAsmMode(asmmode_arm_standard_info);
  986. end.