racpugas.pas 34 KB

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