racpugas.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. {
  2. Copyright (c) 1998-2002 by Mazen NEIFER
  3. Does the parsing for the i386 GNU AS styled inline assembler.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. Unit racpugas;
  18. {$i fpcdefs.inc}
  19. Interface
  20. uses
  21. rautils,
  22. raatt;
  23. type
  24. tMipsReader = class(tattreader)
  25. function is_asmopcode(const s: string):boolean;override;
  26. procedure BuildOperand(oper : TOperand);
  27. procedure BuildOpCode(instr : TInstruction);
  28. procedure ReadSym(oper : TOperand);
  29. procedure ConvertCalljmp(instr : TInstruction);
  30. procedure handlepercent;override;
  31. procedure handledollar;override;
  32. procedure handleopcode;override;
  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. scanner,
  47. procinfo,
  48. rabase,
  49. rgbase,
  50. itcpugas,
  51. cgbase,cgobj
  52. ;
  53. procedure TMipsReader.ReadSym(oper : TOperand);
  54. var
  55. tempstr, mangledname : string;
  56. typesize,l,k : aint;
  57. begin
  58. tempstr:=actasmpattern;
  59. Consume(AS_ID);
  60. { typecasting? }
  61. if (actasmtoken=AS_LPAREN) and
  62. SearchType(tempstr,typesize) then
  63. begin
  64. oper.hastype:=true;
  65. Consume(AS_LPAREN);
  66. BuildOperand(oper);
  67. Consume(AS_RPAREN);
  68. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  69. oper.SetSize(typesize,true);
  70. end
  71. else
  72. if not oper.SetupVar(tempstr,false) then
  73. Message1(sym_e_unknown_id,tempstr);
  74. { record.field ? }
  75. if actasmtoken=AS_DOT then
  76. begin
  77. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  78. if (mangledname<>'') then
  79. Message(asmr_e_invalid_reference_syntax);
  80. inc(oper.opr.ref.offset,l);
  81. end;
  82. end;
  83. procedure TMipsReader.handledollar;
  84. begin
  85. Inherited handledollar;
  86. if (c in ['0'..'9','a'..'z']) then
  87. begin
  88. Consume(AS_DOLLAR);
  89. if (actasmtoken=AS_INTNUM) or (actasmtoken=AS_ID) then
  90. begin
  91. { Try to convert to std register }
  92. if actasmtoken=AS_INTNUM then
  93. actasmregister:=gas_regnum_search('$'+actasmpattern)
  94. else
  95. begin
  96. { AS_ID is uppercased by default but register names
  97. are lowercase }
  98. actasmpattern:=lower(actasmpattern);
  99. actasmregister:=gas_regnum_search(actasmpattern);
  100. if actasmregister=NR_NO then
  101. actasmregister:=std_regnum_search(actasmpattern);
  102. end;
  103. if actasmregister<>NR_NO then
  104. begin
  105. // Consume(actasmtoken);
  106. actasmtoken:=AS_REGISTER;
  107. end;
  108. end;
  109. end;
  110. end;
  111. procedure TMipsReader.handlepercent;
  112. var
  113. len : longint;
  114. begin
  115. len:=1;
  116. actasmpattern[len]:='%';
  117. c:=current_scanner.asmgetchar;
  118. { to be a register there must be a letter and not a number }
  119. while c in ['a'..'z','A'..'Z','0'..'9'] do
  120. Begin
  121. inc(len);
  122. actasmpattern[len]:=c;
  123. c:=current_scanner.asmgetchar;
  124. end;
  125. actasmpattern[0]:=chr(len);
  126. uppervar(actasmpattern);
  127. if (actasmpattern='%HI') then
  128. actasmtoken:=AS_HI
  129. else if (actasmpattern='%LO')then
  130. actasmtoken:=AS_LO
  131. else
  132. Message(asmr_e_invalid_reference_syntax);
  133. end;
  134. Procedure TMipsReader.BuildOperand(oper : TOperand);
  135. var
  136. expr : string;
  137. typesize,l : aint;
  138. procedure AddLabelOperand(hl:tasmlabel);
  139. begin
  140. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  141. is_calljmp(actopcode) then
  142. begin
  143. oper.opr.typ:=OPR_SYMBOL;
  144. oper.opr.symbol:=hl;
  145. end
  146. else
  147. begin
  148. oper.InitRef;
  149. oper.opr.ref.symbol:=hl;
  150. end;
  151. end;
  152. procedure MaybeRecordOffset;
  153. var
  154. mangledname: string;
  155. hasdot : boolean;
  156. l,
  157. toffset,
  158. tsize : aint;
  159. begin
  160. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  161. exit;
  162. l:=0;
  163. hasdot:=(actasmtoken=AS_DOT);
  164. if hasdot then
  165. begin
  166. if expr<>'' then
  167. begin
  168. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  169. if (oper.opr.typ<>OPR_CONSTANT) and
  170. (mangledname<>'') then
  171. Message(asmr_e_wrong_sym_type);
  172. inc(l,toffset);
  173. oper.SetSize(tsize,true);
  174. end;
  175. end;
  176. if actasmtoken in [AS_PLUS,AS_MINUS] then
  177. inc(l,BuildConstExpression(true,false));
  178. case oper.opr.typ of
  179. OPR_LOCAL :
  180. begin
  181. { don't allow direct access to fields of parameters, because that
  182. will generate buggy code. Allow it only for explicit typecasting }
  183. if hasdot and
  184. (not oper.hastype) and
  185. (tabstractnormalvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  186. (current_procinfo.procdef.proccalloption<>pocall_register) then
  187. Message(asmr_e_cannot_access_field_directly_for_parameters);
  188. inc(oper.opr.localsymofs,l)
  189. end;
  190. OPR_CONSTANT :
  191. if (mangledname<>'') then
  192. begin
  193. if (oper.opr.val<>0) then
  194. Message(asmr_e_wrong_sym_type);
  195. oper.opr.typ:=OPR_SYMBOL;
  196. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname);
  197. end
  198. else
  199. inc(oper.opr.val,l);
  200. OPR_REFERENCE :
  201. inc(oper.opr.ref.offset,l);
  202. OPR_SYMBOL:
  203. Message(asmr_e_invalid_symbol_ref);
  204. else
  205. internalerror(200309221);
  206. end;
  207. end;
  208. var
  209. tempreg : tregister;
  210. tempstr : string;
  211. tempsymtyp : TAsmSymType;
  212. hl : tasmlabel;
  213. gotplus,
  214. negative : boolean;
  215. Begin
  216. expr:='';
  217. gotplus:=true;
  218. negative:=false;
  219. repeat
  220. case actasmtoken of
  221. AS_MINUS :
  222. begin
  223. consume(AS_MINUS);
  224. negative:=true;
  225. gotplus:=true;
  226. end;
  227. AS_PLUS :
  228. begin
  229. consume(AS_PLUS);
  230. negative:=false;
  231. gotplus:=true;
  232. end;
  233. AS_INTNUM,
  234. AS_MOD:
  235. Begin
  236. if not gotplus then
  237. Message(asmr_e_invalid_reference_syntax);
  238. l:=BuildConstExpression(True,False);
  239. if negative then
  240. l:=-l;
  241. { Constant memory offset }
  242. oper.InitRef;
  243. oper.opr.ref.refaddr:=addr_full;
  244. oper.opr.ref.offset:=l;
  245. GotPlus:=(prevasmtoken=AS_PLUS) or
  246. (prevasmtoken=AS_MINUS);
  247. if GotPlus then
  248. negative:=(prevasmtoken=AS_MINUS);
  249. end;
  250. AS_LPAREN :
  251. begin
  252. consume(AS_LPAREN);
  253. oper.InitRef;
  254. if actasmtoken=AS_REGISTER then
  255. oper.opr.ref.base:=actasmregister;
  256. consume(AS_REGISTER);
  257. consume(AS_RPAREN);
  258. gotplus:=false;
  259. end;
  260. AS_HI,
  261. AS_LO:
  262. begin
  263. { Low or High part of a constant (or constant
  264. memory location) }
  265. oper.InitRef;
  266. if actasmtoken=AS_LO then
  267. oper.opr.ref.refaddr:=addr_low
  268. else
  269. oper.opr.ref.refaddr:=addr_high;
  270. Consume(actasmtoken);
  271. Consume(AS_LPAREN);
  272. BuildConstSymbolExpression(false, true,false,l,tempstr,tempsymtyp);
  273. if not assigned(oper.opr.ref.symbol) then
  274. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr)
  275. else
  276. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  277. case oper.opr.typ of
  278. OPR_CONSTANT :
  279. inc(oper.opr.val,l);
  280. OPR_LOCAL :
  281. inc(oper.opr.localsymofs,l);
  282. OPR_REFERENCE :
  283. inc(oper.opr.ref.offset,l);
  284. else
  285. internalerror(200309202);
  286. end;
  287. Consume(AS_RPAREN);
  288. gotplus:=false;
  289. end;
  290. AS_ID: { A constant expression, or a Variable ref. }
  291. Begin
  292. { Local Label ? }
  293. if is_locallabel(actasmpattern) then
  294. begin
  295. CreateLocalLabel(actasmpattern,hl,false);
  296. Consume(AS_ID);
  297. AddLabelOperand(hl);
  298. end
  299. else
  300. { Check for label }
  301. if SearchLabel(actasmpattern,hl,false) then
  302. begin
  303. Consume(AS_ID);
  304. AddLabelOperand(hl);
  305. end
  306. else
  307. { probably a variable or normal expression }
  308. { or a procedure (such as in CALL ID) }
  309. Begin
  310. { is it a constant ? }
  311. if SearchIConstant(actasmpattern,l) then
  312. Begin
  313. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  314. Message(asmr_e_invalid_operand_type);
  315. BuildConstantOperand(oper);
  316. end
  317. else
  318. begin
  319. expr:=actasmpattern;
  320. Consume(AS_ID);
  321. { typecasting? }
  322. if (actasmtoken=AS_LPAREN) and
  323. SearchType(expr,typesize) then
  324. begin
  325. oper.hastype:=true;
  326. Consume(AS_LPAREN);
  327. BuildOperand(oper);
  328. Consume(AS_RPAREN);
  329. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  330. oper.SetSize(typesize,true);
  331. end
  332. else
  333. begin
  334. if not oper.SetupVar(expr,false) then
  335. Begin
  336. { look for special symbols ... }
  337. if expr= '__HIGH' then
  338. begin
  339. consume(AS_LPAREN);
  340. if not oper.setupvar('high'+actasmpattern,false) then
  341. Message1(sym_e_unknown_id,'high'+actasmpattern);
  342. consume(AS_ID);
  343. consume(AS_RPAREN);
  344. end
  345. else
  346. if expr = '__RESULT' then
  347. oper.SetUpResult
  348. else
  349. if expr = '__SELF' then
  350. oper.SetupSelf
  351. else
  352. if expr = '__OLDEBP' then
  353. oper.SetupOldEBP
  354. else
  355. Message1(sym_e_unknown_id,expr);
  356. end;
  357. end;
  358. end;
  359. if actasmtoken=AS_DOT then
  360. MaybeRecordOffset;
  361. { add a constant expression? }
  362. if (actasmtoken=AS_PLUS) then
  363. begin
  364. l:=BuildConstExpression(true,false);
  365. case oper.opr.typ of
  366. OPR_CONSTANT :
  367. inc(oper.opr.val,l);
  368. OPR_LOCAL :
  369. inc(oper.opr.localsymofs,l);
  370. OPR_REFERENCE :
  371. inc(oper.opr.ref.offset,l);
  372. else
  373. internalerror(200309202);
  374. end;
  375. end
  376. end;
  377. gotplus:=false;
  378. end;
  379. AS_REGISTER: { Register, a variable reference or a constant reference }
  380. Begin
  381. { save the type of register used. }
  382. tempreg:=actasmregister;
  383. Consume(AS_REGISTER);
  384. begin
  385. if (oper.opr.typ<>OPR_NONE) then
  386. Message(asmr_e_invalid_operand_type);
  387. oper.opr.typ:=OPR_REGISTER;
  388. oper.opr.reg:=tempreg;
  389. end;
  390. gotplus:=false;
  391. end;
  392. AS_END,
  393. AS_SEPARATOR,
  394. AS_COMMA:
  395. break;
  396. else
  397. Begin
  398. Message(asmr_e_syn_operand);
  399. Consume(actasmtoken);
  400. end;
  401. end; { end case }
  402. until false;
  403. end;
  404. {*****************************************************************************
  405. TMipsReader
  406. *****************************************************************************}
  407. procedure TMipsReader.BuildOpCode(instr : TInstruction);
  408. var
  409. operandnum : longint;
  410. Begin
  411. { opcode }
  412. if (actasmtoken<>AS_OPCODE) then
  413. Begin
  414. Message(asmr_e_invalid_or_missing_opcode);
  415. RecoverConsume(true);
  416. exit;
  417. end;
  418. { Fill the instr object with the current state }
  419. with instr do
  420. begin
  421. Opcode:=ActOpcode;
  422. condition:=ActCondition;
  423. end;
  424. { We are reading operands, so opcode will be an AS_ID }
  425. operandnum:=1;
  426. Consume(AS_OPCODE);
  427. { Zero operand opcode ? }
  428. if actasmtoken in [AS_SEPARATOR,AS_END] then
  429. begin
  430. operandnum:=0;
  431. exit;
  432. end;
  433. { Read the operands }
  434. repeat
  435. case actasmtoken of
  436. AS_COMMA: { Operand delimiter }
  437. Begin
  438. if operandnum>Max_Operands then
  439. Message(asmr_e_too_many_operands)
  440. else
  441. begin
  442. { condition operands doesn't set the operand but write to the
  443. condition field of the instruction
  444. }
  445. if instr.Operands[operandnum].opr.typ<>OPR_NONE then
  446. Inc(operandnum);
  447. end;
  448. Consume(AS_COMMA);
  449. end;
  450. AS_SEPARATOR,
  451. AS_END : { End of asm operands for this opcode }
  452. begin
  453. break;
  454. end;
  455. else
  456. BuildOperand(instr.Operands[operandnum] as TOperand);
  457. end; { end case }
  458. until false;
  459. if (operandnum=1) and (instr.Operands[operandnum].opr.typ=OPR_NONE) then
  460. dec(operandnum);
  461. instr.Ops:=operandnum;
  462. end;
  463. function TMipsReader.is_asmopcode(const s: string):boolean;
  464. var
  465. cond:TAsmCond;
  466. Begin
  467. { making s a value parameter would break other assembler readers }
  468. is_asmopcode:=false;
  469. { clear op code }
  470. actopcode:=A_None;
  471. { clear condition }
  472. fillchar(actcondition,sizeof(actcondition),0);
  473. { Search opcodes }
  474. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  475. if actopcode<>A_NONE then
  476. begin
  477. actasmtoken:=AS_OPCODE;
  478. result:=TRUE;
  479. exit;
  480. end;
  481. { not found, check branch instructions }
  482. if (Upcase(s[1])='B') then
  483. begin
  484. { we can search here without an extra table which is sorted by string length
  485. because we take the whole remaining string without the leading B }
  486. actopcode := A_BC;
  487. for cond:=low(TAsmCond) to high(TAsmCond) do
  488. if (Upper(copy(s,2,length(s)-1))=Upper(Cond2Str[cond])) then
  489. begin
  490. actasmtoken:=AS_OPCODE;
  491. actcondition:=cond;
  492. is_asmopcode:=true;
  493. end;
  494. end;
  495. end;
  496. procedure TMipsReader.ConvertCalljmp(instr : TInstruction);
  497. var
  498. newopr : toprrec;
  499. begin
  500. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  501. with newopr do
  502. begin
  503. typ:=OPR_SYMBOL;
  504. symbol:=instr.Operands[1].opr.ref.symbol;
  505. symofs:=instr.Operands[1].opr.ref.offset;
  506. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  507. (instr.Operands[1].opr.ref.index<>NR_NO) or
  508. (instr.Operands[1].opr.ref.refaddr<>addr_full) then
  509. Message(asmr_e_syn_operand);
  510. instr.Operands[1].opr:=newopr;
  511. end;
  512. end;
  513. procedure TMipsReader.handleopcode;
  514. var
  515. instr : TInstruction;
  516. begin
  517. instr:=TInstruction.Create(TOperand);
  518. BuildOpcode(instr);
  519. with instr do
  520. begin
  521. condition := actcondition;
  522. if is_calljmp(opcode) then
  523. ConvertCalljmp(instr);
  524. { Coprocessor-related instructions have operands referring to both coprocessor registers
  525. and general-purpose ones. The input representation "$<number>" is the same for both,
  526. but symbolic names must not be used for non-GPRs on output. }
  527. if (opcode in [A_MTC0,A_MFC0,A_CFC1,A_CTC1{,A_CFC2,A_CTC2}]) then
  528. begin
  529. { operands are 1-based here }
  530. if (ops<2) or (operands[2].opr.typ<>OPR_REGISTER) then
  531. message(asmr_e_syn_operand);
  532. operands[2].opr.reg:=newreg(R_SPECIALREGISTER,getsupreg(operands[2].opr.reg),R_SUBNONE);
  533. end;
  534. ConcatInstruction(curlist);
  535. Free;
  536. end;
  537. end;
  538. {*****************************************************************************
  539. Initialize
  540. *****************************************************************************}
  541. const
  542. asmmode_mips_att_info : tasmmodeinfo =
  543. (
  544. id : asmmode_standard;
  545. idtxt : 'GAS';
  546. casmreader : TMipsReader;
  547. );
  548. initialization
  549. RegisterAsmMode(asmmode_mips_att_info);
  550. end.