rappcgas.pas 25 KB

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