rappcgas.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. {
  2. $Id: rappcgas.pas,v 1.19 2005/02/14 17:13:10 peter Exp $
  3. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  4. Does the parsing for the PowerPC 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 rappcgas;
  19. {$I fpcdefs.inc}
  20. interface
  21. uses
  22. raatt, rappc;
  23. type
  24. tppcattreader = class(tattreader)
  25. function is_asmopcode(const s: string): boolean; override;
  26. procedure handleopcode; override;
  27. procedure BuildReference(oper: tppcoperand);
  28. procedure BuildOperand(oper: tppcoperand);
  29. procedure BuildOpCode(instr: tppcinstruction);
  30. procedure ReadAt(oper: tppcoperand);
  31. procedure ReadSym(oper: tppcoperand);
  32. procedure ConvertCalljmp(instr: tppcinstruction);
  33. function is_targetdirective(const s: string): boolean; override;
  34. procedure HandleTargetDirective; override;
  35. end;
  36. implementation
  37. uses
  38. { helpers }
  39. cutils,
  40. { global }
  41. globtype, globals, verbose,
  42. systems,
  43. { aasm }
  44. cpubase, aasmbase, aasmtai,aasmdata, aasmcpu,
  45. { symtable }
  46. symconst, symsym, symdef,
  47. { parser }
  48. procinfo,
  49. rabase, rautils,
  50. cgbase, cgobj, cgppc, paramgr
  51. ;
  52. procedure tppcattreader.ReadSym(oper: tppcoperand);
  53. var
  54. tempstr, mangledname: string;
  55. typesize, l, k: aint;
  56. begin
  57. tempstr := actasmpattern;
  58. Consume(AS_ID);
  59. { typecasting? }
  60. if (actasmtoken = AS_LPAREN) and
  61. SearchType(tempstr, typesize) then
  62. begin
  63. oper.hastype := true;
  64. Consume(AS_LPAREN);
  65. BuildOperand(oper);
  66. Consume(AS_RPAREN);
  67. if oper.opr.typ in [OPR_REFERENCE, OPR_LOCAL] then
  68. oper.SetSize(typesize, true);
  69. end
  70. else if not oper.SetupVar(tempstr, false) then
  71. Message1(sym_e_unknown_id, tempstr);
  72. { record.field ? }
  73. if actasmtoken = AS_DOT then
  74. begin
  75. BuildRecordOffsetSize(tempstr, l, k, mangledname, false);
  76. if (mangledname<>'') then
  77. Message(asmr_e_invalid_reference_syntax);
  78. inc(oper.opr.ref.offset, l);
  79. end;
  80. end;
  81. procedure tppcattreader.ReadAt(oper: tppcoperand);
  82. begin
  83. { check for ...@ }
  84. if actasmtoken = AS_AT then
  85. begin
  86. if (oper.opr.ref.symbol = nil) and
  87. (oper.opr.ref.offset = 0) then
  88. Message(asmr_e_invalid_reference_syntax);
  89. Consume(AS_AT);
  90. if actasmtoken = AS_ID then
  91. begin
  92. if upper(actasmpattern) = 'L' then
  93. oper.opr.ref.refaddr := addr_low
  94. else if upper(actasmpattern) = 'HA' then
  95. oper.opr.ref.refaddr := addr_higha
  96. else if upper(actasmpattern) = 'H' then
  97. oper.opr.ref.refaddr := addr_high
  98. else if upper(actasmpattern) = 'HIGHERA' then
  99. oper.opr.ref.refaddr := addr_highera
  100. else if upper(actasmpattern) = 'HIGHESTA' then
  101. oper.opr.ref.refaddr := addr_highesta
  102. else if upper(actasmpattern) = 'HIGHER' then
  103. oper.opr.ref.refaddr := addr_higher
  104. else if upper(actasmpattern) = 'HIGHEST' then
  105. oper.opr.ref.refaddr := addr_highest
  106. else
  107. Message(asmr_e_invalid_reference_syntax);
  108. { darwin/ppc64's relocation symbols are 32 bits }
  109. if (target_info.system = system_powerpc64_darwin) and
  110. (not (oper.opr.ref.refaddr in [addr_no, addr_low, addr_higha])) then
  111. Message(asmr_e_invalid_reference_syntax);
  112. Consume(AS_ID);
  113. end
  114. else
  115. Message(asmr_e_invalid_reference_syntax);
  116. end;
  117. end;
  118. procedure tppcattreader.BuildReference(oper: tppcoperand);
  119. procedure Consume_RParen;
  120. begin
  121. if actasmtoken <> AS_RPAREN then
  122. begin
  123. Message(asmr_e_invalid_reference_syntax);
  124. RecoverConsume(true);
  125. end
  126. else
  127. begin
  128. Consume(AS_RPAREN);
  129. if not (actasmtoken in [AS_COMMA, AS_SEPARATOR, AS_END]) then
  130. begin
  131. Message(asmr_e_invalid_reference_syntax);
  132. RecoverConsume(true);
  133. end;
  134. end;
  135. end;
  136. var
  137. l: aint;
  138. relsym: string;
  139. asmsymtyp: tasmsymtype;
  140. begin
  141. Consume(AS_LPAREN);
  142. case actasmtoken of
  143. AS_INTNUM,
  144. AS_MINUS,
  145. AS_PLUS:
  146. begin
  147. { offset(offset) is invalid }
  148. if oper.opr.Ref.Offset <> 0 then
  149. begin
  150. Message(asmr_e_invalid_reference_syntax);
  151. RecoverConsume(true);
  152. end
  153. else
  154. begin
  155. oper.opr.Ref.Offset := BuildConstExpression(false, true);
  156. Consume(AS_RPAREN);
  157. if actasmtoken = AS_AT then
  158. ReadAt(oper);
  159. end;
  160. exit;
  161. end;
  162. AS_REGISTER: { (reg ... }
  163. begin
  164. if ((oper.opr.typ = OPR_REFERENCE) and (oper.opr.ref.base <> NR_NO)) or
  165. ((oper.opr.typ = OPR_LOCAL) and (oper.opr.localsym.localloc.loc <>
  166. LOC_REGISTER)) then
  167. message(asmr_e_cannot_index_relative_var);
  168. oper.opr.ref.base := actasmregister;
  169. Consume(AS_REGISTER);
  170. { can either be a register or a right parenthesis }
  171. { (reg) }
  172. if actasmtoken = AS_RPAREN then
  173. begin
  174. { detect RTOC-based symbol accesses }
  175. if assigned(oper.opr.ref.symbol) and
  176. (oper.opr.ref.base=NR_RTOC) and
  177. (oper.opr.ref.offset=0) then
  178. begin
  179. { replace global symbol reference with TOC entry name
  180. for AIX }
  181. if target_info.system in systems_aix then
  182. tcgppcgen(cg).get_aix_toc_sym(nil,oper.opr.ref.symbol.name,asmsym2indsymflags(oper.opr.ref.symbol),oper.opr.ref,true);
  183. oper.opr.ref.refaddr:=addr_pic_no_got;
  184. end;
  185. Consume_RParen;
  186. exit;
  187. end;
  188. { (reg,reg .. }
  189. Consume(AS_COMMA);
  190. if (actasmtoken = AS_REGISTER) and
  191. (oper.opr.Ref.Offset = 0) then
  192. begin
  193. oper.opr.ref.index := actasmregister;
  194. Consume(AS_REGISTER);
  195. Consume_RParen;
  196. end
  197. else
  198. begin
  199. Message(asmr_e_invalid_reference_syntax);
  200. RecoverConsume(false);
  201. end;
  202. end; {end case }
  203. AS_ID:
  204. begin
  205. ReadSym(oper);
  206. case actasmtoken of
  207. AS_PLUS:
  208. begin
  209. { add a constant expression? }
  210. l:=BuildConstExpression(true,true);
  211. case oper.opr.typ of
  212. OPR_CONSTANT :
  213. inc(oper.opr.val,l);
  214. OPR_LOCAL :
  215. inc(oper.opr.localsymofs,l);
  216. OPR_REFERENCE :
  217. inc(oper.opr.ref.offset,l);
  218. else
  219. internalerror(200309202);
  220. end;
  221. end;
  222. AS_MINUS:
  223. begin
  224. Consume(AS_MINUS);
  225. BuildConstSymbolExpression(false,true,false,l,relsym,asmsymtyp);
  226. if (relsym<>'') then
  227. begin
  228. if (oper.opr.typ = OPR_REFERENCE) then
  229. oper.opr.ref.relsymbol:=current_asmdata.RefAsmSymbol(relsym,asmsymtyp)
  230. else
  231. begin
  232. Message(asmr_e_invalid_reference_syntax);
  233. RecoverConsume(false);
  234. end
  235. end
  236. else
  237. begin
  238. case oper.opr.typ of
  239. OPR_CONSTANT :
  240. dec(oper.opr.val,l);
  241. OPR_LOCAL :
  242. dec(oper.opr.localsymofs,l);
  243. OPR_REFERENCE :
  244. dec(oper.opr.ref.offset,l);
  245. else
  246. internalerror(2007092601);
  247. end;
  248. end;
  249. end;
  250. end;
  251. Consume(AS_RPAREN);
  252. if actasmtoken = AS_AT then
  253. ReadAt(oper);
  254. end;
  255. AS_COMMA: { (, ... can either be scaling, or index }
  256. begin
  257. Consume(AS_COMMA);
  258. { Index }
  259. if (actasmtoken = AS_REGISTER) then
  260. begin
  261. oper.opr.ref.index := actasmregister;
  262. Consume(AS_REGISTER);
  263. { check for scaling ... }
  264. Consume_RParen;
  265. end
  266. else
  267. begin
  268. Message(asmr_e_invalid_reference_syntax);
  269. RecoverConsume(false);
  270. end;
  271. end;
  272. else
  273. begin
  274. Message(asmr_e_invalid_reference_syntax);
  275. RecoverConsume(false);
  276. end;
  277. end;
  278. end;
  279. procedure tppcattreader.BuildOperand(oper: tppcoperand);
  280. var
  281. expr: string;
  282. typesize, l: aint;
  283. procedure AddLabelOperand(hl: tasmlabel);
  284. begin
  285. if not (actasmtoken in [AS_PLUS, AS_MINUS, AS_LPAREN]) and
  286. is_calljmp(actopcode) then
  287. begin
  288. oper.opr.typ := OPR_SYMBOL;
  289. oper.opr.symbol := hl;
  290. end
  291. else
  292. begin
  293. oper.InitRef;
  294. oper.opr.ref.symbol := hl;
  295. end;
  296. end;
  297. procedure MaybeRecordOffset;
  298. var
  299. mangledname : string;
  300. hasdot: boolean;
  301. l,
  302. toffset,
  303. tsize: aint;
  304. begin
  305. if not (actasmtoken in [AS_DOT, AS_PLUS, AS_MINUS]) then
  306. exit;
  307. l := 0;
  308. mangledname := '';
  309. hasdot := (actasmtoken = AS_DOT);
  310. if hasdot then
  311. begin
  312. if expr <> '' then
  313. begin
  314. BuildRecordOffsetSize(expr, toffset, tsize, mangledname, false);
  315. if (oper.opr.typ<>OPR_CONSTANT) and
  316. (mangledname<>'') then
  317. Message(asmr_e_wrong_sym_type);
  318. inc(l, toffset);
  319. oper.SetSize(tsize, true);
  320. end;
  321. end;
  322. if actasmtoken in [AS_PLUS, AS_MINUS] then
  323. inc(l, BuildConstExpression(true, false));
  324. case oper.opr.typ of
  325. OPR_LOCAL:
  326. begin
  327. { don't allow direct access to fields of parameters, because that
  328. will generate buggy code. Allow it only for explicit typecasting }
  329. if hasdot and
  330. (not oper.hastype) and
  331. (oper.opr.localsym.typ=paravarsym) and
  332. ((tparavarsym(oper.opr.localsym).paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  333. not paramanager.push_addr_param(oper.opr.localsym.varspez,oper.opr.localsym.vardef,current_procinfo.procdef.proccalloption)) then
  334. Message(asmr_e_cannot_access_field_directly_for_parameters);
  335. inc(oper.opr.localsymofs, l)
  336. end;
  337. OPR_CONSTANT:
  338. if (mangledname<>'') then
  339. begin
  340. if (oper.opr.val<>0) then
  341. Message(asmr_e_wrong_sym_type);
  342. oper.opr.typ:=OPR_SYMBOL;
  343. oper.opr.symbol:=current_asmdata.DefineAsmSymbol(mangledname,AB_EXTERNAL,AT_FUNCTION,voidcodepointertype);
  344. end
  345. else
  346. inc(oper.opr.val,l);
  347. OPR_REFERENCE:
  348. inc(oper.opr.ref.offset, l);
  349. OPR_SYMBOL:
  350. Message(asmr_e_invalid_symbol_ref);
  351. else
  352. internalerror(200309221);
  353. end;
  354. end;
  355. function MaybeBuildReference: boolean;
  356. { Try to create a reference, if not a reference is found then false
  357. is returned }
  358. begin
  359. MaybeBuildReference := true;
  360. case actasmtoken of
  361. AS_INTNUM,
  362. AS_MINUS,
  363. AS_PLUS:
  364. begin
  365. oper.opr.ref.offset := BuildConstExpression(True, False);
  366. if actasmtoken <> AS_LPAREN then
  367. Message(asmr_e_invalid_reference_syntax)
  368. else
  369. BuildReference(oper);
  370. end;
  371. AS_LPAREN:
  372. BuildReference(oper);
  373. AS_ID: { only a variable is allowed ... }
  374. begin
  375. ReadSym(oper);
  376. case actasmtoken of
  377. AS_END,
  378. AS_SEPARATOR,
  379. AS_COMMA: ;
  380. AS_LPAREN:
  381. BuildReference(oper);
  382. else
  383. begin
  384. Message(asmr_e_invalid_reference_syntax);
  385. Consume(actasmtoken);
  386. end;
  387. end; {end case }
  388. end;
  389. else
  390. MaybeBuildReference := false;
  391. end; { end case }
  392. end;
  393. var
  394. tempreg: tregister;
  395. hl: tasmlabel;
  396. ofs: aint;
  397. begin
  398. expr := '';
  399. case actasmtoken of
  400. AS_LPAREN: { Memory reference or constant expression }
  401. begin
  402. oper.InitRef;
  403. BuildReference(oper);
  404. end;
  405. AS_INTNUM,
  406. AS_MINUS,
  407. AS_PLUS:
  408. begin
  409. { Constant memory offset }
  410. { This must absolutely be followed by ( }
  411. oper.InitRef;
  412. oper.opr.ref.offset := BuildConstExpression(True, False);
  413. if actasmtoken <> AS_LPAREN then
  414. begin
  415. ofs := oper.opr.ref.offset;
  416. BuildConstantOperand(oper);
  417. inc(oper.opr.val, ofs);
  418. end
  419. else
  420. BuildReference(oper);
  421. end;
  422. AS_ID: { A constant expression, or a Variable ref. }
  423. begin
  424. { Local Label ? }
  425. if is_locallabel(actasmpattern) then
  426. begin
  427. CreateLocalLabel(actasmpattern, hl, false);
  428. Consume(AS_ID);
  429. AddLabelOperand(hl);
  430. end
  431. else
  432. { Check for label } if SearchLabel(actasmpattern, hl, false) then
  433. begin
  434. Consume(AS_ID);
  435. AddLabelOperand(hl);
  436. end
  437. else
  438. { probably a variable or normal expression }
  439. { or a procedure (such as in CALL ID) }
  440. begin
  441. { is it a constant ? }
  442. if SearchIConstant(actasmpattern, l) then
  443. begin
  444. if not (oper.opr.typ in [OPR_NONE, OPR_CONSTANT]) then
  445. Message(asmr_e_invalid_operand_type);
  446. BuildConstantOperand(oper);
  447. end
  448. else
  449. begin
  450. expr := actasmpattern;
  451. Consume(AS_ID);
  452. { typecasting? }
  453. if (actasmtoken = AS_LPAREN) and
  454. SearchType(expr, typesize) then
  455. begin
  456. oper.hastype := true;
  457. Consume(AS_LPAREN);
  458. BuildOperand(oper);
  459. Consume(AS_RPAREN);
  460. if oper.opr.typ in [OPR_REFERENCE, OPR_LOCAL] then
  461. oper.SetSize(typesize, true);
  462. end
  463. else
  464. begin
  465. if oper.SetupVar(expr, false) then
  466. ReadAt(oper)
  467. else
  468. begin
  469. { look for special symbols ... }
  470. if expr = '__HIGH' then
  471. begin
  472. consume(AS_LPAREN);
  473. if not oper.setupvar('high' + actasmpattern, false) then
  474. Message1(sym_e_unknown_id, 'high' + actasmpattern);
  475. consume(AS_ID);
  476. consume(AS_RPAREN);
  477. end
  478. else if expr = '__RESULT' then
  479. oper.SetUpResult
  480. else if expr = '__SELF' then
  481. oper.SetupSelf
  482. else if expr = '__OLDEBP' then
  483. oper.SetupOldEBP
  484. else
  485. Message1(sym_e_unknown_id, expr);
  486. end;
  487. end;
  488. end;
  489. if actasmtoken = AS_DOT then
  490. MaybeRecordOffset;
  491. { add a constant expression? }
  492. if (actasmtoken = AS_PLUS) then
  493. begin
  494. l := BuildConstExpression(true, false);
  495. case oper.opr.typ of
  496. OPR_CONSTANT:
  497. inc(oper.opr.val, l);
  498. OPR_LOCAL:
  499. inc(oper.opr.localsymofs, l);
  500. OPR_REFERENCE:
  501. inc(oper.opr.ref.offset, l);
  502. else
  503. internalerror(200309202);
  504. end;
  505. end
  506. end;
  507. { Do we have a indexing reference, then parse it also }
  508. if actasmtoken = AS_LPAREN then
  509. BuildReference(oper);
  510. end;
  511. AS_REGISTER: { Register, a variable reference or a constant reference }
  512. begin
  513. { save the type of register used. }
  514. tempreg := actasmregister;
  515. Consume(AS_REGISTER);
  516. if (actasmtoken in [AS_END, AS_SEPARATOR, AS_COMMA]) then
  517. if is_condreg(tempreg) and
  518. ((actopcode = A_BC) or
  519. (actopcode = A_BCCTR) or
  520. (actopcode = A_BCLR) or
  521. (actopcode = A_TW) or
  522. (actopcode = A_TWI)) then
  523. begin
  524. { it isn't a real operand, everything is stored in the condition }
  525. oper.opr.typ := OPR_NONE;
  526. actcondition.cr := getsupreg(tempreg);
  527. end
  528. else
  529. begin
  530. if not (oper.opr.typ in [OPR_NONE, OPR_REGISTER]) then
  531. Message(asmr_e_invalid_operand_type);
  532. oper.opr.typ := OPR_REGISTER;
  533. oper.opr.reg := tempreg;
  534. end
  535. else if is_condreg(tempreg) then
  536. begin
  537. if not (actcondition.cond in [C_T..C_DZF]) then
  538. Message(asmr_e_syn_operand);
  539. if actasmtoken = AS_STAR then
  540. begin
  541. consume(AS_STAR);
  542. if (actasmtoken = AS_INTNUM) then
  543. begin
  544. consume(AS_INTNUM);
  545. if actasmtoken = AS_PLUS then
  546. begin
  547. consume(AS_PLUS);
  548. if (actasmtoken = AS_ID) then
  549. begin
  550. oper.opr.typ := OPR_NONE;
  551. if actasmpattern = 'LT' then
  552. actcondition.crbit := (getsupreg(tempreg) - (RS_CR0)) * 4
  553. else if actasmpattern = 'GT' then
  554. actcondition.crbit := (getsupreg(tempreg) - (RS_CR0)) * 4 + 1
  555. else if actasmpattern = 'EQ' then
  556. actcondition.crbit := (getsupreg(tempreg) - (RS_CR0)) * 4 + 2
  557. else if actasmpattern = 'SO' then
  558. actcondition.crbit := (getsupreg(tempreg) - (RS_CR0)) * 4 + 3
  559. else
  560. Message(asmr_e_syn_operand);
  561. consume(AS_ID);
  562. end
  563. else
  564. Message(asmr_e_syn_operand);
  565. end
  566. else
  567. Message(asmr_e_syn_operand);
  568. end
  569. else
  570. Message(asmr_e_syn_operand);
  571. end
  572. else
  573. Message(asmr_e_syn_operand);
  574. end
  575. else
  576. Message(asmr_e_syn_operand);
  577. end;
  578. AS_END,
  579. AS_SEPARATOR,
  580. AS_COMMA: ;
  581. else
  582. begin
  583. Message(asmr_e_syn_operand);
  584. Consume(actasmtoken);
  585. end;
  586. end; { end case }
  587. end;
  588. {*****************************************************************************
  589. tppcattreader
  590. *****************************************************************************}
  591. procedure tppcattreader.BuildOpCode(instr: tppcinstruction);
  592. var
  593. operandnum: longint;
  594. begin
  595. { opcode }
  596. if (actasmtoken <> AS_OPCODE) then
  597. begin
  598. Message(asmr_e_invalid_or_missing_opcode);
  599. RecoverConsume(true);
  600. exit;
  601. end;
  602. { Fill the instr object with the current state }
  603. with instr do
  604. begin
  605. Opcode := ActOpcode;
  606. condition := ActCondition;
  607. end;
  608. { We are reading operands, so opcode will be an AS_ID }
  609. operandnum := 1;
  610. Consume(AS_OPCODE);
  611. { Zero operand opcode ? }
  612. if actasmtoken in [AS_SEPARATOR, AS_END] then
  613. begin
  614. operandnum := 0;
  615. exit;
  616. end;
  617. { Read the operands }
  618. repeat
  619. case actasmtoken of
  620. AS_COMMA: { Operand delimiter }
  621. begin
  622. if operandnum > Max_Operands then
  623. Message(asmr_e_too_many_operands)
  624. else
  625. begin
  626. { condition operands doesn't set the operand but write to the
  627. condition field of the instruction
  628. }
  629. if instr.Operands[operandnum].opr.typ <> OPR_NONE then
  630. Inc(operandnum);
  631. end;
  632. Consume(AS_COMMA);
  633. end;
  634. AS_SEPARATOR,
  635. AS_END: { End of asm operands for this opcode }
  636. begin
  637. break;
  638. end;
  639. else
  640. BuildOperand(instr.Operands[operandnum] as tppcoperand);
  641. end; { end case }
  642. until false;
  643. if (operandnum = 1) and (instr.Operands[operandnum].opr.typ = OPR_NONE) then
  644. dec(operandnum);
  645. instr.Ops := operandnum;
  646. end;
  647. function tppcattreader.is_asmopcode(const s: string): boolean;
  648. var
  649. cond: tasmcondflag;
  650. hs: string;
  651. begin
  652. { making s a value parameter would break other assembler readers }
  653. hs := s;
  654. is_asmopcode := false;
  655. { clear op code }
  656. actopcode := A_None;
  657. { clear condition }
  658. fillchar(actcondition, sizeof(actcondition), 0);
  659. { check for direction hint }
  660. if hs[length(s)] = '-' then
  661. begin
  662. dec(ord(hs[0]));
  663. actcondition.dirhint := DH_Minus;
  664. end
  665. else if hs[length(s)] = '+' then
  666. begin
  667. dec(ord(hs[0]));
  668. actcondition.dirhint := DH_Plus;
  669. end;
  670. actopcode := tasmop(ptruint(iasmops.Find(hs)));
  671. if actopcode <> A_NONE then
  672. begin
  673. if actcondition.dirhint <> DH_None then
  674. message1(asmr_e_unknown_opcode, actasmpattern);
  675. actasmtoken := AS_OPCODE;
  676. is_asmopcode := true;
  677. exit;
  678. end;
  679. { not found, check branch instructions }
  680. if hs[1] = 'B' then
  681. begin
  682. { we can search here without an extra table which is sorted by string length
  683. because we take the whole remaining string without the leading B }
  684. if copy(hs, length(s) - 1, 2) = 'LR' then
  685. begin
  686. actopcode := A_BCLR;
  687. setlength(hs, length(hs) - 2)
  688. end
  689. else if copy(hs, length(s) - 2, 3) = 'CTR' then
  690. begin
  691. actopcode := A_BCCTR;
  692. setlength(hs, length(hs) - 3)
  693. end
  694. else
  695. actopcode := A_BC;
  696. for cond := low(TAsmCondFlag) to high(TAsmCondFlag) do
  697. if copy(hs, 2, length(s) - 1) = UpperAsmCondFlag2Str[cond] then
  698. begin
  699. actcondition.simple := true;
  700. actcondition.cond := cond;
  701. if (cond in [C_LT, C_LE, C_EQ, C_GE, C_GT, C_NL, C_NE, C_NG, C_SO, C_NS,
  702. C_UN, C_NU]) then
  703. actcondition.cr := RS_CR0;
  704. actasmtoken := AS_OPCODE;
  705. is_asmopcode := true;
  706. exit;
  707. end;
  708. end;
  709. end;
  710. procedure tppcattreader.ConvertCalljmp(instr: tppcinstruction);
  711. begin
  712. if instr.Operands[1].opr.typ = OPR_CONSTANT then begin
  713. if (instr.operands[1].opr.val > 31) or
  714. (instr.operands[2].opr.typ <> OPR_CONSTANT) or
  715. (instr.operands[2].opr.val > 31) or
  716. not(instr.operands[3].opr.typ in [OPR_REFERENCE,OPR_SYMBOL]) then
  717. Message(asmr_e_syn_operand);
  718. { BO/BI notation }
  719. instr.condition.simple := false;
  720. instr.condition.bo := instr.operands[1].opr.val;
  721. instr.condition.bi := instr.operands[2].opr.val;
  722. instr.operands[1].free;
  723. instr.operands[2].free;
  724. instr.operands[2] := nil;
  725. instr.operands[1] := instr.operands[3];
  726. instr.operands[3] := nil;
  727. instr.ops := 1;
  728. end;
  729. if instr.Operands[1].opr.typ = OPR_REFERENCE then begin
  730. instr.Operands[1].opr.ref.refaddr:=addr_full;
  731. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  732. (instr.Operands[1].opr.ref.index<>NR_NO) then
  733. Message(asmr_e_syn_operand);
  734. if use_dotted_functions and
  735. assigned(instr.Operands[1].opr.ref.symbol) then
  736. instr.Operands[1].opr.ref.symbol:=current_asmdata.DefineAsmSymbol('.'+instr.Operands[1].opr.ref.symbol.name,instr.Operands[1].opr.ref.symbol.bind,AT_FUNCTION,voidcodepointertype);
  737. end;
  738. if use_dotted_functions and
  739. (instr.Operands[1].opr.typ = OPR_SYMBOL) and
  740. (instr.Operands[1].opr.symbol.typ=AT_FUNCTION) then
  741. instr.Operands[1].opr.symbol:=current_asmdata.DefineAsmSymbol('.'+instr.Operands[1].opr.symbol.name,instr.Operands[1].opr.symbol.bind,AT_FUNCTION,voidcodepointertype);
  742. end;
  743. function tppcattreader.is_targetdirective(const s: string): boolean;
  744. begin
  745. if (target_info.abi=abi_powerpc_elfv2) and
  746. (s='.localentry') then
  747. result:=true
  748. else
  749. result:=inherited;
  750. end;
  751. procedure tppcattreader.HandleTargetDirective;
  752. var
  753. symname,
  754. symval : String;
  755. val : aint;
  756. symtyp : TAsmsymtype;
  757. begin
  758. if (target_info.abi=abi_powerpc_elfv2) and
  759. (actasmpattern='.localentry') then
  760. begin
  761. { .localentry funcname, .-funcname }
  762. consume(AS_TARGET_DIRECTIVE);
  763. BuildConstSymbolExpression(true,false,false, val,symname,symtyp);
  764. Consume(AS_COMMA);
  765. { we need a '.', but these are parsed as identifiers -> if the current
  766. pattern is different from a '.' try to consume AS_DOT so we'll get
  767. the correct error message, otherwise consume this '.' identifier }
  768. if actasmpattern<>'.' then
  769. Consume(AS_DOT)
  770. else
  771. Consume(AS_ID);
  772. Consume(AS_MINUS);
  773. BuildConstSymbolExpression(true,false,false, val,symval,symtyp);
  774. curList.concat(tai_symbolpair.create(spk_localentry,symname,symval));
  775. end
  776. else
  777. inherited;
  778. end;
  779. procedure tppcattreader.handleopcode;
  780. var
  781. instr: tppcinstruction;
  782. begin
  783. instr := TPPCInstruction.Create(TPPCOperand);
  784. BuildOpcode(instr);
  785. instr.condition := actcondition;
  786. if is_calljmp(instr.opcode) then
  787. ConvertCalljmp(instr);
  788. {
  789. instr.AddReferenceSizes;
  790. instr.SetInstructionOpsize;
  791. instr.CheckOperandSizes;
  792. }
  793. instr.ConcatInstruction(curlist);
  794. instr.Free;
  795. end;
  796. {*****************************************************************************
  797. Initialize
  798. *****************************************************************************}
  799. const
  800. asmmode_ppc_att_info: tasmmodeinfo =
  801. (
  802. id: asmmode_ppc_gas;
  803. idtxt: 'GAS';
  804. casmreader: tppcattreader;
  805. );
  806. asmmode_ppc_standard_info: tasmmodeinfo =
  807. (
  808. id: asmmode_standard;
  809. idtxt: 'STANDARD';
  810. casmreader: tppcattreader;
  811. );
  812. initialization
  813. RegisterAsmMode(asmmode_ppc_att_info);
  814. RegisterAsmMode(asmmode_ppc_standard_info);
  815. end.