rappcgas.pas 28 KB

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