raarmgas.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing for the ARM 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 raarmgas;
  18. {$i fpcdefs.inc}
  19. Interface
  20. uses
  21. raatt,raarm,
  22. cpubase;
  23. type
  24. tarmattreader = class(tattreader)
  25. actoppostfix : TOpPostfix;
  26. actwideformat : boolean;
  27. function is_asmopcode(const s: string):boolean;override;
  28. function is_register(const s:string):boolean;override;
  29. procedure handleopcode;override;
  30. procedure BuildReference(oper : tarmoperand);
  31. procedure BuildOperand(oper : tarmoperand);
  32. function TryBuildShifterOp(oper : tarmoperand) : boolean;
  33. procedure BuildOpCode(instr : tarminstruction);
  34. procedure ReadSym(oper : tarmoperand);
  35. procedure ConvertCalljmp(instr : tarminstruction);
  36. end;
  37. Implementation
  38. uses
  39. { helpers }
  40. cutils,
  41. { global }
  42. globtype,globals,verbose,
  43. systems,
  44. { aasm }
  45. cpuinfo,aasmbase,aasmtai,aasmdata,aasmcpu,
  46. { symtable }
  47. symconst,symbase,symtype,symsym,symtable,
  48. { parser }
  49. scanner,
  50. procinfo,
  51. itcpugas,
  52. rabase,rautils,
  53. cgbase,cgutils,cgobj
  54. ;
  55. function tarmattreader.is_register(const s:string):boolean;
  56. type
  57. treg2str = record
  58. name : string[2];
  59. reg : tregister;
  60. end;
  61. const
  62. extraregs : array[0..19] of treg2str = (
  63. (name: 'A1'; reg : NR_R0),
  64. (name: 'A2'; reg : NR_R1),
  65. (name: 'A3'; reg : NR_R2),
  66. (name: 'A4'; reg : NR_R3),
  67. (name: 'V1'; reg : NR_R4),
  68. (name: 'V2'; reg : NR_R5),
  69. (name: 'V3'; reg : NR_R6),
  70. (name: 'V4'; reg : NR_R7),
  71. (name: 'V5'; reg : NR_R8),
  72. (name: 'V6'; reg : NR_R9),
  73. (name: 'V7'; reg : NR_R10),
  74. (name: 'V8'; reg : NR_R11),
  75. (name: 'WR'; reg : NR_R7),
  76. (name: 'SB'; reg : NR_R9),
  77. (name: 'SL'; reg : NR_R10),
  78. (name: 'FP'; reg : NR_R11),
  79. (name: 'IP'; reg : NR_R12),
  80. (name: 'SP'; reg : NR_R13),
  81. (name: 'LR'; reg : NR_R14),
  82. (name: 'PC'; reg : NR_R15));
  83. var
  84. i : longint;
  85. begin
  86. result:=inherited is_register(s);
  87. { reg found?
  88. possible aliases are always 2 char
  89. }
  90. if result or (length(s)<>2) then
  91. exit;
  92. for i:=low(extraregs) to high(extraregs) do
  93. begin
  94. if s=extraregs[i].name then
  95. begin
  96. actasmregister:=extraregs[i].reg;
  97. result:=true;
  98. actasmtoken:=AS_REGISTER;
  99. exit;
  100. end;
  101. end;
  102. end;
  103. procedure tarmattreader.ReadSym(oper : tarmoperand);
  104. var
  105. tempstr, mangledname : string;
  106. typesize,l,k : longint;
  107. begin
  108. tempstr:=actasmpattern;
  109. Consume(AS_ID);
  110. { typecasting? }
  111. if (actasmtoken=AS_LPAREN) and
  112. SearchType(tempstr,typesize) then
  113. begin
  114. oper.hastype:=true;
  115. Consume(AS_LPAREN);
  116. BuildOperand(oper);
  117. Consume(AS_RPAREN);
  118. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  119. oper.SetSize(typesize,true);
  120. end
  121. else
  122. if not oper.SetupVar(tempstr,false) then
  123. Message1(sym_e_unknown_id,tempstr);
  124. { record.field ? }
  125. if actasmtoken=AS_DOT then
  126. begin
  127. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  128. if (mangledname<>'') then
  129. Message(asmr_e_invalid_reference_syntax);
  130. inc(oper.opr.ref.offset,l);
  131. end;
  132. end;
  133. Procedure tarmattreader.BuildReference(oper : tarmoperand);
  134. procedure do_error;
  135. begin
  136. Message(asmr_e_invalid_reference_syntax);
  137. RecoverConsume(false);
  138. end;
  139. procedure test_end(require_rbracket : boolean);
  140. begin
  141. if require_rbracket then begin
  142. if not(actasmtoken=AS_RBRACKET) then
  143. begin
  144. do_error;
  145. exit;
  146. end
  147. else
  148. Consume(AS_RBRACKET);
  149. if (actasmtoken=AS_NOT) then
  150. begin
  151. oper.opr.ref.addressmode:=AM_PREINDEXED;
  152. Consume(AS_NOT);
  153. end;
  154. end;
  155. if not(actasmtoken in [AS_SEPARATOR,AS_end]) then
  156. do_error
  157. else
  158. begin
  159. {$IFDEF debugasmreader}
  160. writeln('TEST_end_FINAL_OK. Created the following ref:');
  161. writeln('oper.opr.ref.shiftimm=',oper.opr.ref.shiftimm);
  162. writeln('oper.opr.ref.shiftmode=',ord(oper.opr.ref.shiftmode));
  163. writeln('oper.opr.ref.index=',ord(oper.opr.ref.index));
  164. writeln('oper.opr.ref.base=',ord(oper.opr.ref.base));
  165. writeln('oper.opr.ref.signindex=',ord(oper.opr.ref.signindex));
  166. writeln('oper.opr.ref.addressmode=',ord(oper.opr.ref.addressmode));
  167. writeln;
  168. {$endIF debugasmreader}
  169. end;
  170. end;
  171. function is_shifter_ref_operation(var a : tshiftmode) : boolean;
  172. begin
  173. a := SM_NONE;
  174. if (actasmpattern='LSL') then
  175. a := SM_LSL
  176. else if (actasmpattern='LSR') then
  177. a := SM_LSR
  178. else if (actasmpattern='ASR') then
  179. a := SM_ASR
  180. else if (actasmpattern='ROR') then
  181. a := SM_ROR
  182. else if (actasmpattern='RRX') then
  183. a := SM_RRX;
  184. is_shifter_ref_operation := not(a=SM_NONE);
  185. end;
  186. procedure read_index_shift(require_rbracket : boolean);
  187. var
  188. shift : aint;
  189. begin
  190. case actasmtoken of
  191. AS_COMMA :
  192. begin
  193. Consume(AS_COMMA);
  194. if not(actasmtoken=AS_ID) then
  195. do_error;
  196. if is_shifter_ref_operation(oper.opr.ref.shiftmode) then
  197. begin
  198. Consume(AS_ID);
  199. if not(oper.opr.ref.shiftmode=SM_RRX) then
  200. begin
  201. if not(actasmtoken=AS_HASH) then
  202. do_error;
  203. Consume(AS_HASH);
  204. shift := BuildConstExpression(false,true);
  205. if (shift<0) or (shift>32) then
  206. do_error;
  207. oper.opr.ref.shiftimm := shift;
  208. test_end(require_rbracket);
  209. end;
  210. end
  211. else
  212. begin
  213. do_error;
  214. exit;
  215. end;
  216. end;
  217. AS_RBRACKET :
  218. if require_rbracket then
  219. test_end(require_rbracket)
  220. else
  221. begin
  222. do_error;
  223. exit;
  224. end;
  225. AS_SEPARATOR,AS_END :
  226. if not require_rbracket then
  227. test_end(false)
  228. else
  229. do_error;
  230. else
  231. begin
  232. do_error;
  233. exit;
  234. end;
  235. end;
  236. end;
  237. procedure read_index(require_rbracket : boolean);
  238. var
  239. recname : string;
  240. o_int,s_int : aint;
  241. begin
  242. case actasmtoken of
  243. AS_REGISTER :
  244. begin
  245. oper.opr.ref.index:=actasmregister;
  246. Consume(AS_REGISTER);
  247. read_index_shift(require_rbracket);
  248. exit;
  249. end;
  250. AS_PLUS,AS_MINUS :
  251. begin
  252. if actasmtoken=AS_PLUS then
  253. begin
  254. Consume(AS_PLUS);
  255. end
  256. else
  257. begin
  258. oper.opr.ref.signindex := -1;
  259. Consume(AS_MINUS);
  260. end;
  261. if actasmtoken=AS_REGISTER then
  262. begin
  263. oper.opr.ref.index:=actasmregister;
  264. Consume(AS_REGISTER);
  265. read_index_shift(require_rbracket);
  266. exit;
  267. end
  268. else
  269. begin
  270. do_error;
  271. exit;
  272. end;
  273. test_end(require_rbracket);
  274. exit;
  275. end;
  276. AS_HASH : // constant
  277. begin
  278. Consume(AS_HASH);
  279. o_int := BuildConstExpression(false,true);
  280. if (o_int>4095) or (o_int<-4095) then
  281. begin
  282. Message(asmr_e_constant_out_of_bounds);
  283. RecoverConsume(false);
  284. exit;
  285. end
  286. else
  287. begin
  288. inc(oper.opr.ref.offset,o_int);
  289. test_end(require_rbracket);
  290. exit;
  291. end;
  292. end;
  293. AS_ID :
  294. begin
  295. recname := actasmpattern;
  296. Consume(AS_ID);
  297. BuildRecordOffsetSize(recname,o_int,s_int,recname,false);
  298. if (o_int>4095)or(o_int<-4095) then
  299. begin
  300. Message(asmr_e_constant_out_of_bounds);
  301. RecoverConsume(false);
  302. exit;
  303. end
  304. else
  305. begin
  306. inc(oper.opr.ref.offset,o_int);
  307. test_end(require_rbracket);
  308. exit;
  309. end;
  310. end;
  311. AS_AT:
  312. begin
  313. do_error;
  314. exit;
  315. end;
  316. AS_DOT : // local label
  317. begin
  318. oper.opr.ref.signindex := BuildConstExpression(true,false);
  319. test_end(require_rbracket);
  320. exit;
  321. end;
  322. AS_RBRACKET :
  323. begin
  324. if require_rbracket then
  325. begin
  326. test_end(require_rbracket);
  327. exit;
  328. end
  329. else
  330. begin
  331. do_error; // unexpected rbracket
  332. exit;
  333. end;
  334. end;
  335. AS_SEPARATOR,AS_end :
  336. begin
  337. if not require_rbracket then
  338. begin
  339. test_end(false);
  340. exit;
  341. end
  342. else
  343. begin
  344. do_error;
  345. exit;
  346. end;
  347. end;
  348. else
  349. begin
  350. // unexpected token
  351. do_error;
  352. exit;
  353. end;
  354. end; // case
  355. end;
  356. procedure try_prepostindexed;
  357. begin
  358. Consume(AS_RBRACKET);
  359. case actasmtoken of
  360. AS_COMMA :
  361. begin // post-indexed
  362. Consume(AS_COMMA);
  363. oper.opr.ref.addressmode:=AM_POSTINDEXED;
  364. read_index(false);
  365. exit;
  366. end;
  367. AS_NOT :
  368. begin // pre-indexed
  369. Consume(AS_NOT);
  370. oper.opr.ref.addressmode:=AM_PREINDEXED;
  371. test_end(false);
  372. exit;
  373. end;
  374. else
  375. begin
  376. test_end(false);
  377. exit;
  378. end;
  379. end; // case
  380. end;
  381. var
  382. lab : TASMLABEL;
  383. begin
  384. Consume(AS_LBRACKET);
  385. oper.opr.ref.addressmode:=AM_OFFSET; // assume "neither PRE nor POST inc"
  386. if actasmtoken=AS_REGISTER then
  387. begin
  388. oper.opr.ref.base:=actasmregister;
  389. Consume(AS_REGISTER);
  390. case actasmtoken of
  391. AS_RBRACKET :
  392. begin
  393. try_prepostindexed;
  394. exit;
  395. end;
  396. AS_COMMA :
  397. begin
  398. Consume(AS_COMMA);
  399. read_index(true);
  400. exit;
  401. end;
  402. else
  403. begin
  404. Message(asmr_e_invalid_reference_syntax);
  405. RecoverConsume(false);
  406. end;
  407. end;
  408. end
  409. else
  410. {
  411. if base isn't a register, r15=PC is implied base, so it must be a local label.
  412. pascal constants don't make sense, because implied r15
  413. record offsets probably don't make sense, too (a record offset of code?)
  414. TODO: However, we could make the Stackpointer implied.
  415. }
  416. Begin
  417. case actasmtoken of
  418. AS_ID :
  419. begin
  420. if is_locallabel(actasmpattern) then
  421. begin
  422. CreateLocalLabel(actasmpattern,lab,false);
  423. oper.opr.ref.symbol := lab;
  424. oper.opr.ref.base := NR_PC;
  425. Consume(AS_ID);
  426. test_end(true);
  427. exit;
  428. end
  429. else
  430. begin
  431. // TODO: Stackpointer implied,
  432. Message(asmr_e_invalid_reference_syntax);
  433. RecoverConsume(false);
  434. exit;
  435. end;
  436. end;
  437. else
  438. begin // elsecase
  439. Message(asmr_e_invalid_reference_syntax);
  440. RecoverConsume(false);
  441. exit;
  442. end;
  443. end;
  444. end;
  445. end;
  446. function tarmattreader.TryBuildShifterOp(oper : tarmoperand) : boolean;
  447. procedure handlepara(sm : tshiftmode);
  448. begin
  449. consume(AS_ID);
  450. fillchar(oper.opr,sizeof(oper.opr),0);
  451. oper.opr.typ:=OPR_SHIFTEROP;
  452. oper.opr.shifterop.shiftmode:=sm;
  453. if sm<>SM_RRX then
  454. begin
  455. case actasmtoken of
  456. AS_REGISTER:
  457. begin
  458. oper.opr.shifterop.rs:=actasmregister;
  459. consume(AS_REGISTER);
  460. end;
  461. AS_HASH:
  462. begin
  463. consume(AS_HASH);
  464. oper.opr.shifterop.shiftimm:=BuildConstExpression(false,false);
  465. end;
  466. else
  467. Message(asmr_e_illegal_shifterop_syntax);
  468. end;
  469. end;
  470. end;
  471. begin
  472. result:=true;
  473. if (actasmtoken=AS_ID) then
  474. begin
  475. if (actasmpattern='LSL') then
  476. handlepara(SM_LSL)
  477. else if (actasmpattern='LSR') then
  478. handlepara(SM_LSR)
  479. else if (actasmpattern='ASR') then
  480. handlepara(SM_ASR)
  481. else if (actasmpattern='ROR') then
  482. handlepara(SM_ROR)
  483. else if (actasmpattern='RRX') then
  484. handlepara(SM_ROR)
  485. else
  486. result:=false;
  487. end
  488. else
  489. result:=false;
  490. end;
  491. Procedure tarmattreader.BuildOperand(oper : tarmoperand);
  492. var
  493. expr : string;
  494. typesize,l : longint;
  495. procedure AddLabelOperand(hl:tasmlabel);
  496. begin
  497. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  498. is_calljmp(actopcode) then
  499. begin
  500. oper.opr.typ:=OPR_SYMBOL;
  501. oper.opr.symbol:=hl;
  502. end
  503. else
  504. begin
  505. oper.InitRef;
  506. oper.opr.ref.symbol:=hl;
  507. oper.opr.ref.base:=NR_PC;
  508. end;
  509. end;
  510. procedure MaybeRecordOffset;
  511. var
  512. mangledname: string;
  513. hasdot : boolean;
  514. l,
  515. toffset,
  516. tsize : longint;
  517. begin
  518. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  519. exit;
  520. l:=0;
  521. hasdot:=(actasmtoken=AS_DOT);
  522. if hasdot then
  523. begin
  524. if expr<>'' then
  525. begin
  526. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  527. if (oper.opr.typ<>OPR_CONSTANT) and
  528. (mangledname<>'') then
  529. Message(asmr_e_wrong_sym_type);
  530. inc(l,toffset);
  531. oper.SetSize(tsize,true);
  532. end;
  533. end;
  534. if actasmtoken in [AS_PLUS,AS_MINUS] then
  535. inc(l,BuildConstExpression(true,false));
  536. case oper.opr.typ of
  537. OPR_LOCAL :
  538. begin
  539. { don't allow direct access to fields of parameters, because that
  540. will generate buggy code. Allow it only for explicit typecasting }
  541. if hasdot and
  542. (not oper.hastype) and
  543. (tabstractnormalvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  544. (current_procinfo.procdef.proccalloption<>pocall_register) then
  545. Message(asmr_e_cannot_access_field_directly_for_parameters);
  546. inc(oper.opr.localsymofs,l)
  547. end;
  548. OPR_CONSTANT :
  549. inc(oper.opr.val,l);
  550. OPR_REFERENCE :
  551. if (mangledname<>'') then
  552. begin
  553. if (oper.opr.val<>0) then
  554. Message(asmr_e_wrong_sym_type);
  555. oper.opr.typ:=OPR_SYMBOL;
  556. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname);
  557. end
  558. else
  559. inc(oper.opr.val,l);
  560. OPR_SYMBOL:
  561. Message(asmr_e_invalid_symbol_ref);
  562. else
  563. internalerror(200309221);
  564. end;
  565. end;
  566. function MaybeBuildReference:boolean;
  567. { Try to create a reference, if not a reference is found then false
  568. is returned }
  569. begin
  570. MaybeBuildReference:=true;
  571. case actasmtoken of
  572. AS_INTNUM,
  573. AS_MINUS,
  574. AS_PLUS:
  575. Begin
  576. oper.opr.ref.offset:=BuildConstExpression(True,False);
  577. if actasmtoken<>AS_LPAREN then
  578. Message(asmr_e_invalid_reference_syntax)
  579. else
  580. BuildReference(oper);
  581. end;
  582. AS_LPAREN:
  583. BuildReference(oper);
  584. AS_ID: { only a variable is allowed ... }
  585. Begin
  586. ReadSym(oper);
  587. case actasmtoken of
  588. AS_end,
  589. AS_SEPARATOR,
  590. AS_COMMA: ;
  591. AS_LPAREN:
  592. BuildReference(oper);
  593. else
  594. Begin
  595. Message(asmr_e_invalid_reference_syntax);
  596. Consume(actasmtoken);
  597. end;
  598. end; {end case }
  599. end;
  600. else
  601. MaybeBuildReference:=false;
  602. end; { end case }
  603. end;
  604. function is_ConditionCode(hs: string): boolean;
  605. var icond: tasmcond;
  606. begin
  607. is_ConditionCode := false;
  608. case actopcode of
  609. A_IT,A_ITE,A_ITT,
  610. A_ITEE,A_ITTE,A_ITET,A_ITTT,
  611. A_ITEEE,A_ITTEE,A_ITETE,A_ITTTE,A_ITEET,A_ITTET,A_ITETT,A_ITTTT:
  612. begin
  613. { search for condition, conditions are always 2 chars }
  614. if length(hs)>1 then
  615. begin
  616. for icond:=low(tasmcond) to high(tasmcond) do
  617. begin
  618. if copy(hs,1,2)=uppercond2str[icond] then
  619. begin
  620. //actcondition:=icond;
  621. oper.opr.typ := OPR_COND;
  622. oper.opr.cc := icond;
  623. exit(true);
  624. end;
  625. end;
  626. end;
  627. end;
  628. end;
  629. end;
  630. function is_modeflag(hs : string): boolean;
  631. var
  632. i: longint;
  633. flags: tcpumodeflags;
  634. begin
  635. is_modeflag := false;
  636. flags:=[];
  637. hs:=lower(hs);
  638. if (actopcode in [A_CPSID,A_CPSIE]) and (length(hs) >= 1) then
  639. begin
  640. for i:=1 to length(hs) do
  641. begin
  642. case hs[i] of
  643. 'a':
  644. Include(flags,mfA);
  645. 'f':
  646. Include(flags,mfF);
  647. 'i':
  648. Include(flags,mfI);
  649. else
  650. exit;
  651. end;
  652. end;
  653. oper.opr.typ := OPR_MODEFLAGS;
  654. oper.opr.flags := flags;
  655. exit(true);
  656. end;
  657. end;
  658. var
  659. tempreg : tregister;
  660. ireg : tsuperregister;
  661. regtype: tregistertype;
  662. subreg: tsubregister;
  663. hl : tasmlabel;
  664. {ofs : longint;}
  665. registerset : tcpuregisterset;
  666. Begin
  667. expr:='';
  668. case actasmtoken of
  669. AS_LBRACKET: { Memory reference or constant expression }
  670. Begin
  671. oper.InitRef;
  672. BuildReference(oper);
  673. end;
  674. AS_HASH: { Constant expression }
  675. Begin
  676. Consume(AS_HASH);
  677. BuildConstantOperand(oper);
  678. end;
  679. (*
  680. AS_INTNUM,
  681. AS_MINUS,
  682. AS_PLUS:
  683. Begin
  684. { Constant memory offset }
  685. { This must absolutely be followed by ( }
  686. oper.InitRef;
  687. oper.opr.ref.offset:=BuildConstExpression(True,False);
  688. if actasmtoken<>AS_LPAREN then
  689. begin
  690. ofs:=oper.opr.ref.offset;
  691. BuildConstantOperand(oper);
  692. inc(oper.opr.val,ofs);
  693. end
  694. else
  695. BuildReference(oper);
  696. end;
  697. *)
  698. AS_ID: { A constant expression, or a Variable ref. }
  699. Begin
  700. if is_modeflag(actasmpattern) then
  701. begin
  702. consume(AS_ID);
  703. end
  704. else
  705. { Condition code? }
  706. if is_conditioncode(actasmpattern) then
  707. begin
  708. consume(AS_ID);
  709. end
  710. else
  711. { Local Label ? }
  712. if is_locallabel(actasmpattern) then
  713. begin
  714. CreateLocalLabel(actasmpattern,hl,false);
  715. Consume(AS_ID);
  716. AddLabelOperand(hl);
  717. end
  718. else
  719. { Check for label }
  720. if SearchLabel(actasmpattern,hl,false) then
  721. begin
  722. Consume(AS_ID);
  723. AddLabelOperand(hl);
  724. end
  725. else
  726. { probably a variable or normal expression }
  727. { or a procedure (such as in CALL ID) }
  728. Begin
  729. { is it a constant ? }
  730. if SearchIConstant(actasmpattern,l) then
  731. Begin
  732. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  733. Message(asmr_e_invalid_operand_type);
  734. BuildConstantOperand(oper);
  735. end
  736. else
  737. begin
  738. expr:=actasmpattern;
  739. Consume(AS_ID);
  740. { typecasting? }
  741. if (actasmtoken=AS_LPAREN) and
  742. SearchType(expr,typesize) then
  743. begin
  744. oper.hastype:=true;
  745. Consume(AS_LPAREN);
  746. BuildOperand(oper);
  747. Consume(AS_RPAREN);
  748. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  749. oper.SetSize(typesize,true);
  750. end
  751. else
  752. begin
  753. if not(oper.SetupVar(expr,false)) then
  754. Begin
  755. { look for special symbols ... }
  756. if expr= '__HIGH' then
  757. begin
  758. consume(AS_LPAREN);
  759. if not oper.setupvar('high'+actasmpattern,false) then
  760. Message1(sym_e_unknown_id,'high'+actasmpattern);
  761. consume(AS_ID);
  762. consume(AS_RPAREN);
  763. end
  764. else
  765. if expr = '__RESULT' then
  766. oper.SetUpResult
  767. else
  768. if expr = '__SELF' then
  769. oper.SetupSelf
  770. else
  771. if expr = '__OLDEBP' then
  772. oper.SetupOldEBP
  773. else
  774. Message1(sym_e_unknown_id,expr);
  775. end;
  776. end;
  777. end;
  778. if actasmtoken=AS_DOT then
  779. MaybeRecordOffset;
  780. { add a constant expression? }
  781. if (actasmtoken=AS_PLUS) then
  782. begin
  783. l:=BuildConstExpression(true,false);
  784. case oper.opr.typ of
  785. OPR_CONSTANT :
  786. inc(oper.opr.val,l);
  787. OPR_LOCAL :
  788. inc(oper.opr.localsymofs,l);
  789. OPR_REFERENCE :
  790. inc(oper.opr.ref.offset,l);
  791. else
  792. internalerror(200309202);
  793. end;
  794. end
  795. end;
  796. { Do we have a indexing reference, then parse it also }
  797. if actasmtoken=AS_LPAREN then
  798. BuildReference(oper);
  799. end;
  800. { Register, a variable reference or a constant reference }
  801. AS_REGISTER:
  802. Begin
  803. { save the type of register used. }
  804. tempreg:=actasmregister;
  805. Consume(AS_REGISTER);
  806. if (actasmtoken in [AS_end,AS_SEPARATOR,AS_COMMA]) then
  807. Begin
  808. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  809. Message(asmr_e_invalid_operand_type);
  810. oper.opr.typ:=OPR_REGISTER;
  811. oper.opr.reg:=tempreg;
  812. end
  813. else if (actasmtoken=AS_NOT) and (actopcode in [A_LDM,A_STM,A_FLDM,A_FSTM]) then
  814. begin
  815. consume(AS_NOT);
  816. oper.opr.typ:=OPR_REFERENCE;
  817. oper.opr.ref.addressmode:=AM_PREINDEXED;
  818. oper.opr.ref.index:=tempreg;
  819. end
  820. else
  821. Message(asmr_e_syn_operand);
  822. end;
  823. { Registerset }
  824. AS_LSBRACKET:
  825. begin
  826. consume(AS_LSBRACKET);
  827. registerset:=[];
  828. regtype:=R_INVALIDREGISTER;
  829. subreg:=R_SUBNONE;
  830. while true do
  831. begin
  832. if actasmtoken=AS_REGISTER then
  833. begin
  834. include(registerset,getsupreg(actasmregister));
  835. if regtype<>R_INVALIDREGISTER then
  836. begin
  837. if (getregtype(actasmregister)<>regtype) or
  838. (getsubreg(actasmregister)<>subreg) then
  839. Message(asmr_e_mixing_regtypes);
  840. end
  841. else
  842. begin
  843. regtype:=getregtype(actasmregister);
  844. subreg:=getsubreg(actasmregister);
  845. end;
  846. tempreg:=actasmregister;
  847. consume(AS_REGISTER);
  848. if actasmtoken=AS_MINUS then
  849. begin
  850. consume(AS_MINUS);
  851. for ireg:=getsupreg(tempreg) to getsupreg(actasmregister) do
  852. include(registerset,ireg);
  853. consume(AS_REGISTER);
  854. end;
  855. end
  856. else
  857. consume(AS_REGISTER);
  858. if actasmtoken=AS_COMMA then
  859. consume(AS_COMMA)
  860. else
  861. break;
  862. end;
  863. consume(AS_RSBRACKET);
  864. oper.opr.typ:=OPR_REGSET;
  865. oper.opr.regtype:=regtype;
  866. oper.opr.subreg:=subreg;
  867. oper.opr.regset:=registerset;
  868. if (registerset=[]) then
  869. Message(asmr_e_empty_regset);
  870. end;
  871. AS_end,
  872. AS_SEPARATOR,
  873. AS_COMMA: ;
  874. else
  875. Begin
  876. Message(asmr_e_syn_operand);
  877. Consume(actasmtoken);
  878. end;
  879. end; { end case }
  880. end;
  881. {*****************************************************************************
  882. tarmattreader
  883. *****************************************************************************}
  884. procedure tarmattreader.BuildOpCode(instr : tarminstruction);
  885. var
  886. operandnum : longint;
  887. Begin
  888. { opcode }
  889. if (actasmtoken<>AS_OPCODE) then
  890. Begin
  891. Message(asmr_e_invalid_or_missing_opcode);
  892. RecoverConsume(true);
  893. exit;
  894. end;
  895. { Fill the instr object with the current state }
  896. with instr do
  897. begin
  898. Opcode:=ActOpcode;
  899. condition:=ActCondition;
  900. oppostfix:=actoppostfix;
  901. wideformat:=actwideformat;
  902. end;
  903. { We are reading operands, so opcode will be an AS_ID }
  904. operandnum:=1;
  905. Consume(AS_OPCODE);
  906. { Zero operand opcode ? }
  907. if actasmtoken in [AS_SEPARATOR,AS_end] then
  908. begin
  909. operandnum:=0;
  910. exit;
  911. end;
  912. { Read the operands }
  913. repeat
  914. case actasmtoken of
  915. AS_COMMA: { Operand delimiter }
  916. Begin
  917. if ((instr.opcode=A_MOV) and (operandnum=2)) or
  918. ((operandnum=3) and not(instr.opcode in [A_UMLAL,A_UMULL,A_SMLAL,A_SMULL,A_MLA])) then
  919. begin
  920. Consume(AS_COMMA);
  921. if not(TryBuildShifterOp(instr.Operands[operandnum+1] as tarmoperand)) then
  922. Message(asmr_e_illegal_shifterop_syntax);
  923. Inc(operandnum);
  924. end
  925. else
  926. begin
  927. if operandnum>Max_Operands then
  928. Message(asmr_e_too_many_operands)
  929. else
  930. Inc(operandnum);
  931. Consume(AS_COMMA);
  932. end;
  933. end;
  934. AS_SEPARATOR,
  935. AS_end : { End of asm operands for this opcode }
  936. begin
  937. break;
  938. end;
  939. else
  940. BuildOperand(instr.Operands[operandnum] as tarmoperand);
  941. end; { end case }
  942. until false;
  943. instr.Ops:=operandnum;
  944. end;
  945. function tarmattreader.is_asmopcode(const s: string):boolean;
  946. const
  947. { sorted by length so longer postfixes will match first }
  948. postfix2strsorted : array[1..31] of string[3] = (
  949. 'IAD','DBD','FDD','EAD',
  950. 'IAS','DBS','FDS','EAS',
  951. 'IAX','DBX','FDX','EAX',
  952. 'EP','SB','BT','SH',
  953. 'IA','IB','DA','DB','FD','FA','ED','EA',
  954. 'B','D','E','P','T','H','S');
  955. postfixsorted : array[1..31] of TOpPostfix = (
  956. PF_IAD,PF_DBD,PF_FDD,PF_EAD,
  957. PF_IAS,PF_DBS,PF_FDS,PF_EAS,
  958. PF_IAX,PF_DBX,PF_FDX,PF_EAX,
  959. PF_EP,PF_SB,PF_BT,PF_SH,
  960. PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA,
  961. PF_B,PF_D,PF_E,PF_P,PF_T,PF_H,PF_S);
  962. var
  963. j : longint;
  964. hs : string;
  965. maxlen : longint;
  966. icond : tasmcond;
  967. Begin
  968. { making s a value parameter would break other assembler readers }
  969. hs:=s;
  970. is_asmopcode:=false;
  971. { clear op code }
  972. actopcode:=A_None;
  973. actcondition:=C_None;
  974. { first, handle B else BLS is read wrong }
  975. if ((hs[1]='B') and (length(hs)=3)) then
  976. begin
  977. for icond:=low(tasmcond) to high(tasmcond) do
  978. begin
  979. if copy(hs,2,3)=uppercond2str[icond] then
  980. begin
  981. actopcode:=A_B;
  982. actasmtoken:=AS_OPCODE;
  983. actcondition:=icond;
  984. is_asmopcode:=true;
  985. exit;
  986. end;
  987. end;
  988. end;
  989. maxlen:=max(length(hs),5);
  990. actopcode:=A_NONE;
  991. for j:=maxlen downto 1 do
  992. begin
  993. actopcode:=tasmop(PtrUInt(iasmops.Find(copy(hs,1,j))));
  994. if actopcode<>A_NONE then
  995. begin
  996. actasmtoken:=AS_OPCODE;
  997. { strip op code }
  998. delete(hs,1,j);
  999. break;
  1000. end;
  1001. end;
  1002. if actopcode=A_NONE then
  1003. exit;
  1004. { search for condition, conditions are always 2 chars }
  1005. if length(hs)>1 then
  1006. begin
  1007. for icond:=low(tasmcond) to high(tasmcond) do
  1008. begin
  1009. if copy(hs,1,2)=uppercond2str[icond] then
  1010. begin
  1011. actcondition:=icond;
  1012. { strip condition }
  1013. delete(hs,1,2);
  1014. break;
  1015. end;
  1016. end;
  1017. end;
  1018. { check for postfix }
  1019. if length(hs)>0 then
  1020. begin
  1021. for j:=low(postfixsorted) to high(postfixsorted) do
  1022. begin
  1023. if copy(hs,1,length(postfix2strsorted[j]))=postfix2strsorted[j] then
  1024. begin
  1025. actoppostfix:=postfixsorted[j];
  1026. { strip postfix }
  1027. delete(hs,1,length(postfix2strsorted[j]));
  1028. break;
  1029. end;
  1030. end;
  1031. end;
  1032. { check for format postfix }
  1033. if length(hs)>0 then
  1034. begin
  1035. if upcase(copy(hs,1,2)) = '.W' then
  1036. begin
  1037. actwideformat:=true;
  1038. delete(hs,1,2);
  1039. end;
  1040. end;
  1041. { if we stripped all postfixes, it's a valid opcode }
  1042. is_asmopcode:=length(hs)=0;
  1043. end;
  1044. procedure tarmattreader.ConvertCalljmp(instr : tarminstruction);
  1045. var
  1046. newopr : toprrec;
  1047. begin
  1048. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  1049. begin
  1050. newopr.typ:=OPR_SYMBOL;
  1051. newopr.symbol:=instr.Operands[1].opr.ref.symbol;
  1052. newopr.symofs:=instr.Operands[1].opr.ref.offset;
  1053. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  1054. (instr.Operands[1].opr.ref.index<>NR_NO) then
  1055. Message(asmr_e_syn_operand);
  1056. instr.Operands[1].opr:=newopr;
  1057. end;
  1058. end;
  1059. procedure tarmattreader.handleopcode;
  1060. var
  1061. instr : tarminstruction;
  1062. begin
  1063. instr:=TarmInstruction.Create(TarmOperand);
  1064. BuildOpcode(instr);
  1065. if is_calljmp(instr.opcode) then
  1066. ConvertCalljmp(instr);
  1067. {
  1068. instr.AddReferenceSizes;
  1069. instr.SetInstructionOpsize;
  1070. instr.CheckOperandSizes;
  1071. }
  1072. instr.ConcatInstruction(curlist);
  1073. instr.Free;
  1074. actoppostfix:=PF_None;
  1075. actwideformat:=false;
  1076. end;
  1077. {*****************************************************************************
  1078. Initialize
  1079. *****************************************************************************}
  1080. const
  1081. asmmode_arm_att_info : tasmmodeinfo =
  1082. (
  1083. id : asmmode_arm_gas;
  1084. idtxt : 'GAS';
  1085. casmreader : tarmattreader;
  1086. );
  1087. asmmode_arm_standard_info : tasmmodeinfo =
  1088. (
  1089. id : asmmode_standard;
  1090. idtxt : 'STANDARD';
  1091. casmreader : tarmattreader;
  1092. );
  1093. initialization
  1094. RegisterAsmMode(asmmode_arm_att_info);
  1095. RegisterAsmMode(asmmode_arm_standard_info);
  1096. end.