rappcgas.pas 25 KB

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