racpugas.pas 18 KB

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