rappcgas.pas 28 KB

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