rappcgas.pas 28 KB

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