racpugas.pas 18 KB

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