racpugas.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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','ROR',
  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. else
  488. ;
  489. end;
  490. result:=C_None;;
  491. end;
  492. Procedure taarch64attreader.BuildOperand(oper: taarch64operand; is64bit: boolean);
  493. var
  494. expr: string;
  495. typesize, l: aint;
  496. procedure MaybeAddGotAddrMode;
  497. begin
  498. if actasmtoken=AS_AT then
  499. begin
  500. consume(AS_AT);
  501. if actasmpattern='GOTPAGE' then
  502. oper.opr.ref.refaddr:=addr_gotpage
  503. else if actasmpattern='GOTPAGEOFF' then
  504. oper.opr.ref.refaddr:=addr_gotpageoffset
  505. else if actasmpattern='PAGE' then
  506. oper.opr.ref.refaddr:=addr_page
  507. else if actasmpattern='PAGEOFF' then
  508. oper.opr.ref.refaddr:=addr_pageoffset
  509. else
  510. Message(asmr_e_expr_illegal);
  511. consume(actasmtoken);
  512. end
  513. else
  514. oper.opr.ref.refaddr:=addr_pic;
  515. end;
  516. procedure AddLabelOperand(hl:tasmlabel);
  517. begin
  518. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  519. is_calljmp(actopcode) then
  520. begin
  521. oper.opr.typ:=OPR_SYMBOL;
  522. oper.opr.symbol:=hl;
  523. end
  524. else if (actopcode=A_ADR) or
  525. (actopcode=A_ADRP) or
  526. (actopcode=A_LDR) then
  527. begin
  528. oper.InitRef;
  529. MaybeAddGotAddrMode;
  530. oper.opr.ref.symbol:=hl;
  531. if (actasmtoken in [AS_PLUS, AS_MINUS]) then
  532. begin
  533. l:=BuildConstExpression(true,false);
  534. oper.opr.ref.offset:=l;
  535. end;
  536. end;
  537. end;
  538. procedure MaybeRecordOffset;
  539. var
  540. mangledname: string;
  541. hasdot : boolean;
  542. l,
  543. toffset,
  544. tsize : aint;
  545. begin
  546. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  547. exit;
  548. l:=0;
  549. mangledname:='';
  550. hasdot:=(actasmtoken=AS_DOT);
  551. if hasdot then
  552. begin
  553. if expr<>'' then
  554. begin
  555. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  556. if (oper.opr.typ<>OPR_CONSTANT) and
  557. (mangledname<>'') then
  558. Message(asmr_e_wrong_sym_type);
  559. inc(l,toffset);
  560. oper.SetSize(tsize,true);
  561. end;
  562. end;
  563. if actasmtoken in [AS_PLUS,AS_MINUS] then
  564. inc(l,BuildConstExpression(true,false));
  565. case oper.opr.typ of
  566. OPR_LOCAL :
  567. begin
  568. { don't allow direct access to fields of parameters, because that
  569. will generate buggy code. Allow it only for explicit typecasting }
  570. if hasdot and
  571. (not oper.hastype) then
  572. checklocalsubscript(oper.opr.localsym);
  573. inc(oper.opr.localsymofs,l)
  574. end;
  575. OPR_CONSTANT :
  576. inc(oper.opr.val,l);
  577. OPR_REFERENCE :
  578. if (mangledname<>'') then
  579. begin
  580. if (oper.opr.val<>0) then
  581. Message(asmr_e_wrong_sym_type);
  582. oper.opr.typ:=OPR_SYMBOL;
  583. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname,AT_FUNCTION);
  584. end
  585. else
  586. inc(oper.opr.val,l);
  587. OPR_SYMBOL:
  588. Message(asmr_e_invalid_symbol_ref);
  589. else
  590. internalerror(200309221);
  591. end;
  592. end;
  593. function MaybeBuildReference(is64bit: boolean):boolean;
  594. { Try to create a reference, if not a reference is found then false
  595. is returned }
  596. begin
  597. MaybeBuildReference:=true;
  598. case actasmtoken of
  599. AS_INTNUM,
  600. AS_MINUS,
  601. AS_PLUS:
  602. Begin
  603. oper.opr.ref.offset:=BuildConstExpression(True,False);
  604. if actasmtoken<>AS_LPAREN then
  605. Message(asmr_e_invalid_reference_syntax)
  606. else
  607. BuildReference(oper,is64bit);
  608. end;
  609. AS_LPAREN:
  610. BuildReference(oper,is64bit);
  611. AS_ID: { only a variable is allowed ... }
  612. Begin
  613. ReadSym(oper,is64bit);
  614. case actasmtoken of
  615. AS_end,
  616. AS_SEPARATOR,
  617. AS_COMMA: ;
  618. AS_LPAREN:
  619. BuildReference(oper,is64bit);
  620. else
  621. Begin
  622. Message(asmr_e_invalid_reference_syntax);
  623. Consume(actasmtoken);
  624. end;
  625. end; {end case }
  626. end;
  627. else
  628. MaybeBuildReference:=false;
  629. end; { end case }
  630. end;
  631. var
  632. tempreg: tregister;
  633. hl: tasmlabel;
  634. icond: tasmcond;
  635. Begin
  636. expr:='';
  637. case actasmtoken of
  638. AS_LBRACKET: { Memory reference or constant expression }
  639. Begin
  640. oper.InitRef;
  641. BuildReference(oper,is64bit);
  642. end;
  643. AS_HASH: { Constant expression }
  644. Begin
  645. Consume(AS_HASH);
  646. BuildConstantOperand(oper);
  647. end;
  648. (*
  649. AS_INTNUM,
  650. AS_MINUS,
  651. AS_PLUS:
  652. Begin
  653. { Constant memory offset }
  654. { This must absolutely be followed by ( }
  655. oper.InitRef;
  656. oper.opr.ref.offset:=BuildConstExpression(True,False);
  657. if actasmtoken<>AS_LPAREN then
  658. begin
  659. ofs:=oper.opr.ref.offset;
  660. BuildConstantOperand(oper);
  661. inc(oper.opr.val,ofs);
  662. end
  663. else
  664. BuildReference(oper,is64bit);
  665. end;
  666. *)
  667. AS_ID: { A constant expression, or a Variable ref. }
  668. Begin
  669. { Condition code? }
  670. icond:=ToConditionCode(actasmpattern,true);
  671. if icond<>C_None then
  672. begin
  673. oper.opr.typ:=OPR_COND;
  674. oper.opr.cc:=icond;
  675. consume(AS_ID);
  676. end
  677. else
  678. { Local Label ? }
  679. if is_locallabel(actasmpattern) then
  680. begin
  681. CreateLocalLabel(actasmpattern,hl,false);
  682. Consume(AS_ID);
  683. AddLabelOperand(hl);
  684. end
  685. else
  686. { Check for label }
  687. if SearchLabel(actasmpattern,hl,false) then
  688. begin
  689. Consume(AS_ID);
  690. AddLabelOperand(hl);
  691. end
  692. else
  693. { probably a variable or normal expression }
  694. { or a procedure (such as in CALL ID) }
  695. begin
  696. { is it a constant ? }
  697. if SearchIConstant(actasmpattern,l) then
  698. begin
  699. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  700. Message(asmr_e_invalid_operand_type);
  701. BuildConstantOperand(oper);
  702. end
  703. else
  704. begin
  705. expr:=actasmpattern;
  706. Consume(AS_ID);
  707. { typecasting? }
  708. if (actasmtoken=AS_LPAREN) and
  709. SearchType(expr,typesize) then
  710. begin
  711. oper.hastype:=true;
  712. Consume(AS_LPAREN);
  713. BuildOperand(oper,is64bit);
  714. Consume(AS_RPAREN);
  715. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  716. oper.SetSize(typesize,true);
  717. end
  718. else
  719. begin
  720. if not(oper.SetupVar(expr,false)) then
  721. Begin
  722. { look for special symbols ... }
  723. if expr= '__HIGH' then
  724. begin
  725. consume(AS_LPAREN);
  726. if not oper.setupvar('high'+actasmpattern,false) then
  727. Message1(sym_e_unknown_id,'high'+actasmpattern);
  728. consume(AS_ID);
  729. consume(AS_RPAREN);
  730. end
  731. else
  732. if expr = '__RESULT' then
  733. oper.SetUpResult
  734. else
  735. if expr = '__SELF' then
  736. oper.SetupSelf
  737. else
  738. if expr = '__OLDEBP' then
  739. oper.SetupOldEBP
  740. else
  741. Message1(sym_e_unknown_id,expr);
  742. end
  743. else
  744. MaybeAddGotAddrMode;
  745. end;
  746. end;
  747. if actasmtoken=AS_DOT then
  748. MaybeRecordOffset;
  749. { add a constant expression? }
  750. if (actasmtoken=AS_PLUS) then
  751. begin
  752. l:=BuildConstExpression(true,false);
  753. case oper.opr.typ of
  754. OPR_CONSTANT :
  755. inc(oper.opr.val,l);
  756. OPR_LOCAL :
  757. inc(oper.opr.localsymofs,l);
  758. OPR_REFERENCE :
  759. inc(oper.opr.ref.offset,l);
  760. else
  761. internalerror(200309202);
  762. end;
  763. end
  764. end;
  765. { Do we have a indexing reference, then parse it also }
  766. if actasmtoken=AS_LPAREN then
  767. BuildReference(oper,is64bit);
  768. end;
  769. { Register, a variable reference or a constant reference }
  770. AS_REGISTER:
  771. Begin
  772. { save the type of register used. }
  773. tempreg:=actasmregister;
  774. Consume(AS_REGISTER);
  775. if (actasmtoken in [AS_end,AS_SEPARATOR,AS_COMMA]) then
  776. Begin
  777. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  778. Message(asmr_e_invalid_operand_type);
  779. oper.opr.typ:=OPR_REGISTER;
  780. oper.opr.reg:=tempreg;
  781. end
  782. else
  783. Message(asmr_e_syn_operand);
  784. end;
  785. AS_end,
  786. AS_SEPARATOR,
  787. AS_COMMA: ;
  788. else
  789. Begin
  790. Message(asmr_e_syn_operand);
  791. Consume(actasmtoken);
  792. end;
  793. end; { end case }
  794. end;
  795. {*****************************************************************************
  796. taarch64attreader
  797. *****************************************************************************}
  798. procedure taarch64attreader.BuildOpCode(instr: taarch64instruction);
  799. var
  800. operandnum : longint;
  801. Begin
  802. { opcode }
  803. if (actasmtoken<>AS_OPCODE) then
  804. Begin
  805. Message(asmr_e_invalid_or_missing_opcode);
  806. RecoverConsume(true);
  807. exit;
  808. end;
  809. { Fill the instr object with the current state }
  810. with instr do
  811. begin
  812. Opcode:=ActOpcode;
  813. condition:=ActCondition;
  814. oppostfix:=actoppostfix;
  815. end;
  816. Consume(AS_OPCODE);
  817. { We are reading operands, so opcode will be an AS_ID }
  818. operandnum:=1;
  819. { Zero operand opcode ? }
  820. if actasmtoken in [AS_SEPARATOR,AS_end] then
  821. begin
  822. instr.Ops:=0;
  823. exit;
  824. end;
  825. { Read the operands }
  826. repeat
  827. case actasmtoken of
  828. AS_COMMA: { Operand delimiter }
  829. Begin
  830. { operandnum and not operandnum+1, because tinstruction is
  831. one-based and taicpu is zero-based)
  832. }
  833. if can_be_shifter_operand(instr.opcode,operandnum) then
  834. begin
  835. Consume(AS_COMMA);
  836. if not TryBuildShifterOp(instr,operandnum+1) then
  837. Message(asmr_e_illegal_shifterop_syntax);
  838. Inc(operandnum);
  839. end
  840. else
  841. begin
  842. if operandnum>Max_Operands then
  843. Message(asmr_e_too_many_operands)
  844. else
  845. Inc(operandnum);
  846. Consume(AS_COMMA);
  847. end;
  848. end;
  849. AS_SEPARATOR,
  850. AS_end : { End of asm operands for this opcode }
  851. begin
  852. break;
  853. end;
  854. else
  855. begin
  856. BuildOperand(taarch64operand(instr.operands[operandnum]),instr.Is64bit);
  857. instr.Ops:=operandnum;
  858. if instr.operands[operandnum].opr.typ=OPR_REFERENCE then
  859. if simple_ref_type(instr.opcode,instr.cgsize,instr.oppostfix,instr.operands[operandnum].opr.ref)<>sr_simple then
  860. Message(asmr_e_invalid_reference_syntax);
  861. ;
  862. end;
  863. end; { end case }
  864. until false;
  865. end;
  866. function taarch64attreader.is_asmopcode(const s: string):boolean;
  867. const
  868. { sorted by length so longer postfixes will match first }
  869. postfix2strsorted : array[1..7] of string[3] = (
  870. 'SB','SH','SW',
  871. 'B','H','W',
  872. 'S');
  873. postfixsorted : array[1..7] of TOpPostfix = (
  874. PF_SB,PF_SH,PF_SW,
  875. PF_B,PF_H,PF_W,
  876. PF_S);
  877. var
  878. j : longint;
  879. hs : string;
  880. maxlen : longint;
  881. Begin
  882. { making s a value parameter would break other assembler readers }
  883. hs:=s;
  884. is_asmopcode:=false;
  885. { clear opcode }
  886. actopcode:=A_None;
  887. actcondition:=C_None;
  888. { b.cond ? }
  889. if (length(hs)=4) and
  890. (hs[1]='B') and
  891. (hs[2]='.') then
  892. begin
  893. actopcode:=A_B;
  894. actasmtoken:=AS_OPCODE;
  895. actcondition:=ToConditionCode(copy(hs,3,length(actasmpattern)-2),false);
  896. if actcondition<>C_None then
  897. is_asmopcode:=true;
  898. exit;
  899. end;
  900. maxlen:=max(length(hs),7);
  901. actopcode:=A_NONE;
  902. for j:=maxlen downto 1 do
  903. begin
  904. actopcode:=tasmop(PtrUInt(iasmops.Find(copy(hs,1,j))));
  905. if actopcode<>A_NONE then
  906. begin
  907. actasmtoken:=AS_OPCODE;
  908. { strip op code }
  909. delete(hs,1,j);
  910. break;
  911. end;
  912. end;
  913. if actopcode=A_NONE then
  914. exit;
  915. { check for postfix }
  916. if length(hs)>0 then
  917. begin
  918. for j:=low(postfixsorted) to high(postfixsorted) do
  919. begin
  920. if copy(hs,1,length(postfix2strsorted[j]))=postfix2strsorted[j] then
  921. begin
  922. actoppostfix:=postfixsorted[j];
  923. { strip postfix }
  924. delete(hs,1,length(postfix2strsorted[j]));
  925. break;
  926. end;
  927. end;
  928. end;
  929. { if we stripped all postfixes, it's a valid opcode }
  930. is_asmopcode:=length(hs)=0;
  931. end;
  932. procedure taarch64attreader.ConvertCalljmp(instr: taarch64instruction);
  933. var
  934. newopr : toprrec;
  935. begin
  936. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  937. begin
  938. newopr.typ:=OPR_SYMBOL;
  939. newopr.symbol:=instr.Operands[1].opr.ref.symbol;
  940. newopr.symofs:=instr.Operands[1].opr.ref.offset;
  941. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  942. (instr.Operands[1].opr.ref.index<>NR_NO) or
  943. (instr.Operands[1].opr.ref.refaddr<>addr_pic) then
  944. Message(asmr_e_syn_operand);
  945. instr.Operands[1].opr:=newopr;
  946. end;
  947. end;
  948. procedure taarch64attreader.handleopcode;
  949. var
  950. instr: taarch64instruction;
  951. begin
  952. instr:=taarch64instruction.Create(taarch64operand);
  953. BuildOpcode(instr);
  954. if is_calljmp(instr.opcode) then
  955. ConvertCalljmp(instr);
  956. {
  957. instr.AddReferenceSizes;
  958. instr.SetInstructionOpsize;
  959. instr.CheckOperandSizes;
  960. }
  961. instr.ConcatInstruction(curlist);
  962. instr.Free;
  963. actoppostfix:=PF_None;
  964. end;
  965. {*****************************************************************************
  966. Initialize
  967. *****************************************************************************}
  968. const
  969. asmmode_arm_att_info : tasmmodeinfo =
  970. (
  971. id : asmmode_arm_gas;
  972. idtxt : 'GAS';
  973. casmreader : taarch64attreader;
  974. );
  975. asmmode_arm_standard_info : tasmmodeinfo =
  976. (
  977. id : asmmode_standard;
  978. idtxt : 'STANDARD';
  979. casmreader : taarch64attreader;
  980. );
  981. initialization
  982. RegisterAsmMode(asmmode_arm_att_info);
  983. RegisterAsmMode(asmmode_arm_standard_info);
  984. end.