racpugas.pas 18 KB

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