raarmgas.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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. function is_targetdirective(const s: string): boolean; override;
  30. procedure handleopcode;override;
  31. procedure BuildReference(oper : tarmoperand);
  32. procedure BuildOperand(oper : tarmoperand);
  33. procedure BuildSpecialreg(oper : tarmoperand);
  34. function TryBuildShifterOp(oper : tarmoperand) : boolean;
  35. procedure BuildOpCode(instr : tarminstruction);
  36. procedure ReadSym(oper : tarmoperand);
  37. procedure ConvertCalljmp(instr : tarminstruction);
  38. procedure HandleTargetDirective; override;
  39. protected
  40. function is_unified: boolean; virtual;
  41. end;
  42. tarmunifiedattreader = class(tarmattreader)
  43. protected
  44. function is_unified: boolean; override;
  45. end;
  46. Implementation
  47. uses
  48. { helpers }
  49. cutils,
  50. { global }
  51. globtype,globals,verbose,
  52. systems,aasmbase,aasmtai,aasmdata,aasmcpu,
  53. { symtable }
  54. symconst,symsym,symdef,
  55. procinfo,
  56. rabase,rautils,
  57. cgbase,cgutils,paramgr;
  58. function tarmunifiedattreader.is_unified: boolean;
  59. begin
  60. result:=true;
  61. end;
  62. function tarmattreader.is_register(const s:string):boolean;
  63. type
  64. treg2str = record
  65. name : string[3];
  66. reg : tregister;
  67. end;
  68. const
  69. extraregs : array[0..19+16] of treg2str = (
  70. (name: 'A1'; reg : NR_R0),
  71. (name: 'A2'; reg : NR_R1),
  72. (name: 'A3'; reg : NR_R2),
  73. (name: 'A4'; reg : NR_R3),
  74. (name: 'V1'; reg : NR_R4),
  75. (name: 'V2'; reg : NR_R5),
  76. (name: 'V3'; reg : NR_R6),
  77. (name: 'V4'; reg : NR_R7),
  78. (name: 'V5'; reg : NR_R8),
  79. (name: 'V6'; reg : NR_R9),
  80. (name: 'V7'; reg : NR_R10),
  81. (name: 'V8'; reg : NR_R11),
  82. (name: 'WR'; reg : NR_R7),
  83. (name: 'SB'; reg : NR_R9),
  84. (name: 'SL'; reg : NR_R10),
  85. (name: 'FP'; reg : NR_R11),
  86. (name: 'IP'; reg : NR_R12),
  87. (name: 'SP'; reg : NR_R13),
  88. (name: 'LR'; reg : NR_R14),
  89. (name: 'PC'; reg : NR_R15),
  90. (name: 'C0'; reg : NR_CR0),
  91. (name: 'C1'; reg : NR_CR1),
  92. (name: 'C2'; reg : NR_CR2),
  93. (name: 'C3'; reg : NR_CR3),
  94. (name: 'C4'; reg : NR_CR4),
  95. (name: 'C5'; reg : NR_CR5),
  96. (name: 'C6'; reg : NR_CR6),
  97. (name: 'C7'; reg : NR_CR7),
  98. (name: 'C8'; reg : NR_CR8),
  99. (name: 'C9'; reg : NR_CR9),
  100. (name: 'C10'; reg : NR_CR10),
  101. (name: 'C11'; reg : NR_CR11),
  102. (name: 'C12'; reg : NR_CR12),
  103. (name: 'C13'; reg : NR_CR13),
  104. (name: 'C14'; reg : NR_CR14),
  105. (name: 'C15'; reg : NR_CR15)
  106. );
  107. var
  108. i : longint;
  109. begin
  110. result:=inherited is_register(s);
  111. { reg found?
  112. possible aliases are always 2 char
  113. }
  114. if result or (not (length(s) in [2,3])) then
  115. exit;
  116. for i:=low(extraregs) to high(extraregs) do
  117. begin
  118. if s=extraregs[i].name then
  119. begin
  120. actasmregister:=extraregs[i].reg;
  121. result:=true;
  122. actasmtoken:=AS_REGISTER;
  123. exit;
  124. end;
  125. end;
  126. end;
  127. function tarmattreader.is_targetdirective(const s: string): boolean;
  128. begin
  129. case s of
  130. '.thumb_func',
  131. '.code',
  132. '.thumb_set':
  133. result:=true
  134. else
  135. Result:=inherited is_targetdirective(s);
  136. end;
  137. end;
  138. procedure tarmattreader.ReadSym(oper : tarmoperand);
  139. var
  140. tempstr, mangledname : string;
  141. typesize,l,k : tcgint;
  142. begin
  143. tempstr:=actasmpattern;
  144. Consume(AS_ID);
  145. { typecasting? }
  146. if (actasmtoken=AS_LPAREN) and
  147. SearchType(tempstr,typesize) then
  148. begin
  149. oper.hastype:=true;
  150. Consume(AS_LPAREN);
  151. BuildOperand(oper);
  152. Consume(AS_RPAREN);
  153. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  154. oper.SetSize(typesize,true);
  155. end
  156. else
  157. if not oper.SetupVar(tempstr,false) then
  158. Message1(sym_e_unknown_id,tempstr);
  159. { record.field ? }
  160. if actasmtoken=AS_DOT then
  161. begin
  162. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  163. if (mangledname<>'') then
  164. Message(asmr_e_invalid_reference_syntax);
  165. inc(oper.opr.ref.offset,l);
  166. end;
  167. end;
  168. Procedure tarmattreader.BuildReference(oper : tarmoperand);
  169. procedure do_error;
  170. begin
  171. Message(asmr_e_invalid_reference_syntax);
  172. RecoverConsume(false);
  173. end;
  174. procedure test_end(require_rbracket : boolean);
  175. begin
  176. if require_rbracket then begin
  177. if not(actasmtoken=AS_RBRACKET) then
  178. begin
  179. do_error;
  180. exit;
  181. end
  182. else
  183. Consume(AS_RBRACKET);
  184. if (actasmtoken=AS_NOT) then
  185. begin
  186. oper.opr.ref.addressmode:=AM_PREINDEXED;
  187. Consume(AS_NOT);
  188. end;
  189. end;
  190. if not(actasmtoken in [AS_SEPARATOR,AS_end]) then
  191. do_error
  192. else
  193. begin
  194. {$IFDEF debugasmreader}
  195. writeln('TEST_end_FINAL_OK. Created the following ref:');
  196. writeln('oper.opr.ref.shiftimm=',oper.opr.ref.shiftimm);
  197. writeln('oper.opr.ref.shiftmode=',ord(oper.opr.ref.shiftmode));
  198. writeln('oper.opr.ref.index=',ord(oper.opr.ref.index));
  199. writeln('oper.opr.ref.base=',ord(oper.opr.ref.base));
  200. writeln('oper.opr.ref.signindex=',ord(oper.opr.ref.signindex));
  201. writeln('oper.opr.ref.addressmode=',ord(oper.opr.ref.addressmode));
  202. writeln;
  203. {$endIF debugasmreader}
  204. end;
  205. end;
  206. function is_shifter_ref_operation(var a : tshiftmode) : boolean;
  207. begin
  208. a := SM_NONE;
  209. if (actasmpattern='LSL') then
  210. a := SM_LSL
  211. else if (actasmpattern='LSR') then
  212. a := SM_LSR
  213. else if (actasmpattern='ASR') then
  214. a := SM_ASR
  215. else if (actasmpattern='ROR') then
  216. a := SM_ROR
  217. else if (actasmpattern='RRX') then
  218. a := SM_RRX;
  219. is_shifter_ref_operation := not(a=SM_NONE);
  220. end;
  221. procedure read_index_shift(require_rbracket : boolean);
  222. var
  223. shift : aint;
  224. begin
  225. case actasmtoken of
  226. AS_COMMA :
  227. begin
  228. Consume(AS_COMMA);
  229. if not(actasmtoken=AS_ID) then
  230. do_error;
  231. if is_shifter_ref_operation(oper.opr.ref.shiftmode) then
  232. begin
  233. Consume(AS_ID);
  234. if not(oper.opr.ref.shiftmode=SM_RRX) then
  235. begin
  236. if not(actasmtoken=AS_HASH) then
  237. do_error;
  238. Consume(AS_HASH);
  239. shift := BuildConstExpression(false,true);
  240. if (shift<0) or (shift>32) then
  241. do_error;
  242. oper.opr.ref.shiftimm := shift;
  243. test_end(require_rbracket);
  244. end
  245. else
  246. test_end(require_rbracket);
  247. end
  248. else
  249. begin
  250. do_error;
  251. exit;
  252. end;
  253. end;
  254. AS_RBRACKET :
  255. if require_rbracket then
  256. test_end(require_rbracket)
  257. else
  258. begin
  259. do_error;
  260. exit;
  261. end;
  262. AS_SEPARATOR,AS_END :
  263. if not require_rbracket then
  264. test_end(false)
  265. else
  266. do_error;
  267. else
  268. begin
  269. do_error;
  270. exit;
  271. end;
  272. end;
  273. end;
  274. procedure read_index(require_rbracket : boolean);
  275. var
  276. recname : string;
  277. o_int,s_int : tcgint;
  278. begin
  279. case actasmtoken of
  280. AS_REGISTER :
  281. begin
  282. oper.opr.ref.index:=actasmregister;
  283. Consume(AS_REGISTER);
  284. read_index_shift(require_rbracket);
  285. exit;
  286. end;
  287. AS_PLUS,AS_MINUS :
  288. begin
  289. if actasmtoken=AS_PLUS then
  290. begin
  291. Consume(AS_PLUS);
  292. end
  293. else
  294. begin
  295. oper.opr.ref.signindex := -1;
  296. Consume(AS_MINUS);
  297. end;
  298. if actasmtoken=AS_REGISTER then
  299. begin
  300. oper.opr.ref.index:=actasmregister;
  301. Consume(AS_REGISTER);
  302. read_index_shift(require_rbracket);
  303. exit;
  304. end
  305. else
  306. begin
  307. do_error;
  308. exit;
  309. end;
  310. test_end(require_rbracket);
  311. exit;
  312. end;
  313. AS_HASH : // constant
  314. begin
  315. Consume(AS_HASH);
  316. o_int := BuildConstExpression(false,true);
  317. if (o_int>4095) or (o_int<-4095) then
  318. begin
  319. Message(asmr_e_constant_out_of_bounds);
  320. RecoverConsume(false);
  321. exit;
  322. end
  323. else
  324. begin
  325. inc(oper.opr.ref.offset,o_int);
  326. test_end(require_rbracket);
  327. exit;
  328. end;
  329. end;
  330. AS_ID :
  331. begin
  332. recname := actasmpattern;
  333. Consume(AS_ID);
  334. BuildRecordOffsetSize(recname,o_int,s_int,recname,false);
  335. if (o_int>4095)or(o_int<-4095) then
  336. begin
  337. Message(asmr_e_constant_out_of_bounds);
  338. RecoverConsume(false);
  339. exit;
  340. end
  341. else
  342. begin
  343. inc(oper.opr.ref.offset,o_int);
  344. test_end(require_rbracket);
  345. exit;
  346. end;
  347. end;
  348. AS_AT:
  349. begin
  350. do_error;
  351. exit;
  352. end;
  353. AS_DOT : // local label
  354. begin
  355. oper.opr.ref.signindex := BuildConstExpression(true,false);
  356. test_end(require_rbracket);
  357. exit;
  358. end;
  359. AS_RBRACKET :
  360. begin
  361. if require_rbracket then
  362. begin
  363. test_end(require_rbracket);
  364. exit;
  365. end
  366. else
  367. begin
  368. do_error; // unexpected rbracket
  369. exit;
  370. end;
  371. end;
  372. AS_SEPARATOR,AS_end :
  373. begin
  374. if not require_rbracket then
  375. begin
  376. test_end(false);
  377. exit;
  378. end
  379. else
  380. begin
  381. do_error;
  382. exit;
  383. end;
  384. end;
  385. else
  386. begin
  387. // unexpected token
  388. do_error;
  389. exit;
  390. end;
  391. end; // case
  392. end;
  393. procedure try_prepostindexed;
  394. begin
  395. Consume(AS_RBRACKET);
  396. case actasmtoken of
  397. AS_COMMA :
  398. begin // post-indexed
  399. Consume(AS_COMMA);
  400. oper.opr.ref.addressmode:=AM_POSTINDEXED;
  401. read_index(false);
  402. exit;
  403. end;
  404. AS_NOT :
  405. begin // pre-indexed
  406. Consume(AS_NOT);
  407. oper.opr.ref.addressmode:=AM_PREINDEXED;
  408. test_end(false);
  409. exit;
  410. end;
  411. else
  412. begin
  413. test_end(false);
  414. exit;
  415. end;
  416. end; // case
  417. end;
  418. var
  419. lab : TASMLABEL;
  420. begin
  421. Consume(AS_LBRACKET);
  422. oper.opr.ref.addressmode:=AM_OFFSET; // assume "neither PRE nor POST inc"
  423. if actasmtoken=AS_REGISTER then
  424. begin
  425. oper.opr.ref.base:=actasmregister;
  426. Consume(AS_REGISTER);
  427. case actasmtoken of
  428. AS_RBRACKET :
  429. begin
  430. try_prepostindexed;
  431. exit;
  432. end;
  433. AS_COMMA :
  434. begin
  435. Consume(AS_COMMA);
  436. read_index(true);
  437. exit;
  438. end;
  439. else
  440. begin
  441. Message(asmr_e_invalid_reference_syntax);
  442. RecoverConsume(false);
  443. end;
  444. end;
  445. end
  446. else
  447. {
  448. if base isn't a register, r15=PC is implied base, so it must be a local label.
  449. pascal constants don't make sense, because implied r15
  450. record offsets probably don't make sense, too (a record offset of code?)
  451. TODO: However, we could make the Stackpointer implied.
  452. }
  453. Begin
  454. case actasmtoken of
  455. AS_ID :
  456. begin
  457. if is_locallabel(actasmpattern) then
  458. begin
  459. CreateLocalLabel(actasmpattern,lab,false);
  460. oper.opr.ref.symbol := lab;
  461. oper.opr.ref.base := NR_PC;
  462. Consume(AS_ID);
  463. test_end(true);
  464. exit;
  465. end
  466. else
  467. begin
  468. // TODO: Stackpointer implied,
  469. Message(asmr_e_invalid_reference_syntax);
  470. RecoverConsume(false);
  471. exit;
  472. end;
  473. end;
  474. else
  475. begin // elsecase
  476. Message(asmr_e_invalid_reference_syntax);
  477. RecoverConsume(false);
  478. exit;
  479. end;
  480. end;
  481. end;
  482. end;
  483. function tarmattreader.TryBuildShifterOp(oper : tarmoperand) : boolean;
  484. procedure handlepara(sm : tshiftmode);
  485. begin
  486. consume(AS_ID);
  487. fillchar(oper.opr,sizeof(oper.opr),0);
  488. oper.opr.typ:=OPR_SHIFTEROP;
  489. oper.opr.shifterop.shiftmode:=sm;
  490. if sm<>SM_RRX then
  491. begin
  492. case actasmtoken of
  493. AS_REGISTER:
  494. begin
  495. oper.opr.shifterop.rs:=actasmregister;
  496. consume(AS_REGISTER);
  497. end;
  498. AS_HASH:
  499. begin
  500. consume(AS_HASH);
  501. oper.opr.shifterop.shiftimm:=BuildConstExpression(false,false);
  502. end;
  503. else
  504. Message(asmr_e_illegal_shifterop_syntax);
  505. end;
  506. end;
  507. end;
  508. begin
  509. result:=true;
  510. if (actasmtoken=AS_ID) then
  511. begin
  512. if (actasmpattern='LSL') then
  513. handlepara(SM_LSL)
  514. else if (actasmpattern='LSR') then
  515. handlepara(SM_LSR)
  516. else if (actasmpattern='ASR') then
  517. handlepara(SM_ASR)
  518. else if (actasmpattern='ROR') then
  519. handlepara(SM_ROR)
  520. else if (actasmpattern='RRX') then
  521. handlepara(SM_RRX)
  522. else
  523. result:=false;
  524. end
  525. else
  526. result:=false;
  527. end;
  528. Procedure tarmattreader.BuildOperand(oper : tarmoperand);
  529. var
  530. expr : string;
  531. typesize,l : tcgint;
  532. procedure AddLabelOperand(hl:tasmlabel);
  533. begin
  534. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  535. is_calljmp(actopcode) then
  536. begin
  537. oper.opr.typ:=OPR_SYMBOL;
  538. oper.opr.symbol:=hl;
  539. end
  540. else
  541. begin
  542. oper.InitRef;
  543. oper.opr.ref.symbol:=hl;
  544. oper.opr.ref.base:=NR_PC;
  545. if (actasmtoken in [AS_PLUS, AS_MINUS]) then
  546. begin
  547. l:=BuildConstExpression(true,false);
  548. oper.opr.ref.offset:=l;
  549. end;
  550. end;
  551. end;
  552. procedure MaybeRecordOffset;
  553. var
  554. mangledname: string;
  555. hasdot : boolean;
  556. l,
  557. toffset,
  558. tsize : tcgint;
  559. begin
  560. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  561. exit;
  562. l:=0;
  563. mangledname:='';
  564. hasdot:=(actasmtoken=AS_DOT);
  565. if hasdot then
  566. begin
  567. if expr<>'' then
  568. begin
  569. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  570. if (oper.opr.typ<>OPR_CONSTANT) and
  571. (mangledname<>'') then
  572. Message(asmr_e_wrong_sym_type);
  573. inc(l,toffset);
  574. oper.SetSize(tsize,true);
  575. end;
  576. end;
  577. if actasmtoken in [AS_PLUS,AS_MINUS] then
  578. inc(l,BuildConstExpression(true,false));
  579. case oper.opr.typ of
  580. OPR_LOCAL :
  581. begin
  582. { don't allow direct access to fields of parameters, because that
  583. will generate buggy code. Allow it only for explicit typecasting }
  584. if hasdot and
  585. (not oper.hastype) and
  586. (oper.opr.localsym.typ=paravarsym) and
  587. (not(po_assembler in current_procinfo.procdef.procoptions) or
  588. (tparavarsym(oper.opr.localsym).paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  589. (not is_implicit_pointer_object_type(oper.opr.localsym.vardef) and
  590. not paramanager.push_addr_param(oper.opr.localsym.varspez,oper.opr.localsym.vardef,current_procinfo.procdef.proccalloption))) then
  591. Message(asmr_e_cannot_access_field_directly_for_parameters);
  592. inc(oper.opr.localsymofs,l)
  593. end;
  594. OPR_CONSTANT :
  595. inc(oper.opr.val,l);
  596. OPR_REFERENCE :
  597. if (mangledname<>'') then
  598. begin
  599. if (oper.opr.val<>0) then
  600. Message(asmr_e_wrong_sym_type);
  601. oper.opr.typ:=OPR_SYMBOL;
  602. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname,AT_FUNCTION);
  603. end
  604. else
  605. inc(oper.opr.val,l);
  606. OPR_SYMBOL:
  607. Message(asmr_e_invalid_symbol_ref);
  608. else
  609. internalerror(200309221);
  610. end;
  611. end;
  612. function MaybeBuildReference:boolean;
  613. { Try to create a reference, if not a reference is found then false
  614. is returned }
  615. begin
  616. MaybeBuildReference:=true;
  617. case actasmtoken of
  618. AS_INTNUM,
  619. AS_MINUS,
  620. AS_PLUS:
  621. Begin
  622. oper.opr.ref.offset:=BuildConstExpression(True,False);
  623. if actasmtoken<>AS_LPAREN then
  624. Message(asmr_e_invalid_reference_syntax)
  625. else
  626. BuildReference(oper);
  627. end;
  628. AS_LPAREN:
  629. BuildReference(oper);
  630. AS_ID: { only a variable is allowed ... }
  631. Begin
  632. ReadSym(oper);
  633. case actasmtoken of
  634. AS_end,
  635. AS_SEPARATOR,
  636. AS_COMMA: ;
  637. AS_LPAREN:
  638. BuildReference(oper);
  639. else
  640. Begin
  641. Message(asmr_e_invalid_reference_syntax);
  642. Consume(actasmtoken);
  643. end;
  644. end; {end case }
  645. end;
  646. else
  647. MaybeBuildReference:=false;
  648. end; { end case }
  649. end;
  650. function is_ConditionCode(hs: string): boolean;
  651. var icond: tasmcond;
  652. begin
  653. is_ConditionCode := false;
  654. case actopcode of
  655. A_IT,A_ITE,A_ITT,
  656. A_ITEE,A_ITTE,A_ITET,A_ITTT,
  657. A_ITEEE,A_ITTEE,A_ITETE,A_ITTTE,A_ITEET,A_ITTET,A_ITETT,A_ITTTT:
  658. begin
  659. { search for condition, conditions are always 2 chars }
  660. if length(hs)>1 then
  661. begin
  662. for icond:=low(tasmcond) to high(tasmcond) do
  663. begin
  664. if copy(hs,1,2)=uppercond2str[icond] then
  665. begin
  666. //actcondition:=icond;
  667. oper.opr.typ := OPR_COND;
  668. oper.opr.cc := icond;
  669. exit(true);
  670. end;
  671. end;
  672. end;
  673. end;
  674. end;
  675. end;
  676. function is_modeflag(hs : string): boolean;
  677. var
  678. i: longint;
  679. flags: tcpumodeflags;
  680. begin
  681. is_modeflag := false;
  682. flags:=[];
  683. hs:=lower(hs);
  684. if (actopcode in [A_CPSID,A_CPSIE]) and (length(hs) >= 1) then
  685. begin
  686. for i:=1 to length(hs) do
  687. begin
  688. case hs[i] of
  689. 'a':
  690. Include(flags,mfA);
  691. 'f':
  692. Include(flags,mfF);
  693. 'i':
  694. Include(flags,mfI);
  695. else
  696. exit;
  697. end;
  698. end;
  699. oper.opr.typ := OPR_MODEFLAGS;
  700. oper.opr.flags := flags;
  701. exit(true);
  702. end;
  703. end;
  704. procedure BuildDirectRef;
  705. function GetConstLabel(const symname: string; ofs: aint): TAsmLabel;
  706. var
  707. hp: tai;
  708. newconst: tai_const;
  709. lab: TAsmLabel;
  710. begin
  711. if symname<>'' then
  712. newconst:=tai_const.Createname(symname,ofs)
  713. else
  714. newconst:=tai_const.Create_32bit(ofs);
  715. hp:=tai(current_procinfo.aktlocaldata.First);
  716. while assigned(hp) do
  717. begin
  718. if hp.typ=ait_const then
  719. begin
  720. if (tai_const(hp).sym=newconst.sym) and
  721. (tai_const(hp).value=newconst.value) and
  722. assigned(hp.Previous) and
  723. (tai(hp.previous).typ=ait_label) then
  724. begin
  725. newconst.Free;
  726. result:=tai_label(hp.Previous).labsym;
  727. exit;
  728. end;
  729. end;
  730. hp:=tai(hp.Next);
  731. end;
  732. current_asmdata.getjumplabel(lab);
  733. current_procinfo.aktlocaldata.concat(tai_align.create(4));
  734. current_procinfo.aktlocaldata.concat(tai_label.create(lab));
  735. current_procinfo.aktlocaldata.concat(newconst);
  736. result:=lab;
  737. end;
  738. var
  739. symtype: TAsmsymtype;
  740. sym: string;
  741. val: tcgint;
  742. begin
  743. case actasmtoken of
  744. AS_INTNUM,
  745. AS_ID:
  746. begin
  747. BuildConstSymbolExpression(true,false,false,val,sym,symtype);
  748. if symtype=AT_NONE then
  749. sym:='';
  750. reference_reset(oper.opr.ref,4,[]);
  751. oper.opr.ref.base:=NR_PC;
  752. oper.opr.ref.symbol:=GetConstLabel(sym,val);
  753. end;
  754. end;
  755. end;
  756. function getregsetindex(reg: tregister): integer;
  757. begin
  758. if getsubreg(reg)=R_SUBFS then
  759. begin
  760. result:=getsupreg(reg)*2;
  761. if result>32 then
  762. result:=result-63;
  763. end
  764. else
  765. result:=getsupreg(reg);
  766. end;
  767. var
  768. tempreg : tregister;
  769. ireg : tsuperregister;
  770. regtype: tregistertype;
  771. subreg: tsubregister;
  772. hl : tasmlabel;
  773. {ofs : longint;}
  774. registerset : tcpuregisterset;
  775. Begin
  776. expr:='';
  777. case actasmtoken of
  778. AS_LBRACKET: { Memory reference or constant expression }
  779. Begin
  780. oper.InitRef;
  781. BuildReference(oper);
  782. end;
  783. AS_HASH: { Constant expression }
  784. Begin
  785. Consume(AS_HASH);
  786. BuildConstantOperand(oper);
  787. end;
  788. AS_EQUAL:
  789. begin
  790. case actopcode of
  791. A_LDRBT,A_LDRB,A_LDR,A_LDRH,A_LDRSB,A_LDRSH,A_LDRT,
  792. A_LDREX,A_LDREXB,A_LDREXD,A_LDREXH:
  793. begin
  794. consume(AS_EQUAL);
  795. oper.InitRef;
  796. BuildDirectRef;
  797. end;
  798. else
  799. Message(asmr_e_invalid_opcode_and_operand);
  800. end;
  801. end;
  802. (*
  803. AS_INTNUM,
  804. AS_MINUS,
  805. AS_PLUS:
  806. Begin
  807. { Constant memory offset }
  808. { This must absolutely be followed by ( }
  809. oper.InitRef;
  810. oper.opr.ref.offset:=BuildConstExpression(True,False);
  811. if actasmtoken<>AS_LPAREN then
  812. begin
  813. ofs:=oper.opr.ref.offset;
  814. BuildConstantOperand(oper);
  815. inc(oper.opr.val,ofs);
  816. end
  817. else
  818. BuildReference(oper);
  819. end;
  820. *)
  821. AS_ID: { A constant expression, or a Variable ref. }
  822. Begin
  823. if is_modeflag(actasmpattern) then
  824. begin
  825. consume(AS_ID);
  826. end
  827. else
  828. { Condition code? }
  829. if is_conditioncode(actasmpattern) then
  830. begin
  831. consume(AS_ID);
  832. end
  833. else
  834. { Local Label ? }
  835. if is_locallabel(actasmpattern) then
  836. begin
  837. CreateLocalLabel(actasmpattern,hl,false);
  838. Consume(AS_ID);
  839. AddLabelOperand(hl);
  840. end
  841. else
  842. { Check for label }
  843. if SearchLabel(actasmpattern,hl,false) then
  844. begin
  845. Consume(AS_ID);
  846. AddLabelOperand(hl);
  847. end
  848. else
  849. { probably a variable or normal expression }
  850. { or a procedure (such as in CALL ID) }
  851. Begin
  852. { is it a constant ? }
  853. if SearchIConstant(actasmpattern,l) then
  854. Begin
  855. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  856. Message(asmr_e_invalid_operand_type);
  857. BuildConstantOperand(oper);
  858. end
  859. else
  860. begin
  861. expr:=actasmpattern;
  862. Consume(AS_ID);
  863. { typecasting? }
  864. if (actasmtoken=AS_LPAREN) and
  865. SearchType(expr,typesize) then
  866. begin
  867. oper.hastype:=true;
  868. Consume(AS_LPAREN);
  869. BuildOperand(oper);
  870. Consume(AS_RPAREN);
  871. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  872. oper.SetSize(typesize,true);
  873. end
  874. else
  875. begin
  876. if not(oper.SetupVar(expr,false)) then
  877. Begin
  878. { look for special symbols ... }
  879. if expr= '__HIGH' then
  880. begin
  881. consume(AS_LPAREN);
  882. if not oper.setupvar('high'+actasmpattern,false) then
  883. Message1(sym_e_unknown_id,'high'+actasmpattern);
  884. consume(AS_ID);
  885. consume(AS_RPAREN);
  886. end
  887. else
  888. if expr = '__RESULT' then
  889. oper.SetUpResult
  890. else
  891. if expr = '__SELF' then
  892. oper.SetupSelf
  893. else
  894. if expr = '__OLDEBP' then
  895. oper.SetupOldEBP
  896. else
  897. Message1(sym_e_unknown_id,expr);
  898. end;
  899. end;
  900. end;
  901. if actasmtoken=AS_DOT then
  902. MaybeRecordOffset;
  903. { add a constant expression? }
  904. if (actasmtoken=AS_PLUS) then
  905. begin
  906. l:=BuildConstExpression(true,false);
  907. case oper.opr.typ of
  908. OPR_CONSTANT :
  909. inc(oper.opr.val,l);
  910. OPR_LOCAL :
  911. inc(oper.opr.localsymofs,l);
  912. OPR_REFERENCE :
  913. inc(oper.opr.ref.offset,l);
  914. else
  915. internalerror(200309202);
  916. end;
  917. end
  918. end;
  919. { Do we have a indexing reference, then parse it also }
  920. if actasmtoken=AS_LPAREN then
  921. BuildReference(oper);
  922. end;
  923. { Register, a variable reference or a constant reference }
  924. AS_REGISTER:
  925. Begin
  926. { save the type of register used. }
  927. tempreg:=actasmregister;
  928. Consume(AS_REGISTER);
  929. if (actasmtoken in [AS_end,AS_SEPARATOR,AS_COMMA]) then
  930. Begin
  931. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  932. Message(asmr_e_invalid_operand_type);
  933. oper.opr.typ:=OPR_REGISTER;
  934. oper.opr.reg:=tempreg;
  935. end
  936. else if (actasmtoken=AS_NOT) and (actopcode in [A_LDM,A_STM,A_FLDM,A_FSTM,A_VLDM,A_VSTM,A_SRS,A_RFE]) then
  937. begin
  938. consume(AS_NOT);
  939. oper.opr.typ:=OPR_REFERENCE;
  940. oper.opr.ref.addressmode:=AM_PREINDEXED;
  941. oper.opr.ref.index:=tempreg;
  942. end
  943. else
  944. Message(asmr_e_syn_operand);
  945. end;
  946. { Registerset }
  947. AS_LSBRACKET:
  948. begin
  949. consume(AS_LSBRACKET);
  950. registerset:=[];
  951. regtype:=R_INVALIDREGISTER;
  952. subreg:=R_SUBNONE;
  953. while actasmtoken<>AS_RSBRACKET do
  954. begin
  955. if actasmtoken=AS_REGISTER then
  956. begin
  957. include(registerset,getregsetindex(actasmregister));
  958. if regtype<>R_INVALIDREGISTER then
  959. begin
  960. if (getregtype(actasmregister)<>regtype) or
  961. (getsubreg(actasmregister)<>subreg) then
  962. Message(asmr_e_mixing_regtypes);
  963. end
  964. else
  965. begin
  966. regtype:=getregtype(actasmregister);
  967. subreg:=getsubreg(actasmregister);
  968. end;
  969. tempreg:=actasmregister;
  970. consume(AS_REGISTER);
  971. if actasmtoken=AS_MINUS then
  972. begin
  973. consume(AS_MINUS);
  974. for ireg:=getregsetindex(tempreg) to getregsetindex(actasmregister) do
  975. include(registerset,ireg);
  976. consume(AS_REGISTER);
  977. end;
  978. end
  979. else
  980. consume(AS_REGISTER);
  981. if actasmtoken=AS_COMMA then
  982. consume(AS_COMMA)
  983. else
  984. break;
  985. end;
  986. consume(AS_RSBRACKET);
  987. oper.opr.typ:=OPR_REGSET;
  988. oper.opr.regtype:=regtype;
  989. oper.opr.subreg:=subreg;
  990. oper.opr.regset:=registerset;
  991. if actasmtoken=AS_XOR then
  992. begin
  993. consume(AS_XOR);
  994. oper.opr.usermode:=true;
  995. end
  996. else
  997. oper.opr.usermode:=false;
  998. if (registerset=[]) then
  999. Message(asmr_e_empty_regset);
  1000. end;
  1001. AS_end,
  1002. AS_SEPARATOR,
  1003. AS_COMMA: ;
  1004. else
  1005. Begin
  1006. Message(asmr_e_syn_operand);
  1007. Consume(actasmtoken);
  1008. end;
  1009. end; { end case }
  1010. end;
  1011. procedure tarmattreader.BuildSpecialreg(oper: tarmoperand);
  1012. var
  1013. hs, reg : String;
  1014. ch : char;
  1015. i, t : longint;
  1016. hreg : tregister;
  1017. flags : tspecialregflags;
  1018. begin
  1019. hreg:=NR_NO;
  1020. case actasmtoken of
  1021. AS_REGISTER:
  1022. begin
  1023. oper.opr.typ:=OPR_REGISTER;
  1024. oper.opr.reg:=actasmregister;
  1025. Consume(AS_REGISTER);
  1026. end;
  1027. AS_ID:
  1028. begin
  1029. t := pos('_', actasmpattern);
  1030. if t > 0 then
  1031. begin
  1032. hs:=lower(actasmpattern);
  1033. reg:=copy(hs, 1, t-1);
  1034. delete(hs, 1, t);
  1035. if length(hs) < 1 then
  1036. Message(asmr_e_invalid_operand_type);
  1037. if reg = 'cpsr' then
  1038. hreg:=NR_CPSR
  1039. else if reg='spsr' then
  1040. hreg:=NR_SPSR
  1041. else
  1042. Message(asmr_e_invalid_register);
  1043. flags:=[];
  1044. for i := 1 to length(hs) do
  1045. begin
  1046. ch:=hs[i];
  1047. if ch='c' then
  1048. include(flags, srC)
  1049. else if ch='x' then
  1050. include(flags, srX)
  1051. else if ch='f' then
  1052. include(flags, srF)
  1053. else if ch='s' then
  1054. include(flags, srS)
  1055. else
  1056. message(asmr_e_invalid_operand_type);
  1057. end;
  1058. oper.opr.typ:=OPR_SPECIALREG;
  1059. oper.opr.specialreg:=hreg;
  1060. oper.opr.specialregflags:=flags;
  1061. consume(AS_ID);
  1062. end
  1063. else
  1064. Message(asmr_e_invalid_operand_type); // Otherwise it would have been seen as a AS_REGISTER
  1065. end;
  1066. end;
  1067. end;
  1068. {*****************************************************************************
  1069. tarmattreader
  1070. *****************************************************************************}
  1071. procedure tarmattreader.BuildOpCode(instr : tarminstruction);
  1072. var
  1073. operandnum : longint;
  1074. Begin
  1075. { opcode }
  1076. if (actasmtoken<>AS_OPCODE) then
  1077. Begin
  1078. Message(asmr_e_invalid_or_missing_opcode);
  1079. RecoverConsume(true);
  1080. exit;
  1081. end;
  1082. { Fill the instr object with the current state }
  1083. with instr do
  1084. begin
  1085. Opcode:=ActOpcode;
  1086. condition:=ActCondition;
  1087. oppostfix:=actoppostfix;
  1088. wideformat:=actwideformat;
  1089. end;
  1090. { We are reading operands, so opcode will be an AS_ID }
  1091. operandnum:=1;
  1092. Consume(AS_OPCODE);
  1093. { Zero operand opcode ? }
  1094. if actasmtoken in [AS_SEPARATOR,AS_end] then
  1095. begin
  1096. operandnum:=0;
  1097. exit;
  1098. end;
  1099. { Read the operands }
  1100. repeat
  1101. case actasmtoken of
  1102. AS_COMMA: { Operand delimiter }
  1103. Begin
  1104. if ((instr.opcode in [A_MOV,A_MVN,A_CMP,A_CMN,A_TST,A_TEQ,
  1105. A_UXTB,A_UXTH,A_UXTB16,
  1106. A_SXTB,A_SXTH,A_SXTB16]) and
  1107. (operandnum=2)) or
  1108. ((operandnum=3) and not(instr.opcode in [A_UMLAL,A_UMULL,A_SMLAL,A_SMULL,A_MLA,A_UMAAL,A_MLS,
  1109. A_SMLABB,A_SMLABT,A_SMLATB,A_SMLATT,A_SMMLA,A_SMMLS,A_SMLAD,A_SMLALD,A_SMLSD,
  1110. A_SMLALBB,A_SMLALBT,A_SMLALTB,A_SMLALTT,A_SMLSLD,
  1111. A_SMLAWB,A_SMLAWT,
  1112. A_MRC,A_MCR,A_MCRR,A_MRRC,A_MRC2,A_MCR2,A_MCRR2,A_MRRC2,
  1113. A_STREXD,A_STRD,
  1114. A_USADA8,
  1115. A_VMOV,
  1116. A_SBFX,A_UBFX,A_BFI])) then
  1117. begin
  1118. Consume(AS_COMMA);
  1119. if not(TryBuildShifterOp(instr.Operands[operandnum+1] as tarmoperand)) then
  1120. Message(asmr_e_illegal_shifterop_syntax);
  1121. Inc(operandnum);
  1122. end
  1123. else
  1124. begin
  1125. if operandnum>Max_Operands then
  1126. Message(asmr_e_too_many_operands)
  1127. else
  1128. Inc(operandnum);
  1129. Consume(AS_COMMA);
  1130. end;
  1131. end;
  1132. AS_SEPARATOR,
  1133. AS_end : { End of asm operands for this opcode }
  1134. begin
  1135. break;
  1136. end;
  1137. else
  1138. if ((instr.opcode = A_MRS) and (operandnum = 2)) or
  1139. ((instr.opcode = A_MSR) and (operandnum = 1)) then
  1140. BuildSpecialreg(instr.Operands[operandnum] as tarmoperand)
  1141. else
  1142. BuildOperand(instr.Operands[operandnum] as tarmoperand);
  1143. end; { end case }
  1144. until false;
  1145. instr.Ops:=operandnum;
  1146. end;
  1147. function tarmattreader.is_asmopcode(const s: string):boolean;
  1148. const
  1149. { sorted by length so longer postfixes will match first }
  1150. postfix2strsorted : array[1..70] of string[9] = (
  1151. '.F32.S32','.F32.U32','.S32.F32','.U32.F32','.F64.S32','.F64.U32','.S32.F64','.U32.F64',
  1152. '.F32.S16','.F32.U16','.S16.F32','.U16.F32','.F64.S16','.F64.U16','.S16.F64','.U16.F64',
  1153. '.F32.F64','.F64.F32',
  1154. '.I16','.I32','.I64','.S16','.S32','.S64','.U16','.U32','.U64','.F32','.F64',
  1155. 'IAD','DBD','FDD','EAD','IAS','DBS','FDS','EAS','IAX','DBX','FDX','EAX',
  1156. '.16','.32','.64','.I8','.S8','.U8','.P8',
  1157. 'EP','SB','BT','SH','IA','IB','DA','DB','FD','FA','ED','EA',
  1158. '.8','S','D','E','P','X','R','B','H','T');
  1159. postfixsorted : array[1..70] of TOpPostfix = (
  1160. PF_F32S32,PF_F32U32,PF_S32F32,PF_U32F32,PF_F64S32,PF_F64U32,PF_S32F64,PF_U32F64,
  1161. PF_F32S16,PF_F32U16,PF_S16F32,PF_U16F32,PF_F64S16,PF_F64U16,PF_S16F64,PF_U16F64,
  1162. PF_F32F64,PF_F64F32,
  1163. PF_I16,PF_I32,
  1164. PF_I64,PF_S16,PF_S32,PF_S64,PF_U16,PF_U32,PF_U64,PF_F32,
  1165. PF_F64,PF_IAD,PF_DBD,PF_FDD,PF_EAD,
  1166. PF_IAS,PF_DBS,PF_FDS,PF_EAS,PF_IAX,
  1167. PF_DBX,PF_FDX,PF_EAX,PF_16,PF_32,
  1168. PF_64,PF_I8,PF_S8,PF_U8,PF_P8,
  1169. PF_EP,PF_SB,PF_BT,PF_SH,PF_IA,
  1170. PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,
  1171. PF_ED,PF_EA,PF_8,PF_S,PF_D,PF_E,
  1172. PF_P,PF_X,PF_R,PF_B,PF_H,PF_T);
  1173. var
  1174. j, j2 : longint;
  1175. hs,hs2 : string;
  1176. maxlen : longint;
  1177. icond : tasmcond;
  1178. Begin
  1179. { making s a value parameter would break other assembler readers }
  1180. hs:=s;
  1181. is_asmopcode:=false;
  1182. { clear op code }
  1183. actopcode:=A_None;
  1184. actcondition:=C_None;
  1185. { first, handle B else BLS is read wrong }
  1186. if ((hs[1]='B') and (length(hs)=3)) then
  1187. begin
  1188. for icond:=low(tasmcond) to high(tasmcond) do
  1189. begin
  1190. if copy(hs,2,3)=uppercond2str[icond] then
  1191. begin
  1192. actopcode:=A_B;
  1193. actasmtoken:=AS_OPCODE;
  1194. actcondition:=icond;
  1195. is_asmopcode:=true;
  1196. exit;
  1197. end;
  1198. end;
  1199. end;
  1200. maxlen:=min(length(hs),6);
  1201. actopcode:=A_NONE;
  1202. j2:=maxlen;
  1203. hs2:=hs;
  1204. while j2>=1 do
  1205. begin
  1206. hs:=hs2;
  1207. while j2>=1 do
  1208. begin
  1209. actopcode:=tasmop(PtrUInt(iasmops.Find(copy(hs,1,j2))));
  1210. if actopcode<>A_NONE then
  1211. begin
  1212. actasmtoken:=AS_OPCODE;
  1213. { strip op code }
  1214. delete(hs,1,j2);
  1215. dec(j2);
  1216. break;
  1217. end;
  1218. dec(j2);
  1219. end;
  1220. if actopcode=A_NONE then
  1221. exit;
  1222. if is_unified then
  1223. begin
  1224. { check for postfix }
  1225. if (length(hs)>0) and (actoppostfix=PF_None) then
  1226. begin
  1227. for j:=low(postfixsorted) to high(postfixsorted) do
  1228. begin
  1229. if copy(hs,1,length(postfix2strsorted[j]))=postfix2strsorted[j] then
  1230. begin
  1231. if not ((length(hs)-length(postfix2strsorted[j])) in [0,2,4]) then
  1232. continue;
  1233. actoppostfix:=postfixsorted[j];
  1234. { strip postfix }
  1235. delete(hs,1,length(postfix2strsorted[j]));
  1236. break;
  1237. end;
  1238. end;
  1239. end;
  1240. { search for condition, conditions are always 2 chars }
  1241. if length(hs)>1 then
  1242. begin
  1243. for icond:=low(tasmcond) to high(tasmcond) do
  1244. begin
  1245. if copy(hs,1,2)=uppercond2str[icond] then
  1246. begin
  1247. actcondition:=icond;
  1248. { strip condition }
  1249. delete(hs,1,2);
  1250. break;
  1251. end;
  1252. end;
  1253. end;
  1254. { check for postfix }
  1255. if (length(hs)>0) and (actoppostfix=PF_None) then
  1256. begin
  1257. for j:=low(postfixsorted) to high(postfixsorted) do
  1258. begin
  1259. if copy(hs,1,length(postfix2strsorted[j]))=postfix2strsorted[j] then
  1260. begin
  1261. if not ((length(hs)-length(postfix2strsorted[j])) = 0) then
  1262. continue;
  1263. actoppostfix:=postfixsorted[j];
  1264. { strip postfix }
  1265. delete(hs,1,length(postfix2strsorted[j]));
  1266. break;
  1267. end;
  1268. end;
  1269. end;
  1270. end
  1271. else
  1272. begin
  1273. { search for condition, conditions are always 2 chars }
  1274. if length(hs)>1 then
  1275. begin
  1276. for icond:=low(tasmcond) to high(tasmcond) do
  1277. begin
  1278. if copy(hs,1,2)=uppercond2str[icond] then
  1279. begin
  1280. actcondition:=icond;
  1281. { strip condition }
  1282. delete(hs,1,2);
  1283. break;
  1284. end;
  1285. end;
  1286. end;
  1287. { check for postfix }
  1288. if (length(hs)>0) and (actoppostfix=PF_None) then
  1289. begin
  1290. for j:=low(postfixsorted) to high(postfixsorted) do
  1291. begin
  1292. if copy(hs,1,length(postfix2strsorted[j]))=postfix2strsorted[j] then
  1293. begin
  1294. actoppostfix:=postfixsorted[j];
  1295. { strip postfix }
  1296. delete(hs,1,length(postfix2strsorted[j]));
  1297. break;
  1298. end;
  1299. end;
  1300. end;
  1301. end;
  1302. { check for format postfix }
  1303. if length(hs)>0 then
  1304. begin
  1305. if copy(hs,1,2) = '.W' then
  1306. begin
  1307. actwideformat:=true;
  1308. delete(hs,1,2);
  1309. end;
  1310. end;
  1311. { if we stripped all postfixes, it's a valid opcode }
  1312. is_asmopcode:=length(hs)=0;
  1313. if is_asmopcode = true then
  1314. break;
  1315. end;
  1316. end;
  1317. procedure tarmattreader.ConvertCalljmp(instr : tarminstruction);
  1318. var
  1319. newopr : toprrec;
  1320. begin
  1321. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  1322. begin
  1323. newopr.typ:=OPR_SYMBOL;
  1324. newopr.symbol:=instr.Operands[1].opr.ref.symbol;
  1325. newopr.symofs:=instr.Operands[1].opr.ref.offset;
  1326. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  1327. (instr.Operands[1].opr.ref.index<>NR_NO) then
  1328. Message(asmr_e_syn_operand);
  1329. instr.Operands[1].opr:=newopr;
  1330. end;
  1331. end;
  1332. procedure tarmattreader.HandleTargetDirective;
  1333. var
  1334. symname,
  1335. symval : String;
  1336. val : tcgint;
  1337. symtyp : TAsmsymtype;
  1338. begin
  1339. case actasmpattern of
  1340. '.thumb_set':
  1341. begin
  1342. consume(AS_TARGET_DIRECTIVE);
  1343. BuildConstSymbolExpression(true,false,false, val,symname,symtyp);
  1344. Consume(AS_COMMA);
  1345. BuildConstSymbolExpression(true,false,false, val,symval,symtyp);
  1346. curList.concat(tai_symbolpair.create(spk_thumb_set,symname,symval));
  1347. end;
  1348. '.code':
  1349. begin
  1350. consume(AS_TARGET_DIRECTIVE);
  1351. val:=BuildConstExpression(false,false);
  1352. if not(val in [16,32]) then
  1353. Message(asmr_e_invalid_code_value);
  1354. curList.concat(tai_directive.create(asd_code,tostr(val)));
  1355. end;
  1356. '.thumb_func':
  1357. begin
  1358. consume(AS_TARGET_DIRECTIVE);
  1359. curList.concat(tai_directive.create(asd_thumb_func,''));
  1360. end
  1361. else
  1362. inherited HandleTargetDirective;
  1363. end;
  1364. end;
  1365. function tarmattreader.is_unified: boolean;
  1366. begin
  1367. result:=false;
  1368. end;
  1369. procedure tarmattreader.handleopcode;
  1370. var
  1371. instr : tarminstruction;
  1372. begin
  1373. instr:=TarmInstruction.Create(TarmOperand);
  1374. BuildOpcode(instr);
  1375. if is_calljmp(instr.opcode) then
  1376. ConvertCalljmp(instr);
  1377. {
  1378. instr.AddReferenceSizes;
  1379. instr.SetInstructionOpsize;
  1380. instr.CheckOperandSizes;
  1381. }
  1382. instr.ConcatInstruction(curlist);
  1383. instr.Free;
  1384. actoppostfix:=PF_None;
  1385. actwideformat:=false;
  1386. end;
  1387. {*****************************************************************************
  1388. Initialize
  1389. *****************************************************************************}
  1390. const
  1391. asmmode_arm_att_info : tasmmodeinfo =
  1392. (
  1393. id : asmmode_arm_gas;
  1394. idtxt : 'DIVIDED';
  1395. casmreader : tarmattreader;
  1396. );
  1397. asmmode_arm_att_unified_info : tasmmodeinfo =
  1398. (
  1399. id : asmmode_arm_gas_unified;
  1400. idtxt : 'UNIFIED';
  1401. casmreader : tarmunifiedattreader;
  1402. );
  1403. asmmode_arm_standard_info : tasmmodeinfo =
  1404. (
  1405. id : asmmode_standard;
  1406. idtxt : 'STANDARD';
  1407. casmreader : tarmattreader;
  1408. );
  1409. initialization
  1410. RegisterAsmMode(asmmode_arm_att_info);
  1411. RegisterAsmMode(asmmode_arm_att_unified_info);
  1412. RegisterAsmMode(asmmode_arm_standard_info);
  1413. end.