aasmcpu.pas 73 KB

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