racpugas.pas 18 KB

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