racpugas.pas 18 KB

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