rappcgas.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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) then
  331. checklocalsubscript(oper.opr.localsym);
  332. inc(oper.opr.localsymofs, l)
  333. end;
  334. OPR_CONSTANT:
  335. if (mangledname<>'') then
  336. begin
  337. if (oper.opr.val<>0) then
  338. Message(asmr_e_wrong_sym_type);
  339. oper.opr.typ:=OPR_SYMBOL;
  340. oper.opr.symbol:=current_asmdata.DefineAsmSymbol(mangledname,AB_EXTERNAL,AT_FUNCTION,voidcodepointertype);
  341. end
  342. else
  343. inc(oper.opr.val,l);
  344. OPR_REFERENCE:
  345. inc(oper.opr.ref.offset, l);
  346. OPR_SYMBOL:
  347. Message(asmr_e_invalid_symbol_ref);
  348. else
  349. internalerror(200309221);
  350. end;
  351. end;
  352. function MaybeBuildReference: boolean;
  353. { Try to create a reference, if not a reference is found then false
  354. is returned }
  355. begin
  356. MaybeBuildReference := true;
  357. case actasmtoken of
  358. AS_INTNUM,
  359. AS_MINUS,
  360. AS_PLUS:
  361. begin
  362. oper.opr.ref.offset := BuildConstExpression(True, False);
  363. if actasmtoken <> AS_LPAREN then
  364. Message(asmr_e_invalid_reference_syntax)
  365. else
  366. BuildReference(oper);
  367. end;
  368. AS_LPAREN:
  369. BuildReference(oper);
  370. AS_ID: { only a variable is allowed ... }
  371. begin
  372. ReadSym(oper);
  373. case actasmtoken of
  374. AS_END,
  375. AS_SEPARATOR,
  376. AS_COMMA: ;
  377. AS_LPAREN:
  378. BuildReference(oper);
  379. else
  380. begin
  381. Message(asmr_e_invalid_reference_syntax);
  382. Consume(actasmtoken);
  383. end;
  384. end; {end case }
  385. end;
  386. else
  387. MaybeBuildReference := false;
  388. end; { end case }
  389. end;
  390. var
  391. tempreg: tregister;
  392. hl: tasmlabel;
  393. ofs: aint;
  394. begin
  395. expr := '';
  396. case actasmtoken of
  397. AS_LPAREN: { Memory reference or constant expression }
  398. begin
  399. oper.InitRef;
  400. BuildReference(oper);
  401. end;
  402. AS_INTNUM,
  403. AS_MINUS,
  404. AS_PLUS:
  405. begin
  406. { Constant memory offset }
  407. { This must absolutely be followed by ( }
  408. oper.InitRef;
  409. oper.opr.ref.offset := BuildConstExpression(True, False);
  410. if actasmtoken <> AS_LPAREN then
  411. begin
  412. ofs := oper.opr.ref.offset;
  413. BuildConstantOperand(oper);
  414. inc(oper.opr.val, ofs);
  415. end
  416. else
  417. BuildReference(oper);
  418. end;
  419. AS_ID: { A constant expression, or a Variable ref. }
  420. begin
  421. { Local Label ? }
  422. if is_locallabel(actasmpattern) then
  423. begin
  424. CreateLocalLabel(actasmpattern, hl, false);
  425. Consume(AS_ID);
  426. AddLabelOperand(hl);
  427. end
  428. else
  429. { Check for label } if SearchLabel(actasmpattern, hl, false) then
  430. begin
  431. Consume(AS_ID);
  432. AddLabelOperand(hl);
  433. end
  434. else
  435. { probably a variable or normal expression }
  436. { or a procedure (such as in CALL ID) }
  437. begin
  438. { is it a constant ? }
  439. if SearchIConstant(actasmpattern, l) then
  440. begin
  441. if not (oper.opr.typ in [OPR_NONE, OPR_CONSTANT]) then
  442. Message(asmr_e_invalid_operand_type);
  443. BuildConstantOperand(oper);
  444. end
  445. else
  446. begin
  447. expr := actasmpattern;
  448. Consume(AS_ID);
  449. { typecasting? }
  450. if (actasmtoken = AS_LPAREN) and
  451. SearchType(expr, typesize) then
  452. begin
  453. oper.hastype := true;
  454. Consume(AS_LPAREN);
  455. BuildOperand(oper);
  456. Consume(AS_RPAREN);
  457. if oper.opr.typ in [OPR_REFERENCE, OPR_LOCAL] then
  458. oper.SetSize(typesize, true);
  459. end
  460. else
  461. begin
  462. if oper.SetupVar(expr, false) then
  463. ReadAt(oper)
  464. else
  465. begin
  466. { look for special symbols ... }
  467. if expr = '__HIGH' then
  468. begin
  469. consume(AS_LPAREN);
  470. if not oper.setupvar('high' + actasmpattern, false) then
  471. Message1(sym_e_unknown_id, 'high' + actasmpattern);
  472. consume(AS_ID);
  473. consume(AS_RPAREN);
  474. end
  475. else if expr = '__RESULT' then
  476. oper.SetUpResult
  477. else if expr = '__SELF' then
  478. oper.SetupSelf
  479. else if expr = '__OLDEBP' then
  480. oper.SetupOldEBP
  481. else
  482. Message1(sym_e_unknown_id, expr);
  483. end;
  484. end;
  485. end;
  486. if actasmtoken = AS_DOT then
  487. MaybeRecordOffset;
  488. { add a constant expression? }
  489. if (actasmtoken = AS_PLUS) then
  490. begin
  491. l := BuildConstExpression(true, false);
  492. case oper.opr.typ of
  493. OPR_CONSTANT:
  494. inc(oper.opr.val, l);
  495. OPR_LOCAL:
  496. inc(oper.opr.localsymofs, l);
  497. OPR_REFERENCE:
  498. inc(oper.opr.ref.offset, l);
  499. else
  500. internalerror(200309202);
  501. end;
  502. end
  503. end;
  504. { Do we have a indexing reference, then parse it also }
  505. if actasmtoken = AS_LPAREN then
  506. BuildReference(oper);
  507. end;
  508. AS_REGISTER: { Register, a variable reference or a constant reference }
  509. begin
  510. { save the type of register used. }
  511. tempreg := actasmregister;
  512. Consume(AS_REGISTER);
  513. if (actasmtoken in [AS_END, AS_SEPARATOR, AS_COMMA]) then
  514. if is_condreg(tempreg) and
  515. ((actopcode = A_BC) or
  516. (actopcode = A_BCCTR) or
  517. (actopcode = A_BCLR) or
  518. (actopcode = A_TW) or
  519. (actopcode = A_TWI)) then
  520. begin
  521. { it isn't a real operand, everything is stored in the condition }
  522. oper.opr.typ := OPR_NONE;
  523. actcondition.cr := getsupreg(tempreg);
  524. end
  525. else
  526. begin
  527. if not (oper.opr.typ in [OPR_NONE, OPR_REGISTER]) then
  528. Message(asmr_e_invalid_operand_type);
  529. oper.opr.typ := OPR_REGISTER;
  530. oper.opr.reg := tempreg;
  531. end
  532. else if is_condreg(tempreg) then
  533. begin
  534. if not (actcondition.cond in [C_T..C_DZF]) then
  535. Message(asmr_e_syn_operand);
  536. if actasmtoken = AS_STAR then
  537. begin
  538. consume(AS_STAR);
  539. if (actasmtoken = AS_INTNUM) then
  540. begin
  541. consume(AS_INTNUM);
  542. if actasmtoken = AS_PLUS then
  543. begin
  544. consume(AS_PLUS);
  545. if (actasmtoken = AS_ID) then
  546. begin
  547. oper.opr.typ := OPR_NONE;
  548. if actasmpattern = 'LT' then
  549. actcondition.crbit := (getsupreg(tempreg) - (RS_CR0)) * 4
  550. else if actasmpattern = 'GT' then
  551. actcondition.crbit := (getsupreg(tempreg) - (RS_CR0)) * 4 + 1
  552. else if actasmpattern = 'EQ' then
  553. actcondition.crbit := (getsupreg(tempreg) - (RS_CR0)) * 4 + 2
  554. else if actasmpattern = 'SO' then
  555. actcondition.crbit := (getsupreg(tempreg) - (RS_CR0)) * 4 + 3
  556. else
  557. Message(asmr_e_syn_operand);
  558. consume(AS_ID);
  559. end
  560. else
  561. Message(asmr_e_syn_operand);
  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. AS_END,
  576. AS_SEPARATOR,
  577. AS_COMMA: ;
  578. else
  579. begin
  580. Message(asmr_e_syn_operand);
  581. Consume(actasmtoken);
  582. end;
  583. end; { end case }
  584. end;
  585. {*****************************************************************************
  586. tppcattreader
  587. *****************************************************************************}
  588. procedure tppcattreader.BuildOpCode(instr: tppcinstruction);
  589. var
  590. operandnum: longint;
  591. begin
  592. { opcode }
  593. if (actasmtoken <> AS_OPCODE) then
  594. begin
  595. Message(asmr_e_invalid_or_missing_opcode);
  596. RecoverConsume(true);
  597. exit;
  598. end;
  599. { Fill the instr object with the current state }
  600. with instr do
  601. begin
  602. Opcode := ActOpcode;
  603. condition := ActCondition;
  604. end;
  605. { We are reading operands, so opcode will be an AS_ID }
  606. operandnum := 1;
  607. Consume(AS_OPCODE);
  608. { Zero operand opcode ? }
  609. if actasmtoken in [AS_SEPARATOR, AS_END] then
  610. begin
  611. operandnum := 0;
  612. exit;
  613. end;
  614. { Read the operands }
  615. repeat
  616. case actasmtoken of
  617. AS_COMMA: { Operand delimiter }
  618. begin
  619. if operandnum > Max_Operands then
  620. Message(asmr_e_too_many_operands)
  621. else
  622. begin
  623. { condition operands doesn't set the operand but write to the
  624. condition field of the instruction
  625. }
  626. if instr.Operands[operandnum].opr.typ <> OPR_NONE then
  627. Inc(operandnum);
  628. end;
  629. Consume(AS_COMMA);
  630. end;
  631. AS_SEPARATOR,
  632. AS_END: { End of asm operands for this opcode }
  633. begin
  634. break;
  635. end;
  636. else
  637. BuildOperand(instr.Operands[operandnum] as tppcoperand);
  638. end; { end case }
  639. until false;
  640. if (operandnum = 1) and (instr.Operands[operandnum].opr.typ = OPR_NONE) then
  641. dec(operandnum);
  642. instr.Ops := operandnum;
  643. end;
  644. function tppcattreader.is_asmopcode(const s: string): boolean;
  645. var
  646. cond: tasmcondflag;
  647. hs: string;
  648. begin
  649. { making s a value parameter would break other assembler readers }
  650. hs := s;
  651. is_asmopcode := false;
  652. { clear op code }
  653. actopcode := A_None;
  654. { clear condition }
  655. fillchar(actcondition, sizeof(actcondition), 0);
  656. { check for direction hint }
  657. if hs[length(s)] = '-' then
  658. begin
  659. dec(ord(hs[0]));
  660. actcondition.dirhint := DH_Minus;
  661. end
  662. else if hs[length(s)] = '+' then
  663. begin
  664. dec(ord(hs[0]));
  665. actcondition.dirhint := DH_Plus;
  666. end;
  667. actopcode := tasmop(ptruint(iasmops.Find(hs)));
  668. if actopcode <> A_NONE then
  669. begin
  670. if actcondition.dirhint <> DH_None then
  671. message1(asmr_e_unknown_opcode, actasmpattern);
  672. actasmtoken := AS_OPCODE;
  673. is_asmopcode := true;
  674. exit;
  675. end;
  676. { not found, check branch instructions }
  677. if hs[1] = 'B' then
  678. begin
  679. { we can search here without an extra table which is sorted by string length
  680. because we take the whole remaining string without the leading B }
  681. if copy(hs, length(s) - 1, 2) = 'LR' then
  682. begin
  683. actopcode := A_BCLR;
  684. setlength(hs, length(hs) - 2)
  685. end
  686. else if copy(hs, length(s) - 2, 3) = 'CTR' then
  687. begin
  688. actopcode := A_BCCTR;
  689. setlength(hs, length(hs) - 3)
  690. end
  691. else
  692. actopcode := A_BC;
  693. for cond := low(TAsmCondFlag) to high(TAsmCondFlag) do
  694. if copy(hs, 2, length(s) - 1) = UpperAsmCondFlag2Str[cond] then
  695. begin
  696. actcondition.simple := true;
  697. actcondition.cond := cond;
  698. if (cond in [C_LT, C_LE, C_EQ, C_GE, C_GT, C_NL, C_NE, C_NG, C_SO, C_NS,
  699. C_UN, C_NU]) then
  700. actcondition.cr := RS_CR0;
  701. actasmtoken := AS_OPCODE;
  702. is_asmopcode := true;
  703. exit;
  704. end;
  705. end;
  706. end;
  707. procedure tppcattreader.ConvertCalljmp(instr: tppcinstruction);
  708. begin
  709. if instr.Operands[1].opr.typ = OPR_CONSTANT then begin
  710. if (instr.operands[1].opr.val > 31) or
  711. (instr.operands[2].opr.typ <> OPR_CONSTANT) or
  712. (instr.operands[2].opr.val > 31) or
  713. not(instr.operands[3].opr.typ in [OPR_REFERENCE,OPR_SYMBOL]) then
  714. Message(asmr_e_syn_operand);
  715. { BO/BI notation }
  716. instr.condition.simple := false;
  717. instr.condition.bo := instr.operands[1].opr.val;
  718. instr.condition.bi := instr.operands[2].opr.val;
  719. instr.operands[1].free;
  720. instr.operands[2].free;
  721. instr.operands[2] := nil;
  722. instr.operands[1] := instr.operands[3];
  723. instr.operands[3] := nil;
  724. instr.ops := 1;
  725. end;
  726. if instr.Operands[1].opr.typ = OPR_REFERENCE then begin
  727. instr.Operands[1].opr.ref.refaddr:=addr_full;
  728. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  729. (instr.Operands[1].opr.ref.index<>NR_NO) then
  730. Message(asmr_e_syn_operand);
  731. if use_dotted_functions and
  732. assigned(instr.Operands[1].opr.ref.symbol) then
  733. 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);
  734. end;
  735. if use_dotted_functions and
  736. (instr.Operands[1].opr.typ = OPR_SYMBOL) and
  737. (instr.Operands[1].opr.symbol.typ=AT_FUNCTION) then
  738. instr.Operands[1].opr.symbol:=current_asmdata.DefineAsmSymbol('.'+instr.Operands[1].opr.symbol.name,instr.Operands[1].opr.symbol.bind,AT_FUNCTION,voidcodepointertype);
  739. end;
  740. function tppcattreader.is_targetdirective(const s: string): boolean;
  741. begin
  742. if (target_info.abi=abi_powerpc_elfv2) and
  743. (s='.localentry') then
  744. result:=true
  745. else
  746. result:=inherited;
  747. end;
  748. procedure tppcattreader.HandleTargetDirective;
  749. var
  750. symname,
  751. symval : String;
  752. val : aint;
  753. symtyp : TAsmsymtype;
  754. begin
  755. if (target_info.abi=abi_powerpc_elfv2) and
  756. (actasmpattern='.localentry') then
  757. begin
  758. { .localentry funcname, .-funcname }
  759. consume(AS_TARGET_DIRECTIVE);
  760. BuildConstSymbolExpression(true,false,false, val,symname,symtyp);
  761. Consume(AS_COMMA);
  762. { we need a '.', but these are parsed as identifiers -> if the current
  763. pattern is different from a '.' try to consume AS_DOT so we'll get
  764. the correct error message, otherwise consume this '.' identifier }
  765. if actasmpattern<>'.' then
  766. Consume(AS_DOT)
  767. else
  768. Consume(AS_ID);
  769. Consume(AS_MINUS);
  770. BuildConstSymbolExpression(true,false,false, val,symval,symtyp);
  771. curList.concat(tai_symbolpair.create(spk_localentry,symname,symval));
  772. end
  773. else
  774. inherited;
  775. end;
  776. procedure tppcattreader.handleopcode;
  777. var
  778. instr: tppcinstruction;
  779. begin
  780. instr := TPPCInstruction.Create(TPPCOperand);
  781. BuildOpcode(instr);
  782. instr.condition := actcondition;
  783. if is_calljmp(instr.opcode) then
  784. ConvertCalljmp(instr);
  785. {
  786. instr.AddReferenceSizes;
  787. instr.SetInstructionOpsize;
  788. instr.CheckOperandSizes;
  789. }
  790. instr.ConcatInstruction(curlist);
  791. instr.Free;
  792. end;
  793. {*****************************************************************************
  794. Initialize
  795. *****************************************************************************}
  796. const
  797. asmmode_ppc_att_info: tasmmodeinfo =
  798. (
  799. id: asmmode_ppc_gas;
  800. idtxt: 'GAS';
  801. casmreader: tppcattreader;
  802. );
  803. asmmode_ppc_standard_info: tasmmodeinfo =
  804. (
  805. id: asmmode_standard;
  806. idtxt: 'STANDARD';
  807. casmreader: tppcattreader;
  808. );
  809. initialization
  810. RegisterAsmMode(asmmode_ppc_att_info);
  811. RegisterAsmMode(asmmode_ppc_standard_info);
  812. end.