rappcgas.pas 28 KB

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