racpugas.pas 18 KB

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