aasmcpu.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Contains the assembler object for the ARM
  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 aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,globtype,globals,verbose,
  22. aasmbase,aasmtai,
  23. symtype,
  24. cpubase,cpuinfo,cgbase,cgutils;
  25. const
  26. { "mov reg,reg" source operand number }
  27. O_MOV_SOURCE = 1;
  28. { "mov reg,reg" source operand number }
  29. O_MOV_DEST = 0;
  30. { Operand types }
  31. OT_NONE = $00000000;
  32. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  33. OT_BITS16 = $00000002;
  34. OT_BITS32 = $00000004;
  35. OT_BITS64 = $00000008; { FPU only }
  36. OT_BITS80 = $00000010;
  37. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  38. OT_NEAR = $00000040;
  39. OT_SHORT = $00000080;
  40. OT_BITSTINY = $00000100; { fpu constant }
  41. OT_BITSSHIFTER =
  42. $00000200;
  43. OT_SIZE_MASK = $000003FF; { all the size attributes }
  44. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  45. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  46. OT_TO = $00000200; { operand is followed by a colon }
  47. { reverse effect in FADD, FSUB &c }
  48. OT_COLON = $00000400;
  49. OT_SHIFTEROP = $00000800;
  50. OT_REGISTER = $00001000;
  51. OT_IMMEDIATE = $00002000;
  52. OT_REGLIST = $00008000;
  53. OT_IMM8 = $00002001;
  54. OT_IMM24 = $00002002;
  55. OT_IMM32 = $00002004;
  56. OT_IMM64 = $00002008;
  57. OT_IMM80 = $00002010;
  58. OT_IMMTINY = $00002100;
  59. OT_IMMSHIFTER= $00002200;
  60. OT_IMMEDIATE24 = OT_IMM24;
  61. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  62. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  63. OT_IMMEDIATEFPU = OT_IMMTINY;
  64. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  65. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  66. OT_REG8 = $00201001;
  67. OT_REG16 = $00201002;
  68. OT_REG32 = $00201004;
  69. OT_REG64 = $00201008;
  70. OT_VREG = $00201010; { vector register }
  71. OT_MEMORY = $00204000; { register number in 'basereg' }
  72. OT_MEM8 = $00204001;
  73. OT_MEM16 = $00204002;
  74. OT_MEM32 = $00204004;
  75. OT_MEM64 = $00204008;
  76. OT_MEM80 = $00204010;
  77. { word/byte load/store }
  78. OT_AM2 = $00010000;
  79. { misc ld/st operations }
  80. OT_AM3 = $00020000;
  81. { multiple ld/st operations }
  82. OT_AM4 = $00040000;
  83. { co proc. ld/st operations }
  84. OT_AM5 = $00080000;
  85. OT_AMMASK = $000f0000;
  86. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  87. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  88. OT_FPUREG = $01000000; { floating point stack registers }
  89. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  90. { a mask for the following }
  91. OT_MEM_OFFS = $00604000; { special type of EA }
  92. { simple [address] offset }
  93. OT_ONENESS = $00800000; { special type of immediate operand }
  94. { so UNITY == IMMEDIATE | ONENESS }
  95. OT_UNITY = $00802000; { for shift/rotate instructions }
  96. instabentries = {$i armnop.inc}
  97. maxinfolen = 5;
  98. IF_NONE = $00000000;
  99. IF_ARMMASK = $000F0000;
  100. IF_ARM7 = $00070000;
  101. IF_FPMASK = $00F00000;
  102. IF_FPA = $00100000;
  103. { if the instruction can change in a second pass }
  104. IF_PASS2 = longint($80000000);
  105. type
  106. TInsTabCache=array[TasmOp] of longint;
  107. PInsTabCache=^TInsTabCache;
  108. tinsentry = record
  109. opcode : tasmop;
  110. ops : byte;
  111. optypes : array[0..3] of longint;
  112. code : array[0..maxinfolen] of char;
  113. flags : longint;
  114. end;
  115. pinsentry=^tinsentry;
  116. const
  117. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  118. var
  119. InsTabCache : PInsTabCache;
  120. type
  121. taicpu = class(tai_cpu_abstract)
  122. oppostfix : TOpPostfix;
  123. roundingmode : troundingmode;
  124. procedure loadshifterop(opidx:longint;const so:tshifterop);
  125. procedure loadregset(opidx:longint;const s:tcpuregisterset);
  126. constructor op_none(op : tasmop);
  127. constructor op_reg(op : tasmop;_op1 : tregister);
  128. constructor op_const(op : tasmop;_op1 : longint);
  129. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  130. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  131. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  132. constructor op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  133. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  134. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  135. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  136. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  137. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  138. { SFM/LFM }
  139. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  140. { *M*LL }
  141. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  142. { this is for Jmp instructions }
  143. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  144. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  145. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  146. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  147. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  148. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  149. function spilling_get_operation_type(opnr: longint): topertype;override;
  150. { assembler }
  151. public
  152. { the next will reset all instructions that can change in pass 2 }
  153. procedure ResetPass1;
  154. procedure ResetPass2;
  155. function CheckIfValid:boolean;
  156. function GetString:string;
  157. function Pass1(offset:longint):longint;virtual;
  158. procedure Pass2(objdata:TAsmObjectdata);virtual;
  159. protected
  160. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  161. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  162. procedure ppubuildderefimploper(var o:toper);override;
  163. procedure ppuderefoper(var o:toper);override;
  164. private
  165. { next fields are filled in pass1, so pass2 is faster }
  166. inssize : shortint;
  167. insoffset : longint;
  168. LastInsOffset : longint; { need to be public to be reset }
  169. insentry : PInsEntry;
  170. function InsEnd:longint;
  171. procedure create_ot;
  172. function Matches(p:PInsEntry):longint;
  173. function calcsize(p:PInsEntry):shortint;
  174. procedure gencode(objdata:TAsmObjectData);
  175. function NeedAddrPrefix(opidx:byte):boolean;
  176. procedure Swapoperands;
  177. function FindInsentry:boolean;
  178. end;
  179. tai_align = class(tai_align_abstract)
  180. { nothing to add }
  181. end;
  182. function spilling_create_load(const ref:treference;r:tregister): tai;
  183. function spilling_create_store(r:tregister; const ref:treference): tai;
  184. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  185. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  186. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  187. { inserts pc relative symbols at places where they are reachable }
  188. procedure insertpcrelativedata(list,listtoinsert : taasmoutput);
  189. procedure InitAsm;
  190. procedure DoneAsm;
  191. implementation
  192. uses
  193. cutils,rgobj,itcpugas;
  194. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  195. begin
  196. allocate_oper(opidx+1);
  197. with oper[opidx]^ do
  198. begin
  199. if typ<>top_shifterop then
  200. begin
  201. clearop(opidx);
  202. new(shifterop);
  203. end;
  204. shifterop^:=so;
  205. typ:=top_shifterop;
  206. if assigned(add_reg_instruction_hook) then
  207. add_reg_instruction_hook(self,shifterop^.rs);
  208. end;
  209. end;
  210. procedure taicpu.loadregset(opidx:longint;const s:tcpuregisterset);
  211. var
  212. i : byte;
  213. begin
  214. allocate_oper(opidx+1);
  215. with oper[opidx]^ do
  216. begin
  217. if typ<>top_regset then
  218. clearop(opidx);
  219. new(regset);
  220. regset^:=s;
  221. typ:=top_regset;
  222. for i:=RS_R0 to RS_R15 do
  223. begin
  224. if assigned(add_reg_instruction_hook) and (i in regset^) then
  225. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  226. end;
  227. end;
  228. end;
  229. {*****************************************************************************
  230. taicpu Constructors
  231. *****************************************************************************}
  232. constructor taicpu.op_none(op : tasmop);
  233. begin
  234. inherited create(op);
  235. end;
  236. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  237. begin
  238. inherited create(op);
  239. ops:=1;
  240. loadreg(0,_op1);
  241. end;
  242. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  243. begin
  244. inherited create(op);
  245. ops:=1;
  246. loadconst(0,aint(_op1));
  247. end;
  248. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  249. begin
  250. inherited create(op);
  251. ops:=2;
  252. loadreg(0,_op1);
  253. loadreg(1,_op2);
  254. end;
  255. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  256. begin
  257. inherited create(op);
  258. ops:=2;
  259. loadreg(0,_op1);
  260. loadconst(1,aint(_op2));
  261. end;
  262. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  263. begin
  264. inherited create(op);
  265. ops:=2;
  266. loadref(0,_op1);
  267. loadregset(1,_op2);
  268. end;
  269. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  270. begin
  271. inherited create(op);
  272. ops:=2;
  273. loadreg(0,_op1);
  274. loadref(1,_op2);
  275. end;
  276. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  277. begin
  278. inherited create(op);
  279. ops:=3;
  280. loadreg(0,_op1);
  281. loadreg(1,_op2);
  282. loadreg(2,_op3);
  283. end;
  284. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  285. begin
  286. inherited create(op);
  287. ops:=4;
  288. loadreg(0,_op1);
  289. loadreg(1,_op2);
  290. loadreg(2,_op3);
  291. loadreg(3,_op4);
  292. end;
  293. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  294. begin
  295. inherited create(op);
  296. ops:=3;
  297. loadreg(0,_op1);
  298. loadreg(1,_op2);
  299. loadconst(2,aint(_op3));
  300. end;
  301. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  302. begin
  303. inherited create(op);
  304. ops:=3;
  305. loadreg(0,_op1);
  306. loadconst(1,_op2);
  307. loadref(2,_op3);
  308. end;
  309. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  310. begin
  311. inherited create(op);
  312. ops:=3;
  313. loadreg(0,_op1);
  314. loadreg(1,_op2);
  315. loadsymbol(0,_op3,_op3ofs);
  316. end;
  317. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  318. begin
  319. inherited create(op);
  320. ops:=3;
  321. loadreg(0,_op1);
  322. loadreg(1,_op2);
  323. loadref(2,_op3);
  324. end;
  325. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  326. begin
  327. inherited create(op);
  328. ops:=3;
  329. loadreg(0,_op1);
  330. loadreg(1,_op2);
  331. loadshifterop(2,_op3);
  332. end;
  333. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  334. begin
  335. inherited create(op);
  336. condition:=cond;
  337. ops:=1;
  338. loadsymbol(0,_op1,0);
  339. end;
  340. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  341. begin
  342. inherited create(op);
  343. ops:=1;
  344. loadsymbol(0,_op1,0);
  345. end;
  346. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  347. begin
  348. inherited create(op);
  349. ops:=1;
  350. loadsymbol(0,_op1,_op1ofs);
  351. end;
  352. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  353. begin
  354. inherited create(op);
  355. ops:=2;
  356. loadreg(0,_op1);
  357. loadsymbol(1,_op2,_op2ofs);
  358. end;
  359. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  360. begin
  361. inherited create(op);
  362. ops:=2;
  363. loadsymbol(0,_op1,_op1ofs);
  364. loadref(1,_op2);
  365. end;
  366. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  367. begin
  368. { allow the register allocator to remove unnecessary moves }
  369. result:=(((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  370. ((opcode=A_MVF) and (regtype = R_FPUREGISTER))
  371. ) and
  372. (condition=C_None) and
  373. (ops=2) and
  374. (oper[0]^.typ=top_reg) and
  375. (oper[1]^.typ=top_reg) and
  376. (oper[0]^.reg=oper[1]^.reg);
  377. end;
  378. function spilling_create_load(const ref:treference;r:tregister): tai;
  379. begin
  380. case getregtype(r) of
  381. R_INTREGISTER :
  382. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  383. R_FPUREGISTER :
  384. { use lfm because we don't know the current internal format
  385. and avoid exceptions
  386. }
  387. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  388. else
  389. internalerror(200401041);
  390. end;
  391. end;
  392. function spilling_create_store(r:tregister; const ref:treference): tai;
  393. begin
  394. case getregtype(r) of
  395. R_INTREGISTER :
  396. result:=taicpu.op_reg_ref(A_STR,r,ref);
  397. R_FPUREGISTER :
  398. { use sfm because we don't know the current internal format
  399. and avoid exceptions
  400. }
  401. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  402. else
  403. internalerror(200401041);
  404. end;
  405. end;
  406. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  407. begin
  408. case opcode of
  409. A_ADC,A_ADD,A_AND,
  410. A_EOR,A_CLZ,
  411. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  412. A_LDRSH,A_LDRT,
  413. A_MOV,A_MVN,A_MLA,A_MUL,
  414. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  415. A_SWP,A_SWPB,
  416. A_LDF,A_FLT,A_FIX,
  417. A_ADF,A_DVF,A_FDV,A_FML,
  418. A_RFS,A_RFC,A_RDF,
  419. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  420. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  421. A_LFM:
  422. if opnr=0 then
  423. result:=operand_write
  424. else
  425. result:=operand_read;
  426. A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  427. A_CMN,A_CMP,A_TEQ,A_TST,
  428. A_CMF,A_CMFE,A_WFS,A_CNF:
  429. result:=operand_read;
  430. A_SMLAL,A_UMLAL:
  431. if opnr in [0,1] then
  432. result:=operand_readwrite
  433. else
  434. result:=operand_read;
  435. A_SMULL,A_UMULL:
  436. if opnr in [0,1] then
  437. result:=operand_write
  438. else
  439. result:=operand_read;
  440. A_STR,A_STRB,A_STRBT,
  441. A_STRH,A_STRT,A_STF,A_SFM:
  442. { important is what happens with the involved registers }
  443. if opnr=0 then
  444. result := operand_read
  445. else
  446. { check for pre/post indexed }
  447. result := operand_read;
  448. else
  449. internalerror(200403151);
  450. end;
  451. end;
  452. procedure BuildInsTabCache;
  453. var
  454. i : longint;
  455. begin
  456. new(instabcache);
  457. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  458. i:=0;
  459. while (i<InsTabEntries) do
  460. begin
  461. if InsTabCache^[InsTab[i].Opcode]=-1 then
  462. InsTabCache^[InsTab[i].Opcode]:=i;
  463. inc(i);
  464. end;
  465. end;
  466. procedure InitAsm;
  467. begin
  468. if not assigned(instabcache) then
  469. BuildInsTabCache;
  470. end;
  471. procedure DoneAsm;
  472. begin
  473. if assigned(instabcache) then
  474. begin
  475. dispose(instabcache);
  476. instabcache:=nil;
  477. end;
  478. end;
  479. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  480. begin
  481. i.oppostfix:=pf;
  482. result:=i;
  483. end;
  484. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  485. begin
  486. i.roundingmode:=rm;
  487. result:=i;
  488. end;
  489. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  490. begin
  491. i.condition:=c;
  492. result:=i;
  493. end;
  494. procedure insertpcrelativedata(list,listtoinsert : taasmoutput);
  495. var
  496. curpos : longint;
  497. lastpos : longint;
  498. curop : longint;
  499. curtai : tai;
  500. curdatatai,hp : tai;
  501. curdata : taasmoutput;
  502. l : tasmlabel;
  503. begin
  504. curdata:=taasmoutput.create;
  505. lastpos:=-1;
  506. curpos:=0;
  507. curtai:=tai(list.first);
  508. while assigned(curtai) do
  509. begin
  510. { instruction? }
  511. if curtai.typ=ait_instruction then
  512. begin
  513. { walk through all operand of the instruction }
  514. for curop:=0 to taicpu(curtai).ops-1 do
  515. begin
  516. { reference? }
  517. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  518. begin
  519. { pc relative symbol? }
  520. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  521. if assigned(curdatatai) then
  522. begin
  523. { if yes, insert till next symbol }
  524. repeat
  525. hp:=tai(curdatatai.next);
  526. listtoinsert.remove(curdatatai);
  527. curdata.concat(curdatatai);
  528. curdatatai:=hp;
  529. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  530. if lastpos=-1 then
  531. lastpos:=curpos;
  532. end;
  533. end;
  534. end;
  535. inc(curpos);
  536. end;
  537. { split only at real instructions else the test below fails }
  538. if ((curpos-lastpos)>1016) and (curtai.typ=ait_instruction) and
  539. (
  540. { don't split loads of pc to lr and the following move }
  541. not(
  542. (taicpu(curtai).opcode=A_MOV) and
  543. (taicpu(curtai).oper[0]^.typ=top_reg) and
  544. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  545. (taicpu(curtai).oper[1]^.typ=top_reg) and
  546. (taicpu(curtai).oper[1]^.reg=NR_PC)
  547. )
  548. ) then
  549. begin
  550. lastpos:=curpos;
  551. hp:=tai(curtai.next);
  552. objectlibrary.getlabel(l);
  553. curdata.insert(taicpu.op_sym(A_B,l));
  554. curdata.concat(tai_label.create(l));
  555. list.insertlistafter(curtai,curdata);
  556. curtai:=hp;
  557. end
  558. else
  559. curtai:=tai(curtai.next);
  560. end;
  561. list.concatlist(curdata);
  562. curdata.free;
  563. end;
  564. (*
  565. Floating point instruction format information, taken from the linux kernel
  566. ARM Floating Point Instruction Classes
  567. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  568. |c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
  569. |c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
  570. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  571. |c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
  572. |c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
  573. |c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
  574. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  575. CPDT data transfer instructions
  576. LDF, STF, LFM (copro 2), SFM (copro 2)
  577. CPDO dyadic arithmetic instructions
  578. ADF, MUF, SUF, RSF, DVF, RDF,
  579. POW, RPW, RMF, FML, FDV, FRD, POL
  580. CPDO monadic arithmetic instructions
  581. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  582. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  583. CPRT joint arithmetic/data transfer instructions
  584. FIX (arithmetic followed by load/store)
  585. FLT (load/store followed by arithmetic)
  586. CMF, CNF CMFE, CNFE (comparisons)
  587. WFS, RFS (write/read floating point status register)
  588. WFC, RFC (write/read floating point control register)
  589. cond condition codes
  590. P pre/post index bit: 0 = postindex, 1 = preindex
  591. U up/down bit: 0 = stack grows down, 1 = stack grows up
  592. W write back bit: 1 = update base register (Rn)
  593. L load/store bit: 0 = store, 1 = load
  594. Rn base register
  595. Rd destination/source register
  596. Fd floating point destination register
  597. Fn floating point source register
  598. Fm floating point source register or floating point constant
  599. uv transfer length (TABLE 1)
  600. wx register count (TABLE 2)
  601. abcd arithmetic opcode (TABLES 3 & 4)
  602. ef destination size (rounding precision) (TABLE 5)
  603. gh rounding mode (TABLE 6)
  604. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  605. i constant bit: 1 = constant (TABLE 6)
  606. */
  607. /*
  608. TABLE 1
  609. +-------------------------+---+---+---------+---------+
  610. | Precision | u | v | FPSR.EP | length |
  611. +-------------------------+---+---+---------+---------+
  612. | Single | 0 | 0 | x | 1 words |
  613. | Double | 1 | 1 | x | 2 words |
  614. | Extended | 1 | 1 | x | 3 words |
  615. | Packed decimal | 1 | 1 | 0 | 3 words |
  616. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  617. +-------------------------+---+---+---------+---------+
  618. Note: x = don't care
  619. */
  620. /*
  621. TABLE 2
  622. +---+---+---------------------------------+
  623. | w | x | Number of registers to transfer |
  624. +---+---+---------------------------------+
  625. | 0 | 1 | 1 |
  626. | 1 | 0 | 2 |
  627. | 1 | 1 | 3 |
  628. | 0 | 0 | 4 |
  629. +---+---+---------------------------------+
  630. */
  631. /*
  632. TABLE 3: Dyadic Floating Point Opcodes
  633. +---+---+---+---+----------+-----------------------+-----------------------+
  634. | a | b | c | d | Mnemonic | Description | Operation |
  635. +---+---+---+---+----------+-----------------------+-----------------------+
  636. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  637. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  638. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  639. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  640. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  641. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  642. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  643. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  644. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  645. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  646. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  647. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  648. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  649. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  650. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  651. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  652. +---+---+---+---+----------+-----------------------+-----------------------+
  653. Note: POW, RPW, POL are deprecated, and are available for backwards
  654. compatibility only.
  655. */
  656. /*
  657. TABLE 4: Monadic Floating Point Opcodes
  658. +---+---+---+---+----------+-----------------------+-----------------------+
  659. | a | b | c | d | Mnemonic | Description | Operation |
  660. +---+---+---+---+----------+-----------------------+-----------------------+
  661. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  662. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  663. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  664. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  665. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  666. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  667. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  668. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  669. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  670. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  671. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  672. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  673. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  674. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  675. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  676. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  677. +---+---+---+---+----------+-----------------------+-----------------------+
  678. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  679. available for backwards compatibility only.
  680. */
  681. /*
  682. TABLE 5
  683. +-------------------------+---+---+
  684. | Rounding Precision | e | f |
  685. +-------------------------+---+---+
  686. | IEEE Single precision | 0 | 0 |
  687. | IEEE Double precision | 0 | 1 |
  688. | IEEE Extended precision | 1 | 0 |
  689. | undefined (trap) | 1 | 1 |
  690. +-------------------------+---+---+
  691. */
  692. /*
  693. TABLE 5
  694. +---------------------------------+---+---+
  695. | Rounding Mode | g | h |
  696. +---------------------------------+---+---+
  697. | Round to nearest (default) | 0 | 0 |
  698. | Round toward plus infinity | 0 | 1 |
  699. | Round toward negative infinity | 1 | 0 |
  700. | Round toward zero | 1 | 1 |
  701. +---------------------------------+---+---+
  702. *)
  703. function taicpu.GetString:string;
  704. var
  705. i : longint;
  706. s : string;
  707. addsize : boolean;
  708. begin
  709. s:='['+gas_op2str[opcode];
  710. for i:=0 to ops-1 do
  711. begin
  712. with oper[i]^ do
  713. begin
  714. if i=0 then
  715. s:=s+' '
  716. else
  717. s:=s+',';
  718. { type }
  719. addsize:=false;
  720. if (ot and OT_VREG)=OT_VREG then
  721. s:=s+'vreg'
  722. else
  723. if (ot and OT_FPUREG)=OT_FPUREG then
  724. s:=s+'fpureg'
  725. else
  726. if (ot and OT_REGISTER)=OT_REGISTER then
  727. begin
  728. s:=s+'reg';
  729. addsize:=true;
  730. end
  731. else
  732. if (ot and OT_REGLIST)=OT_REGLIST then
  733. begin
  734. s:=s+'reglist';
  735. addsize:=false;
  736. end
  737. else
  738. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  739. begin
  740. s:=s+'imm';
  741. addsize:=true;
  742. end
  743. else
  744. if (ot and OT_MEMORY)=OT_MEMORY then
  745. begin
  746. s:=s+'mem';
  747. addsize:=true;
  748. if (ot and OT_AM2)<>0 then
  749. s:=s+' am2';
  750. end
  751. else
  752. s:=s+'???';
  753. { size }
  754. if addsize then
  755. begin
  756. if (ot and OT_BITS8)<>0 then
  757. s:=s+'8'
  758. else
  759. if (ot and OT_BITS16)<>0 then
  760. s:=s+'16'
  761. else
  762. if (ot and OT_BITS32)<>0 then
  763. s:=s+'32'
  764. else
  765. if (ot and OT_BITSSHIFTER)<>0 then
  766. s:=s+'shifter'
  767. else
  768. s:=s+'??';
  769. { signed }
  770. if (ot and OT_SIGNED)<>0 then
  771. s:=s+'s';
  772. end;
  773. end;
  774. end;
  775. GetString:=s+']';
  776. end;
  777. procedure taicpu.ResetPass1;
  778. begin
  779. { we need to reset everything here, because the choosen insentry
  780. can be invalid for a new situation where the previously optimized
  781. insentry is not correct }
  782. InsEntry:=nil;
  783. InsSize:=0;
  784. LastInsOffset:=-1;
  785. end;
  786. procedure taicpu.ResetPass2;
  787. begin
  788. { we are here in a second pass, check if the instruction can be optimized }
  789. if assigned(InsEntry) and
  790. ((InsEntry^.flags and IF_PASS2)<>0) then
  791. begin
  792. InsEntry:=nil;
  793. InsSize:=0;
  794. end;
  795. LastInsOffset:=-1;
  796. end;
  797. function taicpu.CheckIfValid:boolean;
  798. begin
  799. end;
  800. function taicpu.Pass1(offset:longint):longint;
  801. var
  802. ldr2op : array[PF_B..PF_T] of tasmop = (
  803. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  804. str2op : array[PF_B..PF_T] of tasmop = (
  805. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  806. begin
  807. Pass1:=0;
  808. { Save the old offset and set the new offset }
  809. InsOffset:=Offset;
  810. { Error? }
  811. if (Insentry=nil) and (InsSize=-1) then
  812. exit;
  813. { set the file postion }
  814. aktfilepos:=fileinfo;
  815. { tranlate LDR+postfix to complete opcode }
  816. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  817. begin
  818. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  819. opcode:=ldr2op[oppostfix]
  820. else
  821. internalerror(2005091001);
  822. if opcode=A_None then
  823. internalerror(2005091004);
  824. end
  825. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  826. begin
  827. if (oppostfix in [low(str2op)..high(str2op)]) then
  828. opcode:=str2op[oppostfix]
  829. else
  830. internalerror(2005091002);
  831. if opcode=A_None then
  832. internalerror(2005091003);
  833. end;
  834. { Get InsEntry }
  835. if FindInsEntry then
  836. begin
  837. InsSize:=4;
  838. LastInsOffset:=InsOffset;
  839. Pass1:=InsSize;
  840. exit;
  841. end;
  842. LastInsOffset:=-1;
  843. end;
  844. procedure taicpu.Pass2(objdata:TAsmObjectdata);
  845. begin
  846. end;
  847. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  848. begin
  849. end;
  850. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  851. begin
  852. end;
  853. procedure taicpu.ppubuildderefimploper(var o:toper);
  854. begin
  855. end;
  856. procedure taicpu.ppuderefoper(var o:toper);
  857. begin
  858. end;
  859. function taicpu.InsEnd:longint;
  860. begin
  861. end;
  862. procedure taicpu.create_ot;
  863. var
  864. i,l,relsize : longint;
  865. dummy : byte;
  866. begin
  867. if ops=0 then
  868. exit;
  869. { update oper[].ot field }
  870. for i:=0 to ops-1 do
  871. with oper[i]^ do
  872. begin
  873. case typ of
  874. top_regset:
  875. begin
  876. ot:=OT_REGLIST;
  877. end;
  878. top_reg :
  879. begin
  880. case getregtype(reg) of
  881. R_INTREGISTER:
  882. ot:=OT_REG32 or OT_SHIFTEROP;
  883. R_FPUREGISTER:
  884. ot:=OT_FPUREG;
  885. else
  886. internalerror(2005090901);
  887. end;
  888. end;
  889. top_ref :
  890. begin
  891. if ref^.refaddr=addr_no then
  892. begin
  893. { create ot field }
  894. { we should get the size here dependend on the
  895. instruction }
  896. if (ot and OT_SIZE_MASK)=0 then
  897. ot:=OT_MEMORY or OT_BITS32
  898. else
  899. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  900. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  901. ot:=ot or OT_MEM_OFFS;
  902. { if we need to fix a reference, we do it here }
  903. { determine possible address modes }
  904. if (ref^.base<>NR_NO) and
  905. (
  906. (
  907. (ref^.index=NR_NO) and
  908. (ref^.shiftmode=SM_None) and
  909. (ref^.offset>=-4097) and
  910. (ref^.offset<=4097)
  911. ) or
  912. (
  913. (ref^.shiftmode=SM_None) and
  914. (ref^.offset=0)
  915. ) or
  916. (
  917. (ref^.index<>NR_NO) and
  918. (ref^.shiftmode<>SM_None) and
  919. (ref^.shiftimm<=31) and
  920. (ref^.offset=0)
  921. )
  922. ) then
  923. ot:=ot or OT_AM2;
  924. if (ref^.index<>NR_NO) and
  925. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  926. (
  927. (ref^.base=NR_NO) and
  928. (ref^.shiftmode=SM_None) and
  929. (ref^.offset=0)
  930. ) then
  931. ot:=ot or OT_AM4;
  932. end
  933. else
  934. begin
  935. l:=ref^.offset;
  936. if assigned(ref^.symbol) then
  937. inc(l,ref^.symbol.address);
  938. if (not assigned(ref^.symbol) or
  939. ((ref^.symbol.currbind<>AB_EXTERNAL) and (ref^.symbol.address<>0))) and
  940. (relsize>=-33554428) and (relsize<=33554428) and (relsize mod 4=0) then
  941. ot:=OT_IMM24
  942. else
  943. ot:=OT_IMM32 or OT_NEAR;
  944. end;
  945. end;
  946. top_local :
  947. begin
  948. { we should get the size here dependend on the
  949. instruction }
  950. if (ot and OT_SIZE_MASK)=0 then
  951. ot:=OT_MEMORY or OT_BITS32
  952. else
  953. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  954. end;
  955. top_const :
  956. begin
  957. ot:=OT_IMMEDIATE;
  958. if is_shifter_const(val,dummy) then
  959. ot:=OT_IMMSHIFTER
  960. else
  961. ot:=OT_IMM32
  962. end;
  963. top_none :
  964. begin
  965. { generated when there was an error in the
  966. assembler reader. It never happends when generating
  967. assembler }
  968. end;
  969. top_shifterop:
  970. begin
  971. ot:=OT_SHIFTEROP;
  972. end;
  973. else
  974. internalerror(200402261);
  975. end;
  976. end;
  977. end;
  978. function taicpu.Matches(p:PInsEntry):longint;
  979. { * IF_SM stands for Size Match: any operand whose size is not
  980. * explicitly specified by the template is `really' intended to be
  981. * the same size as the first size-specified operand.
  982. * Non-specification is tolerated in the input instruction, but
  983. * _wrong_ specification is not.
  984. *
  985. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  986. * three-operand instructions such as SHLD: it implies that the
  987. * first two operands must match in size, but that the third is
  988. * required to be _unspecified_.
  989. *
  990. * IF_SB invokes Size Byte: operands with unspecified size in the
  991. * template are really bytes, and so no non-byte specification in
  992. * the input instruction will be tolerated. IF_SW similarly invokes
  993. * Size Word, and IF_SD invokes Size Doubleword.
  994. *
  995. * (The default state if neither IF_SM nor IF_SM2 is specified is
  996. * that any operand with unspecified size in the template is
  997. * required to have unspecified size in the instruction too...)
  998. }
  999. var
  1000. i,j,asize,oprs : longint;
  1001. siz : array[0..3] of longint;
  1002. begin
  1003. Matches:=100;
  1004. writeln(getstring,'---');
  1005. { Check the opcode and operands }
  1006. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1007. begin
  1008. Matches:=0;
  1009. exit;
  1010. end;
  1011. { Check that no spurious colons or TOs are present }
  1012. for i:=0 to p^.ops-1 do
  1013. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1014. begin
  1015. Matches:=0;
  1016. exit;
  1017. end;
  1018. { Check that the operand flags all match up }
  1019. for i:=0 to p^.ops-1 do
  1020. begin
  1021. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1022. ((p^.optypes[i] and OT_SIZE_MASK) and
  1023. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1024. begin
  1025. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1026. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1027. begin
  1028. Matches:=0;
  1029. exit;
  1030. end
  1031. else
  1032. Matches:=1;
  1033. end;
  1034. end;
  1035. { check postfixes:
  1036. the existance of a certain postfix requires a
  1037. particular code }
  1038. { update condition flags
  1039. or floating point single }
  1040. if (oppostfix=PF_S) and
  1041. not(p^.code[0] in []) then
  1042. begin
  1043. Matches:=0;
  1044. exit;
  1045. end;
  1046. { floating point size }
  1047. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1048. not(p^.code[0] in []) then
  1049. begin
  1050. Matches:=0;
  1051. exit;
  1052. end;
  1053. { multiple load/store address modes }
  1054. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1055. not(p^.code[0] in [
  1056. // stm,ldm
  1057. #$26
  1058. ]) then
  1059. begin
  1060. Matches:=0;
  1061. exit;
  1062. end;
  1063. { we shouldn't see any opsize prefixes here }
  1064. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1065. begin
  1066. Matches:=0;
  1067. exit;
  1068. end;
  1069. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1070. begin
  1071. Matches:=0;
  1072. exit;
  1073. end;
  1074. { Check operand sizes }
  1075. { as default an untyped size can get all the sizes, this is different
  1076. from nasm, but else we need to do a lot checking which opcodes want
  1077. size or not with the automatic size generation }
  1078. asize:=longint($ffffffff);
  1079. (*
  1080. if (p^.flags and IF_SB)<>0 then
  1081. asize:=OT_BITS8
  1082. else if (p^.flags and IF_SW)<>0 then
  1083. asize:=OT_BITS16
  1084. else if (p^.flags and IF_SD)<>0 then
  1085. asize:=OT_BITS32;
  1086. if (p^.flags and IF_ARMASK)<>0 then
  1087. begin
  1088. siz[0]:=0;
  1089. siz[1]:=0;
  1090. siz[2]:=0;
  1091. if (p^.flags and IF_AR0)<>0 then
  1092. siz[0]:=asize
  1093. else if (p^.flags and IF_AR1)<>0 then
  1094. siz[1]:=asize
  1095. else if (p^.flags and IF_AR2)<>0 then
  1096. siz[2]:=asize;
  1097. end
  1098. else
  1099. begin
  1100. { we can leave because the size for all operands is forced to be
  1101. the same
  1102. but not if IF_SB IF_SW or IF_SD is set PM }
  1103. if asize=-1 then
  1104. exit;
  1105. siz[0]:=asize;
  1106. siz[1]:=asize;
  1107. siz[2]:=asize;
  1108. end;
  1109. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1110. begin
  1111. if (p^.flags and IF_SM2)<>0 then
  1112. oprs:=2
  1113. else
  1114. oprs:=p^.ops;
  1115. for i:=0 to oprs-1 do
  1116. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1117. begin
  1118. for j:=0 to oprs-1 do
  1119. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1120. break;
  1121. end;
  1122. end
  1123. else
  1124. oprs:=2;
  1125. { Check operand sizes }
  1126. for i:=0 to p^.ops-1 do
  1127. begin
  1128. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1129. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1130. { Immediates can always include smaller size }
  1131. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1132. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1133. Matches:=2;
  1134. end;
  1135. *)
  1136. end;
  1137. function taicpu.calcsize(p:PInsEntry):shortint;
  1138. begin
  1139. end;
  1140. procedure taicpu.gencode(objdata:TAsmObjectData);
  1141. begin
  1142. end;
  1143. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1144. begin
  1145. end;
  1146. procedure taicpu.Swapoperands;
  1147. begin
  1148. end;
  1149. function taicpu.FindInsentry:boolean;
  1150. var
  1151. i : longint;
  1152. begin
  1153. result:=false;
  1154. { Things which may only be done once, not when a second pass is done to
  1155. optimize }
  1156. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1157. begin
  1158. { create the .ot fields }
  1159. create_ot;
  1160. { set the file postion }
  1161. aktfilepos:=fileinfo;
  1162. end
  1163. else
  1164. begin
  1165. { we've already an insentry so it's valid }
  1166. result:=true;
  1167. exit;
  1168. end;
  1169. { Lookup opcode in the table }
  1170. InsSize:=-1;
  1171. i:=instabcache^[opcode];
  1172. if i=-1 then
  1173. begin
  1174. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1175. exit;
  1176. end;
  1177. insentry:=@instab[i];
  1178. while (insentry^.opcode=opcode) do
  1179. begin
  1180. if matches(insentry)=100 then
  1181. begin
  1182. result:=true;
  1183. exit;
  1184. end;
  1185. inc(i);
  1186. insentry:=@instab[i];
  1187. end;
  1188. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1189. { No instruction found, set insentry to nil and inssize to -1 }
  1190. insentry:=nil;
  1191. inssize:=-1;
  1192. end;
  1193. end.
  1194. {$ifdef dummy}
  1195. (*
  1196. static void gencode (long segment, long offset, int bits,
  1197. insn *ins, char *codes, long insn_end)
  1198. {
  1199. int has_S_code; /* S - setflag */
  1200. int has_B_code; /* B - setflag */
  1201. int has_T_code; /* T - setflag */
  1202. int has_W_code; /* ! => W flag */
  1203. int has_F_code; /* ^ => S flag */
  1204. int keep;
  1205. unsigned char c;
  1206. unsigned char bytes[4];
  1207. long data, size;
  1208. static int cc_code[] = /* bit pattern of cc */
  1209. { /* order as enum in */
  1210. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1211. 0x0A, 0x0C, 0x08, 0x0D,
  1212. 0x09, 0x0B, 0x04, 0x01,
  1213. 0x05, 0x07, 0x06,
  1214. };
  1215. (*
  1216. #ifdef DEBUG
  1217. static char *CC[] =
  1218. { /* condition code names */
  1219. "AL", "CC", "CS", "EQ",
  1220. "GE", "GT", "HI", "LE",
  1221. "LS", "LT", "MI", "NE",
  1222. "PL", "VC", "VS", "",
  1223. "S"
  1224. };
  1225. *)
  1226. has_S_code = (ins->condition & C_SSETFLAG);
  1227. has_B_code = (ins->condition & C_BSETFLAG);
  1228. has_T_code = (ins->condition & C_TSETFLAG);
  1229. has_W_code = (ins->condition & C_EXSETFLAG);
  1230. has_F_code = (ins->condition & C_FSETFLAG);
  1231. ins->condition = (ins->condition & 0x0F);
  1232. (*
  1233. if (rt_debug)
  1234. {
  1235. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1236. CC[ins->condition & 0x0F]);
  1237. if (has_S_code)
  1238. printf ("S");
  1239. if (has_B_code)
  1240. printf ("B");
  1241. if (has_T_code)
  1242. printf ("T");
  1243. if (has_W_code)
  1244. printf ("!");
  1245. if (has_F_code)
  1246. printf ("^");
  1247. printf ("\n");
  1248. c = *codes;
  1249. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1250. bytes[0] = 0xB;
  1251. bytes[1] = 0xE;
  1252. bytes[2] = 0xE;
  1253. bytes[3] = 0xF;
  1254. }
  1255. *)
  1256. // First condition code in upper nibble
  1257. if (ins->condition < C_NONE)
  1258. {
  1259. c = cc_code[ins->condition] << 4;
  1260. }
  1261. else
  1262. {
  1263. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1264. }
  1265. switch (keep = *codes)
  1266. {
  1267. case 1:
  1268. // B, BL
  1269. ++codes;
  1270. c |= *codes++;
  1271. bytes[0] = c;
  1272. if (ins->oprs[0].segment != segment)
  1273. {
  1274. // fais une relocation
  1275. c = 1;
  1276. data = 0; // Let the linker locate ??
  1277. }
  1278. else
  1279. {
  1280. c = 0;
  1281. data = ins->oprs[0].offset - (offset + 8);
  1282. if (data % 4)
  1283. {
  1284. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1285. }
  1286. }
  1287. if (data >= 0x1000)
  1288. {
  1289. errfunc (ERR_NONFATAL, "too long offset");
  1290. }
  1291. data = data >> 2;
  1292. bytes[1] = (data >> 16) & 0xFF;
  1293. bytes[2] = (data >> 8) & 0xFF;
  1294. bytes[3] = (data ) & 0xFF;
  1295. if (c == 1)
  1296. {
  1297. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1298. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1299. }
  1300. else
  1301. {
  1302. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1303. }
  1304. return;
  1305. case 2:
  1306. // SWI
  1307. ++codes;
  1308. c |= *codes++;
  1309. bytes[0] = c;
  1310. data = ins->oprs[0].offset;
  1311. bytes[1] = (data >> 16) & 0xFF;
  1312. bytes[2] = (data >> 8) & 0xFF;
  1313. bytes[3] = (data) & 0xFF;
  1314. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1315. return;
  1316. case 3:
  1317. // BX
  1318. ++codes;
  1319. c |= *codes++;
  1320. bytes[0] = c;
  1321. bytes[1] = *codes++;
  1322. bytes[2] = *codes++;
  1323. bytes[3] = *codes++;
  1324. c = regval (&ins->oprs[0],1);
  1325. if (c == 15) // PC
  1326. {
  1327. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1328. }
  1329. else if (c > 15)
  1330. {
  1331. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1332. }
  1333. bytes[3] |= (c & 0x0F);
  1334. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1335. return;
  1336. case 4: // AND Rd,Rn,Rm
  1337. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1338. case 6: // AND Rd,Rn,Rm,<shift>imm
  1339. case 7: // AND Rd,Rn,<shift>imm
  1340. ++codes;
  1341. #ifdef DEBUG
  1342. if (rt_debug)
  1343. {
  1344. printf (" decode - '0x%02X'\n", keep);
  1345. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1346. }
  1347. #endif
  1348. bytes[0] = c | *codes;
  1349. ++codes;
  1350. bytes[1] = *codes;
  1351. if (has_S_code)
  1352. bytes[1] |= 0x10;
  1353. c = regval (&ins->oprs[1],1);
  1354. // Rn in low nibble
  1355. bytes[1] |= c;
  1356. // Rd in high nibble
  1357. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1358. if (keep != 7)
  1359. {
  1360. // Rm in low nibble
  1361. bytes[3] = regval (&ins->oprs[2],1);
  1362. }
  1363. // Shifts if any
  1364. if (keep == 5 || keep == 6)
  1365. {
  1366. // Shift in bytes 2 and 3
  1367. if (keep == 5)
  1368. {
  1369. // Rs
  1370. c = regval (&ins->oprs[3],1);
  1371. bytes[2] |= c;
  1372. c = 0x10; // Set bit 4 in byte[3]
  1373. }
  1374. if (keep == 6)
  1375. {
  1376. c = (ins->oprs[3].offset) & 0x1F;
  1377. // #imm
  1378. bytes[2] |= c >> 1;
  1379. if (c & 0x01)
  1380. {
  1381. bytes[3] |= 0x80;
  1382. }
  1383. c = 0; // Clr bit 4 in byte[3]
  1384. }
  1385. // <shift>
  1386. c |= shiftval (&ins->oprs[3]) << 5;
  1387. bytes[3] |= c;
  1388. }
  1389. // reg,reg,imm
  1390. if (keep == 7)
  1391. {
  1392. int shimm;
  1393. shimm = imm_shift (ins->oprs[2].offset);
  1394. if (shimm == -1)
  1395. {
  1396. errfunc (ERR_NONFATAL, "cannot create that constant");
  1397. }
  1398. bytes[3] = shimm & 0xFF;
  1399. bytes[2] |= (shimm & 0xF00) >> 8;
  1400. }
  1401. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1402. return;
  1403. case 8: // MOV Rd,Rm
  1404. case 9: // MOV Rd,Rm,<shift>Rs
  1405. case 0xA: // MOV Rd,Rm,<shift>imm
  1406. case 0xB: // MOV Rd,<shift>imm
  1407. ++codes;
  1408. #ifdef DEBUG
  1409. if (rt_debug)
  1410. {
  1411. printf (" decode - '0x%02X'\n", keep);
  1412. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1413. }
  1414. #endif
  1415. bytes[0] = c | *codes;
  1416. ++codes;
  1417. bytes[1] = *codes;
  1418. if (has_S_code)
  1419. bytes[1] |= 0x10;
  1420. // Rd in high nibble
  1421. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1422. if (keep != 0x0B)
  1423. {
  1424. // Rm in low nibble
  1425. bytes[3] = regval (&ins->oprs[1],1);
  1426. }
  1427. // Shifts if any
  1428. if (keep == 0x09 || keep == 0x0A)
  1429. {
  1430. // Shift in bytes 2 and 3
  1431. if (keep == 0x09)
  1432. {
  1433. // Rs
  1434. c = regval (&ins->oprs[2],1);
  1435. bytes[2] |= c;
  1436. c = 0x10; // Set bit 4 in byte[3]
  1437. }
  1438. if (keep == 0x0A)
  1439. {
  1440. c = (ins->oprs[2].offset) & 0x1F;
  1441. // #imm
  1442. bytes[2] |= c >> 1;
  1443. if (c & 0x01)
  1444. {
  1445. bytes[3] |= 0x80;
  1446. }
  1447. c = 0; // Clr bit 4 in byte[3]
  1448. }
  1449. // <shift>
  1450. c |= shiftval (&ins->oprs[2]) << 5;
  1451. bytes[3] |= c;
  1452. }
  1453. // reg,imm
  1454. if (keep == 0x0B)
  1455. {
  1456. int shimm;
  1457. shimm = imm_shift (ins->oprs[1].offset);
  1458. if (shimm == -1)
  1459. {
  1460. errfunc (ERR_NONFATAL, "cannot create that constant");
  1461. }
  1462. bytes[3] = shimm & 0xFF;
  1463. bytes[2] |= (shimm & 0xF00) >> 8;
  1464. }
  1465. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1466. return;
  1467. case 0xC: // CMP Rn,Rm
  1468. case 0xD: // CMP Rn,Rm,<shift>Rs
  1469. case 0xE: // CMP Rn,Rm,<shift>imm
  1470. case 0xF: // CMP Rn,<shift>imm
  1471. ++codes;
  1472. bytes[0] = c | *codes++;
  1473. bytes[1] = *codes;
  1474. // Implicit S code
  1475. bytes[1] |= 0x10;
  1476. c = regval (&ins->oprs[0],1);
  1477. // Rn in low nibble
  1478. bytes[1] |= c;
  1479. // No destination
  1480. bytes[2] = 0;
  1481. if (keep != 0x0B)
  1482. {
  1483. // Rm in low nibble
  1484. bytes[3] = regval (&ins->oprs[1],1);
  1485. }
  1486. // Shifts if any
  1487. if (keep == 0x0D || keep == 0x0E)
  1488. {
  1489. // Shift in bytes 2 and 3
  1490. if (keep == 0x0D)
  1491. {
  1492. // Rs
  1493. c = regval (&ins->oprs[2],1);
  1494. bytes[2] |= c;
  1495. c = 0x10; // Set bit 4 in byte[3]
  1496. }
  1497. if (keep == 0x0E)
  1498. {
  1499. c = (ins->oprs[2].offset) & 0x1F;
  1500. // #imm
  1501. bytes[2] |= c >> 1;
  1502. if (c & 0x01)
  1503. {
  1504. bytes[3] |= 0x80;
  1505. }
  1506. c = 0; // Clr bit 4 in byte[3]
  1507. }
  1508. // <shift>
  1509. c |= shiftval (&ins->oprs[2]) << 5;
  1510. bytes[3] |= c;
  1511. }
  1512. // reg,imm
  1513. if (keep == 0x0F)
  1514. {
  1515. int shimm;
  1516. shimm = imm_shift (ins->oprs[1].offset);
  1517. if (shimm == -1)
  1518. {
  1519. errfunc (ERR_NONFATAL, "cannot create that constant");
  1520. }
  1521. bytes[3] = shimm & 0xFF;
  1522. bytes[2] |= (shimm & 0xF00) >> 8;
  1523. }
  1524. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1525. return;
  1526. case 0x10: // MRS Rd,<psr>
  1527. ++codes;
  1528. bytes[0] = c | *codes++;
  1529. bytes[1] = *codes++;
  1530. // Rd
  1531. c = regval (&ins->oprs[0],1);
  1532. bytes[2] = c << 4;
  1533. bytes[3] = 0;
  1534. c = ins->oprs[1].basereg;
  1535. if (c == R_CPSR || c == R_SPSR)
  1536. {
  1537. if (c == R_SPSR)
  1538. {
  1539. bytes[1] |= 0x40;
  1540. }
  1541. }
  1542. else
  1543. {
  1544. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1545. }
  1546. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1547. return;
  1548. case 0x11: // MSR <psr>,Rm
  1549. case 0x12: // MSR <psrf>,Rm
  1550. case 0x13: // MSR <psrf>,#expression
  1551. ++codes;
  1552. bytes[0] = c | *codes++;
  1553. bytes[1] = *codes++;
  1554. bytes[2] = *codes;
  1555. if (keep == 0x11 || keep == 0x12)
  1556. {
  1557. // Rm
  1558. c = regval (&ins->oprs[1],1);
  1559. bytes[3] = c;
  1560. }
  1561. else
  1562. {
  1563. int shimm;
  1564. shimm = imm_shift (ins->oprs[1].offset);
  1565. if (shimm == -1)
  1566. {
  1567. errfunc (ERR_NONFATAL, "cannot create that constant");
  1568. }
  1569. bytes[3] = shimm & 0xFF;
  1570. bytes[2] |= (shimm & 0xF00) >> 8;
  1571. }
  1572. c = ins->oprs[0].basereg;
  1573. if ( keep == 0x11)
  1574. {
  1575. if ( c == R_CPSR || c == R_SPSR)
  1576. {
  1577. if ( c== R_SPSR)
  1578. {
  1579. bytes[1] |= 0x40;
  1580. }
  1581. }
  1582. else
  1583. {
  1584. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1585. }
  1586. }
  1587. else
  1588. {
  1589. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  1590. {
  1591. if ( c== R_SPSR_FLG)
  1592. {
  1593. bytes[1] |= 0x40;
  1594. }
  1595. }
  1596. else
  1597. {
  1598. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  1599. }
  1600. }
  1601. break;
  1602. case 0x14: // MUL Rd,Rm,Rs
  1603. case 0x15: // MULA Rd,Rm,Rs,Rn
  1604. ++codes;
  1605. bytes[0] = c | *codes++;
  1606. bytes[1] = *codes++;
  1607. bytes[3] = *codes;
  1608. // Rd
  1609. bytes[1] |= regval (&ins->oprs[0],1);
  1610. if (has_S_code)
  1611. bytes[1] |= 0x10;
  1612. // Rm
  1613. bytes[3] |= regval (&ins->oprs[1],1);
  1614. // Rs
  1615. bytes[2] = regval (&ins->oprs[2],1);
  1616. if (keep == 0x15)
  1617. {
  1618. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  1619. }
  1620. break;
  1621. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  1622. ++codes;
  1623. bytes[0] = c | *codes++;
  1624. bytes[1] = *codes++;
  1625. bytes[3] = *codes;
  1626. // RdHi
  1627. bytes[1] |= regval (&ins->oprs[1],1);
  1628. if (has_S_code)
  1629. bytes[1] |= 0x10;
  1630. // RdLo
  1631. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1632. // Rm
  1633. bytes[3] |= regval (&ins->oprs[2],1);
  1634. // Rs
  1635. bytes[2] |= regval (&ins->oprs[3],1);
  1636. break;
  1637. case 0x17: // LDR Rd, expression
  1638. ++codes;
  1639. bytes[0] = c | *codes++;
  1640. bytes[1] = *codes++;
  1641. // Rd
  1642. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1643. if (has_B_code)
  1644. bytes[1] |= 0x40;
  1645. if (has_T_code)
  1646. {
  1647. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1648. }
  1649. if (has_W_code)
  1650. {
  1651. errfunc (ERR_NONFATAL, "'!' not allowed");
  1652. }
  1653. // Rn - implicit R15
  1654. bytes[1] |= 0xF;
  1655. if (ins->oprs[1].segment != segment)
  1656. {
  1657. errfunc (ERR_NONFATAL, "label not in same segment");
  1658. }
  1659. data = ins->oprs[1].offset - (offset + 8);
  1660. if (data < 0)
  1661. {
  1662. data = -data;
  1663. }
  1664. else
  1665. {
  1666. bytes[1] |= 0x80;
  1667. }
  1668. if (data >= 0x1000)
  1669. {
  1670. errfunc (ERR_NONFATAL, "too long offset");
  1671. }
  1672. bytes[2] |= ((data & 0xF00) >> 8);
  1673. bytes[3] = data & 0xFF;
  1674. break;
  1675. case 0x18: // LDR Rd, [Rn]
  1676. ++codes;
  1677. bytes[0] = c | *codes++;
  1678. bytes[1] = *codes++;
  1679. // Rd
  1680. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1681. if (has_B_code)
  1682. bytes[1] |= 0x40;
  1683. if (has_T_code)
  1684. {
  1685. bytes[1] |= 0x20; // write-back
  1686. }
  1687. else
  1688. {
  1689. bytes[0] |= 0x01; // implicit pre-index mode
  1690. }
  1691. if (has_W_code)
  1692. {
  1693. bytes[1] |= 0x20; // write-back
  1694. }
  1695. // Rn
  1696. c = regval (&ins->oprs[1],1);
  1697. bytes[1] |= c;
  1698. if (c == 0x15) // R15
  1699. data = -8;
  1700. else
  1701. data = 0;
  1702. if (data < 0)
  1703. {
  1704. data = -data;
  1705. }
  1706. else
  1707. {
  1708. bytes[1] |= 0x80;
  1709. }
  1710. bytes[2] |= ((data & 0xF00) >> 8);
  1711. bytes[3] = data & 0xFF;
  1712. break;
  1713. case 0x19: // LDR Rd, [Rn,#expression]
  1714. case 0x20: // LDR Rd, [Rn,Rm]
  1715. case 0x21: // LDR Rd, [Rn,Rm,shift]
  1716. ++codes;
  1717. bytes[0] = c | *codes++;
  1718. bytes[1] = *codes++;
  1719. // Rd
  1720. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1721. if (has_B_code)
  1722. bytes[1] |= 0x40;
  1723. // Rn
  1724. c = regval (&ins->oprs[1],1);
  1725. bytes[1] |= c;
  1726. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1727. {
  1728. bytes[0] |= 0x01; // pre-index mode
  1729. if (has_W_code)
  1730. {
  1731. bytes[1] |= 0x20;
  1732. }
  1733. if (has_T_code)
  1734. {
  1735. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1736. }
  1737. }
  1738. else
  1739. {
  1740. if (has_T_code) // Forced write-back in post-index mode
  1741. {
  1742. bytes[1] |= 0x20;
  1743. }
  1744. if (has_W_code)
  1745. {
  1746. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1747. }
  1748. }
  1749. if (keep == 0x19)
  1750. {
  1751. data = ins->oprs[2].offset;
  1752. if (data < 0)
  1753. {
  1754. data = -data;
  1755. }
  1756. else
  1757. {
  1758. bytes[1] |= 0x80;
  1759. }
  1760. if (data >= 0x1000)
  1761. {
  1762. errfunc (ERR_NONFATAL, "too long offset");
  1763. }
  1764. bytes[2] |= ((data & 0xF00) >> 8);
  1765. bytes[3] = data & 0xFF;
  1766. }
  1767. else
  1768. {
  1769. if (ins->oprs[2].minus == 0)
  1770. {
  1771. bytes[1] |= 0x80;
  1772. }
  1773. c = regval (&ins->oprs[2],1);
  1774. bytes[3] = c;
  1775. if (keep == 0x21)
  1776. {
  1777. c = ins->oprs[3].offset;
  1778. if (c > 0x1F)
  1779. {
  1780. errfunc (ERR_NONFATAL, "too large shiftvalue");
  1781. c = c & 0x1F;
  1782. }
  1783. bytes[2] |= c >> 1;
  1784. if (c & 0x01)
  1785. {
  1786. bytes[3] |= 0x80;
  1787. }
  1788. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  1789. }
  1790. }
  1791. break;
  1792. case 0x22: // LDRH Rd, expression
  1793. ++codes;
  1794. bytes[0] = c | 0x01; // Implicit pre-index
  1795. bytes[1] = *codes++;
  1796. // Rd
  1797. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1798. // Rn - implicit R15
  1799. bytes[1] |= 0xF;
  1800. if (ins->oprs[1].segment != segment)
  1801. {
  1802. errfunc (ERR_NONFATAL, "label not in same segment");
  1803. }
  1804. data = ins->oprs[1].offset - (offset + 8);
  1805. if (data < 0)
  1806. {
  1807. data = -data;
  1808. }
  1809. else
  1810. {
  1811. bytes[1] |= 0x80;
  1812. }
  1813. if (data >= 0x100)
  1814. {
  1815. errfunc (ERR_NONFATAL, "too long offset");
  1816. }
  1817. bytes[3] = *codes++;
  1818. bytes[2] |= ((data & 0xF0) >> 4);
  1819. bytes[3] |= data & 0xF;
  1820. break;
  1821. case 0x23: // LDRH Rd, Rn
  1822. ++codes;
  1823. bytes[0] = c | 0x01; // Implicit pre-index
  1824. bytes[1] = *codes++;
  1825. // Rd
  1826. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1827. // Rn
  1828. c = regval (&ins->oprs[1],1);
  1829. bytes[1] |= c;
  1830. if (c == 0x15) // R15
  1831. data = -8;
  1832. else
  1833. data = 0;
  1834. if (data < 0)
  1835. {
  1836. data = -data;
  1837. }
  1838. else
  1839. {
  1840. bytes[1] |= 0x80;
  1841. }
  1842. if (data >= 0x100)
  1843. {
  1844. errfunc (ERR_NONFATAL, "too long offset");
  1845. }
  1846. bytes[3] = *codes++;
  1847. bytes[2] |= ((data & 0xF0) >> 4);
  1848. bytes[3] |= data & 0xF;
  1849. break;
  1850. case 0x24: // LDRH Rd, Rn, expression
  1851. case 0x25: // LDRH Rd, Rn, Rm
  1852. ++codes;
  1853. bytes[0] = c;
  1854. bytes[1] = *codes++;
  1855. // Rd
  1856. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1857. // Rn
  1858. c = regval (&ins->oprs[1],1);
  1859. bytes[1] |= c;
  1860. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1861. {
  1862. bytes[0] |= 0x01; // pre-index mode
  1863. if (has_W_code)
  1864. {
  1865. bytes[1] |= 0x20;
  1866. }
  1867. }
  1868. else
  1869. {
  1870. if (has_W_code)
  1871. {
  1872. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1873. }
  1874. }
  1875. bytes[3] = *codes++;
  1876. if (keep == 0x24)
  1877. {
  1878. data = ins->oprs[2].offset;
  1879. if (data < 0)
  1880. {
  1881. data = -data;
  1882. }
  1883. else
  1884. {
  1885. bytes[1] |= 0x80;
  1886. }
  1887. if (data >= 0x100)
  1888. {
  1889. errfunc (ERR_NONFATAL, "too long offset");
  1890. }
  1891. bytes[2] |= ((data & 0xF0) >> 4);
  1892. bytes[3] |= data & 0xF;
  1893. }
  1894. else
  1895. {
  1896. if (ins->oprs[2].minus == 0)
  1897. {
  1898. bytes[1] |= 0x80;
  1899. }
  1900. c = regval (&ins->oprs[2],1);
  1901. bytes[3] |= c;
  1902. }
  1903. break;
  1904. case 0x26: // LDM/STM Rn, {reg-list}
  1905. ++codes;
  1906. bytes[0] = c;
  1907. bytes[0] |= ( *codes >> 4) & 0xF;
  1908. bytes[1] = ( *codes << 4) & 0xF0;
  1909. ++codes;
  1910. if (has_W_code)
  1911. {
  1912. bytes[1] |= 0x20;
  1913. }
  1914. if (has_F_code)
  1915. {
  1916. bytes[1] |= 0x40;
  1917. }
  1918. // Rn
  1919. bytes[1] |= regval (&ins->oprs[0],1);
  1920. data = ins->oprs[1].basereg;
  1921. bytes[2] = ((data >> 8) & 0xFF);
  1922. bytes[3] = (data & 0xFF);
  1923. break;
  1924. case 0x27: // SWP Rd, Rm, [Rn]
  1925. ++codes;
  1926. bytes[0] = c;
  1927. bytes[0] |= *codes++;
  1928. bytes[1] = regval (&ins->oprs[2],1);
  1929. if (has_B_code)
  1930. {
  1931. bytes[1] |= 0x40;
  1932. }
  1933. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1934. bytes[3] = *codes++;
  1935. bytes[3] |= regval (&ins->oprs[1],1);
  1936. break;
  1937. default:
  1938. errfunc (ERR_FATAL, "unknown decoding of instruction");
  1939. bytes[0] = c;
  1940. // And a fix nibble
  1941. ++codes;
  1942. bytes[0] |= *codes++;
  1943. if ( *codes == 0x01) // An I bit
  1944. {
  1945. }
  1946. if ( *codes == 0x02) // An I bit
  1947. {
  1948. }
  1949. ++codes;
  1950. }
  1951. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1952. }
  1953. *)
  1954. {$endif dummy
  1955. }