rappcgas.pas 24 KB

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