racpugas.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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) then
  150. checklocalsubscript(oper.opr.localsym);
  151. inc(oper.opr.localsymofs,l)
  152. end;
  153. OPR_CONSTANT :
  154. if (mangledname<>'') then
  155. begin
  156. if (oper.opr.val<>0) then
  157. Message(asmr_e_wrong_sym_type);
  158. oper.opr.typ:=OPR_SYMBOL;
  159. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname,AT_FUNCTION);
  160. end
  161. else
  162. inc(oper.opr.val,l);
  163. OPR_REFERENCE :
  164. inc(oper.opr.ref.offset,l);
  165. OPR_SYMBOL:
  166. Message(asmr_e_invalid_symbol_ref);
  167. else
  168. internalerror(200309221);
  169. end;
  170. end;
  171. var
  172. tempreg : tregister;
  173. tempstr : string;
  174. tempsymtyp : TAsmSymType;
  175. hl : tasmlabel;
  176. gotplus,
  177. negative : boolean;
  178. Begin
  179. expr:='';
  180. gotplus:=true;
  181. negative:=false;
  182. repeat
  183. case actasmtoken of
  184. AS_MINUS :
  185. begin
  186. consume(AS_MINUS);
  187. negative:=true;
  188. gotplus:=true;
  189. end;
  190. AS_PLUS :
  191. begin
  192. consume(AS_PLUS);
  193. negative:=false;
  194. gotplus:=true;
  195. end;
  196. AS_INTNUM:
  197. Begin
  198. if not gotplus then
  199. Message(asmr_e_invalid_reference_syntax);
  200. l:=BuildConstExpression(True,False);
  201. if negative then
  202. l:=-l;
  203. case oper.opr.typ of
  204. OPR_NONE:
  205. begin
  206. oper.opr.typ:=OPR_CONSTANT;
  207. oper.opr.val:=l;
  208. end;
  209. OPR_CONSTANT :
  210. inc(oper.opr.val,l);
  211. OPR_REFERENCE:
  212. inc(oper.opr.ref.offset,l);
  213. OPR_LOCAL:
  214. inc(oper.opr.localsymofs,l);
  215. else
  216. InternalError(12345);
  217. end;
  218. GotPlus:=(prevasmtoken=AS_PLUS) or
  219. (prevasmtoken=AS_MINUS);
  220. if GotPlus then
  221. negative:=(prevasmtoken=AS_MINUS);
  222. end;
  223. AS_LPAREN :
  224. begin
  225. consume(AS_LPAREN);
  226. oper.InitRef;
  227. if actasmtoken=AS_REGISTER then
  228. oper.opr.ref.base:=actasmregister;
  229. consume(AS_REGISTER);
  230. consume(AS_RPAREN);
  231. gotplus:=false;
  232. end;
  233. AS_RELTYPE:
  234. begin
  235. { Low or High part of a constant (or constant
  236. memory location) }
  237. oper.InitRef;
  238. oper.opr.ref.refaddr:=actrel;
  239. Consume(actasmtoken);
  240. Consume(AS_LPAREN);
  241. BuildConstSymbolExpression(false, true,false,l,tempstr,tempsymtyp);
  242. if not assigned(oper.opr.ref.symbol) then
  243. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp)
  244. else
  245. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  246. case oper.opr.typ of
  247. OPR_CONSTANT :
  248. inc(oper.opr.val,l);
  249. OPR_LOCAL :
  250. inc(oper.opr.localsymofs,l);
  251. OPR_REFERENCE :
  252. inc(oper.opr.ref.offset,l);
  253. else
  254. internalerror(200309202);
  255. end;
  256. Consume(AS_RPAREN);
  257. gotplus:=false;
  258. end;
  259. AS_ID: { A constant expression, or a Variable ref. }
  260. Begin
  261. { Local Label ? }
  262. if is_locallabel(actasmpattern) then
  263. begin
  264. CreateLocalLabel(actasmpattern,hl,false);
  265. Consume(AS_ID);
  266. AddLabelOperand(hl);
  267. end
  268. else
  269. { Check for label }
  270. if SearchLabel(actasmpattern,hl,false) then
  271. begin
  272. Consume(AS_ID);
  273. AddLabelOperand(hl);
  274. end
  275. else
  276. { probably a variable or normal expression }
  277. { or a procedure (such as in CALL ID) }
  278. Begin
  279. { is it a constant ? }
  280. if SearchIConstant(actasmpattern,l) then
  281. Begin
  282. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  283. Message(asmr_e_invalid_operand_type);
  284. BuildConstantOperand(oper);
  285. end
  286. else
  287. begin
  288. expr:=actasmpattern;
  289. Consume(AS_ID);
  290. { typecasting? }
  291. if (actasmtoken=AS_LPAREN) and
  292. SearchType(expr,typesize) then
  293. begin
  294. oper.hastype:=true;
  295. Consume(AS_LPAREN);
  296. BuildOperand(oper);
  297. Consume(AS_RPAREN);
  298. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  299. oper.SetSize(typesize,true);
  300. end
  301. else
  302. begin
  303. if not oper.SetupVar(expr,false) then
  304. Begin
  305. { look for special symbols ... }
  306. if expr= '__HIGH' then
  307. begin
  308. consume(AS_LPAREN);
  309. if not oper.setupvar('high'+actasmpattern,false) then
  310. Message1(sym_e_unknown_id,'high'+actasmpattern);
  311. consume(AS_ID);
  312. consume(AS_RPAREN);
  313. end
  314. else
  315. if expr = '__RESULT' then
  316. oper.SetUpResult
  317. else
  318. if expr = '__SELF' then
  319. oper.SetupSelf
  320. else
  321. if expr = '__OLDEBP' then
  322. oper.SetupOldEBP
  323. else
  324. Message1(sym_e_unknown_id,expr);
  325. end;
  326. end;
  327. end;
  328. if actasmtoken=AS_DOT then
  329. MaybeRecordOffset;
  330. { add a constant expression? }
  331. if (actasmtoken=AS_PLUS) then
  332. begin
  333. l:=BuildConstExpression(true,false);
  334. case oper.opr.typ of
  335. OPR_CONSTANT :
  336. inc(oper.opr.val,l);
  337. OPR_LOCAL :
  338. inc(oper.opr.localsymofs,l);
  339. OPR_REFERENCE :
  340. inc(oper.opr.ref.offset,l);
  341. else
  342. internalerror(200309202);
  343. end;
  344. end
  345. end;
  346. gotplus:=false;
  347. end;
  348. AS_REGISTER: { Register, a variable reference or a constant reference }
  349. Begin
  350. { save the type of register used. }
  351. tempreg:=actasmregister;
  352. Consume(AS_REGISTER);
  353. begin
  354. if (oper.opr.typ<>OPR_NONE) then
  355. Message(asmr_e_invalid_operand_type);
  356. oper.opr.typ:=OPR_REGISTER;
  357. oper.opr.reg:=tempreg;
  358. end;
  359. gotplus:=false;
  360. end;
  361. AS_END,
  362. AS_SEPARATOR,
  363. AS_COMMA:
  364. break;
  365. else
  366. Begin
  367. Message(asmr_e_syn_operand);
  368. Consume(actasmtoken);
  369. end;
  370. end; { end case }
  371. until false;
  372. end;
  373. {*****************************************************************************
  374. TMipsReader
  375. *****************************************************************************}
  376. procedure TMipsReader.BuildOpCode(instr : TInstruction);
  377. var
  378. operandnum : longint;
  379. Begin
  380. { opcode }
  381. if (actasmtoken<>AS_OPCODE) then
  382. Begin
  383. Message(asmr_e_invalid_or_missing_opcode);
  384. RecoverConsume(true);
  385. exit;
  386. end;
  387. { Fill the instr object with the current state }
  388. with instr do
  389. begin
  390. Opcode:=ActOpcode;
  391. condition:=ActCondition;
  392. end;
  393. { We are reading operands, so opcode will be an AS_ID }
  394. operandnum:=1;
  395. Consume(AS_OPCODE);
  396. { Zero operand opcode ? }
  397. if actasmtoken in [AS_SEPARATOR,AS_END] then
  398. begin
  399. operandnum:=0;
  400. exit;
  401. end;
  402. { Read the operands }
  403. repeat
  404. case actasmtoken of
  405. AS_COMMA: { Operand delimiter }
  406. Begin
  407. if operandnum>Max_Operands then
  408. Message(asmr_e_too_many_operands)
  409. else
  410. begin
  411. { condition operands doesn't set the operand but write to the
  412. condition field of the instruction
  413. }
  414. if instr.Operands[operandnum].opr.typ<>OPR_NONE then
  415. Inc(operandnum);
  416. end;
  417. Consume(AS_COMMA);
  418. end;
  419. AS_SEPARATOR,
  420. AS_END : { End of asm operands for this opcode }
  421. begin
  422. break;
  423. end;
  424. else
  425. BuildOperand(instr.Operands[operandnum] as TOperand);
  426. end; { end case }
  427. until false;
  428. if (operandnum=1) and (instr.Operands[operandnum].opr.typ=OPR_NONE) then
  429. dec(operandnum);
  430. instr.Ops:=operandnum;
  431. end;
  432. function TMipsReader.is_asmopcode(const s: string):boolean;
  433. var
  434. cond:TAsmCond;
  435. hs:string;
  436. Begin
  437. is_asmopcode:=false;
  438. { clear op code and condition }
  439. actopcode:=A_None;
  440. actcondition:=C_None;
  441. { Search opcodes }
  442. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  443. if actopcode<>A_NONE then
  444. begin
  445. actasmtoken:=AS_OPCODE;
  446. result:=TRUE;
  447. exit;
  448. end;
  449. { not found, check branch instructions }
  450. if (Upcase(s[1])='B') then
  451. begin
  452. { we can search here without an extra table which is sorted by string length
  453. because we take the whole remaining string without the leading B }
  454. actopcode := A_BC;
  455. hs:=lower(copy(s,2,maxint));
  456. for cond:=low(TAsmCond) to high(TAsmCond) do
  457. if (hs=Cond2Str[cond]) then
  458. begin
  459. actasmtoken:=AS_OPCODE;
  460. actcondition:=cond;
  461. is_asmopcode:=true;
  462. end;
  463. end;
  464. end;
  465. procedure TMipsReader.handleopcode;
  466. var
  467. instr : TInstruction;
  468. begin
  469. instr:=TInstruction.Create(TOperand);
  470. BuildOpcode(instr);
  471. with instr do
  472. begin
  473. condition := actcondition;
  474. { Coprocessor-related instructions have operands referring to both coprocessor registers
  475. and general-purpose ones. The input representation "$<number>" is the same for both,
  476. but symbolic names must not be used for non-GPRs on output. }
  477. if (opcode in [A_MTC0,A_MFC0,A_CFC1,A_CTC1{,A_CFC2,A_CTC2}]) then
  478. begin
  479. { operands are 1-based here }
  480. if (ops<2) or (operands[2].opr.typ<>OPR_REGISTER) then
  481. message(asmr_e_syn_operand);
  482. operands[2].opr.reg:=newreg(R_SPECIALREGISTER,getsupreg(operands[2].opr.reg),R_SUBNONE);
  483. end;
  484. ConcatInstruction(curlist);
  485. Free;
  486. end;
  487. end;
  488. {*****************************************************************************
  489. Initialize
  490. *****************************************************************************}
  491. const
  492. asmmode_mips_att_info : tasmmodeinfo =
  493. (
  494. id : asmmode_standard;
  495. idtxt : 'GAS';
  496. casmreader : TMipsReader;
  497. );
  498. initialization
  499. RegisterAsmMode(asmmode_mips_att_info);
  500. end.