rappcgas.pas 25 KB

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