aasmcpu.pas 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  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,aasmsym,
  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_sym)
  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. else
  584. if curtai.typ=ait_const then
  585. inc(curpos);
  586. { split only at real instructions else the test below fails }
  587. if ((curpos-lastpos)>1016) and (curtai.typ=ait_instruction) and
  588. (
  589. { don't split loads of pc to lr and the following move }
  590. not(
  591. (taicpu(curtai).opcode=A_MOV) and
  592. (taicpu(curtai).oper[0]^.typ=top_reg) and
  593. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  594. (taicpu(curtai).oper[1]^.typ=top_reg) and
  595. (taicpu(curtai).oper[1]^.reg=NR_PC)
  596. )
  597. ) then
  598. begin
  599. lastpos:=curpos;
  600. hp:=tai(curtai.next);
  601. current_asmdata.getjumplabel(l);
  602. curdata.insert(taicpu.op_sym(A_B,l));
  603. curdata.concat(tai_label.create(l));
  604. list.insertlistafter(curtai,curdata);
  605. curtai:=hp;
  606. end
  607. else
  608. curtai:=tai(curtai.next);
  609. end;
  610. list.concatlist(curdata);
  611. curdata.free;
  612. end;
  613. procedure InsertPData;
  614. var
  615. prolog: TAsmList;
  616. begin
  617. prolog:=TAsmList.create;
  618. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(aint),secorder_begin);
  619. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  620. prolog.concat(Tai_const.Create_32bit(0));
  621. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  622. { dummy function }
  623. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  624. current_asmdata.asmlists[al_start].insertList(prolog);
  625. prolog.Free;
  626. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(aint));
  627. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  628. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit($ffffff01));
  629. end;
  630. (*
  631. Floating point instruction format information, taken from the linux kernel
  632. ARM Floating Point Instruction Classes
  633. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  634. |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
  635. |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)
  636. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  637. |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
  638. |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
  639. |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
  640. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  641. CPDT data transfer instructions
  642. LDF, STF, LFM (copro 2), SFM (copro 2)
  643. CPDO dyadic arithmetic instructions
  644. ADF, MUF, SUF, RSF, DVF, RDF,
  645. POW, RPW, RMF, FML, FDV, FRD, POL
  646. CPDO monadic arithmetic instructions
  647. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  648. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  649. CPRT joint arithmetic/data transfer instructions
  650. FIX (arithmetic followed by load/store)
  651. FLT (load/store followed by arithmetic)
  652. CMF, CNF CMFE, CNFE (comparisons)
  653. WFS, RFS (write/read floating point status register)
  654. WFC, RFC (write/read floating point control register)
  655. cond condition codes
  656. P pre/post index bit: 0 = postindex, 1 = preindex
  657. U up/down bit: 0 = stack grows down, 1 = stack grows up
  658. W write back bit: 1 = update base register (Rn)
  659. L load/store bit: 0 = store, 1 = load
  660. Rn base register
  661. Rd destination/source register
  662. Fd floating point destination register
  663. Fn floating point source register
  664. Fm floating point source register or floating point constant
  665. uv transfer length (TABLE 1)
  666. wx register count (TABLE 2)
  667. abcd arithmetic opcode (TABLES 3 & 4)
  668. ef destination size (rounding precision) (TABLE 5)
  669. gh rounding mode (TABLE 6)
  670. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  671. i constant bit: 1 = constant (TABLE 6)
  672. */
  673. /*
  674. TABLE 1
  675. +-------------------------+---+---+---------+---------+
  676. | Precision | u | v | FPSR.EP | length |
  677. +-------------------------+---+---+---------+---------+
  678. | Single | 0 | 0 | x | 1 words |
  679. | Double | 1 | 1 | x | 2 words |
  680. | Extended | 1 | 1 | x | 3 words |
  681. | Packed decimal | 1 | 1 | 0 | 3 words |
  682. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  683. +-------------------------+---+---+---------+---------+
  684. Note: x = don't care
  685. */
  686. /*
  687. TABLE 2
  688. +---+---+---------------------------------+
  689. | w | x | Number of registers to transfer |
  690. +---+---+---------------------------------+
  691. | 0 | 1 | 1 |
  692. | 1 | 0 | 2 |
  693. | 1 | 1 | 3 |
  694. | 0 | 0 | 4 |
  695. +---+---+---------------------------------+
  696. */
  697. /*
  698. TABLE 3: Dyadic Floating Point Opcodes
  699. +---+---+---+---+----------+-----------------------+-----------------------+
  700. | a | b | c | d | Mnemonic | Description | Operation |
  701. +---+---+---+---+----------+-----------------------+-----------------------+
  702. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  703. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  704. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  705. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  706. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  707. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  708. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  709. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  710. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  711. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  712. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  713. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  714. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  715. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  716. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  717. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  718. +---+---+---+---+----------+-----------------------+-----------------------+
  719. Note: POW, RPW, POL are deprecated, and are available for backwards
  720. compatibility only.
  721. */
  722. /*
  723. TABLE 4: Monadic Floating Point Opcodes
  724. +---+---+---+---+----------+-----------------------+-----------------------+
  725. | a | b | c | d | Mnemonic | Description | Operation |
  726. +---+---+---+---+----------+-----------------------+-----------------------+
  727. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  728. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  729. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  730. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  731. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  732. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  733. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  734. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  735. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  736. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  737. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  738. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  739. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  740. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  741. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  742. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  743. +---+---+---+---+----------+-----------------------+-----------------------+
  744. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  745. available for backwards compatibility only.
  746. */
  747. /*
  748. TABLE 5
  749. +-------------------------+---+---+
  750. | Rounding Precision | e | f |
  751. +-------------------------+---+---+
  752. | IEEE Single precision | 0 | 0 |
  753. | IEEE Double precision | 0 | 1 |
  754. | IEEE Extended precision | 1 | 0 |
  755. | undefined (trap) | 1 | 1 |
  756. +-------------------------+---+---+
  757. */
  758. /*
  759. TABLE 5
  760. +---------------------------------+---+---+
  761. | Rounding Mode | g | h |
  762. +---------------------------------+---+---+
  763. | Round to nearest (default) | 0 | 0 |
  764. | Round toward plus infinity | 0 | 1 |
  765. | Round toward negative infinity | 1 | 0 |
  766. | Round toward zero | 1 | 1 |
  767. +---------------------------------+---+---+
  768. *)
  769. function taicpu.GetString:string;
  770. var
  771. i : longint;
  772. s : string;
  773. addsize : boolean;
  774. begin
  775. s:='['+gas_op2str[opcode];
  776. for i:=0 to ops-1 do
  777. begin
  778. with oper[i]^ do
  779. begin
  780. if i=0 then
  781. s:=s+' '
  782. else
  783. s:=s+',';
  784. { type }
  785. addsize:=false;
  786. if (ot and OT_VREG)=OT_VREG then
  787. s:=s+'vreg'
  788. else
  789. if (ot and OT_FPUREG)=OT_FPUREG then
  790. s:=s+'fpureg'
  791. else
  792. if (ot and OT_REGISTER)=OT_REGISTER then
  793. begin
  794. s:=s+'reg';
  795. addsize:=true;
  796. end
  797. else
  798. if (ot and OT_REGLIST)=OT_REGLIST then
  799. begin
  800. s:=s+'reglist';
  801. addsize:=false;
  802. end
  803. else
  804. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  805. begin
  806. s:=s+'imm';
  807. addsize:=true;
  808. end
  809. else
  810. if (ot and OT_MEMORY)=OT_MEMORY then
  811. begin
  812. s:=s+'mem';
  813. addsize:=true;
  814. if (ot and OT_AM2)<>0 then
  815. s:=s+' am2 ';
  816. end
  817. else
  818. s:=s+'???';
  819. { size }
  820. if addsize then
  821. begin
  822. if (ot and OT_BITS8)<>0 then
  823. s:=s+'8'
  824. else
  825. if (ot and OT_BITS16)<>0 then
  826. s:=s+'24'
  827. else
  828. if (ot and OT_BITS32)<>0 then
  829. s:=s+'32'
  830. else
  831. if (ot and OT_BITSSHIFTER)<>0 then
  832. s:=s+'shifter'
  833. else
  834. s:=s+'??';
  835. { signed }
  836. if (ot and OT_SIGNED)<>0 then
  837. s:=s+'s';
  838. end;
  839. end;
  840. end;
  841. GetString:=s+']';
  842. end;
  843. procedure taicpu.ResetPass1;
  844. begin
  845. { we need to reset everything here, because the choosen insentry
  846. can be invalid for a new situation where the previously optimized
  847. insentry is not correct }
  848. InsEntry:=nil;
  849. InsSize:=0;
  850. LastInsOffset:=-1;
  851. end;
  852. procedure taicpu.ResetPass2;
  853. begin
  854. { we are here in a second pass, check if the instruction can be optimized }
  855. if assigned(InsEntry) and
  856. ((InsEntry^.flags and IF_PASS2)<>0) then
  857. begin
  858. InsEntry:=nil;
  859. InsSize:=0;
  860. end;
  861. LastInsOffset:=-1;
  862. end;
  863. function taicpu.CheckIfValid:boolean;
  864. begin
  865. end;
  866. function taicpu.Pass1(objdata:TObjData):longint;
  867. var
  868. ldr2op : array[PF_B..PF_T] of tasmop = (
  869. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  870. str2op : array[PF_B..PF_T] of tasmop = (
  871. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  872. begin
  873. Pass1:=0;
  874. { Save the old offset and set the new offset }
  875. InsOffset:=ObjData.CurrObjSec.Size;
  876. { Error? }
  877. if (Insentry=nil) and (InsSize=-1) then
  878. exit;
  879. { set the file postion }
  880. current_filepos:=fileinfo;
  881. { tranlate LDR+postfix to complete opcode }
  882. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  883. begin
  884. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  885. opcode:=ldr2op[oppostfix]
  886. else
  887. internalerror(2005091001);
  888. if opcode=A_None then
  889. internalerror(2005091004);
  890. { postfix has been added to opcode }
  891. oppostfix:=PF_None;
  892. end
  893. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  894. begin
  895. if (oppostfix in [low(str2op)..high(str2op)]) then
  896. opcode:=str2op[oppostfix]
  897. else
  898. internalerror(2005091002);
  899. if opcode=A_None then
  900. internalerror(2005091003);
  901. { postfix has been added to opcode }
  902. oppostfix:=PF_None;
  903. end;
  904. { Get InsEntry }
  905. if FindInsEntry(objdata) then
  906. begin
  907. InsSize:=4;
  908. LastInsOffset:=InsOffset;
  909. Pass1:=InsSize;
  910. exit;
  911. end;
  912. LastInsOffset:=-1;
  913. end;
  914. procedure taicpu.Pass2(objdata:TObjData);
  915. begin
  916. { error in pass1 ? }
  917. if insentry=nil then
  918. exit;
  919. current_filepos:=fileinfo;
  920. { Generate the instruction }
  921. GenCode(objdata);
  922. end;
  923. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  924. begin
  925. end;
  926. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  927. begin
  928. end;
  929. procedure taicpu.ppubuildderefimploper(var o:toper);
  930. begin
  931. end;
  932. procedure taicpu.ppuderefoper(var o:toper);
  933. begin
  934. end;
  935. function taicpu.InsEnd:longint;
  936. begin
  937. end;
  938. procedure taicpu.create_ot(objdata:TObjData);
  939. var
  940. i,l,relsize : longint;
  941. dummy : byte;
  942. currsym : TObjSymbol;
  943. begin
  944. if ops=0 then
  945. exit;
  946. { update oper[].ot field }
  947. for i:=0 to ops-1 do
  948. with oper[i]^ do
  949. begin
  950. case typ of
  951. top_regset:
  952. begin
  953. ot:=OT_REGLIST;
  954. end;
  955. top_reg :
  956. begin
  957. case getregtype(reg) of
  958. R_INTREGISTER:
  959. ot:=OT_REG32 or OT_SHIFTEROP;
  960. R_FPUREGISTER:
  961. ot:=OT_FPUREG;
  962. else
  963. internalerror(2005090901);
  964. end;
  965. end;
  966. top_ref :
  967. begin
  968. if ref^.refaddr=addr_no then
  969. begin
  970. { create ot field }
  971. { we should get the size here dependend on the
  972. instruction }
  973. if (ot and OT_SIZE_MASK)=0 then
  974. ot:=OT_MEMORY or OT_BITS32
  975. else
  976. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  977. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  978. ot:=ot or OT_MEM_OFFS;
  979. { if we need to fix a reference, we do it here }
  980. { pc relative addressing }
  981. if (ref^.base=NR_NO) and
  982. (ref^.index=NR_NO) and
  983. (ref^.shiftmode=SM_None)
  984. { at least we should check if the destination symbol
  985. is in a text section }
  986. { and
  987. (ref^.symbol^.owner="text") } then
  988. ref^.base:=NR_PC;
  989. { determine possible address modes }
  990. if (ref^.base<>NR_NO) and
  991. (
  992. (
  993. (ref^.index=NR_NO) and
  994. (ref^.shiftmode=SM_None) and
  995. (ref^.offset>=-4097) and
  996. (ref^.offset<=4097)
  997. ) or
  998. (
  999. (ref^.shiftmode=SM_None) and
  1000. (ref^.offset=0)
  1001. ) or
  1002. (
  1003. (ref^.index<>NR_NO) and
  1004. (ref^.shiftmode<>SM_None) and
  1005. (ref^.shiftimm<=31) and
  1006. (ref^.offset=0)
  1007. )
  1008. ) then
  1009. ot:=ot or OT_AM2;
  1010. if (ref^.index<>NR_NO) and
  1011. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1012. (
  1013. (ref^.base=NR_NO) and
  1014. (ref^.shiftmode=SM_None) and
  1015. (ref^.offset=0)
  1016. ) then
  1017. ot:=ot or OT_AM4;
  1018. end
  1019. else
  1020. begin
  1021. l:=ref^.offset;
  1022. currsym:=ObjData.symbolref(ref^.symbol);
  1023. if assigned(currsym) then
  1024. inc(l,currsym.address);
  1025. relsize:=(InsOffset+2)-l;
  1026. if (relsize<-33554428) or (relsize>33554428) then
  1027. ot:=OT_IMM32
  1028. else
  1029. ot:=OT_IMM24;
  1030. end;
  1031. end;
  1032. top_local :
  1033. begin
  1034. { we should get the size here dependend on the
  1035. instruction }
  1036. if (ot and OT_SIZE_MASK)=0 then
  1037. ot:=OT_MEMORY or OT_BITS32
  1038. else
  1039. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1040. end;
  1041. top_const :
  1042. begin
  1043. ot:=OT_IMMEDIATE;
  1044. if is_shifter_const(val,dummy) then
  1045. ot:=OT_IMMSHIFTER
  1046. else
  1047. ot:=OT_IMM32
  1048. end;
  1049. top_none :
  1050. begin
  1051. { generated when there was an error in the
  1052. assembler reader. It never happends when generating
  1053. assembler }
  1054. end;
  1055. top_shifterop:
  1056. begin
  1057. ot:=OT_SHIFTEROP;
  1058. end;
  1059. else
  1060. internalerror(200402261);
  1061. end;
  1062. end;
  1063. end;
  1064. function taicpu.Matches(p:PInsEntry):longint;
  1065. { * IF_SM stands for Size Match: any operand whose size is not
  1066. * explicitly specified by the template is `really' intended to be
  1067. * the same size as the first size-specified operand.
  1068. * Non-specification is tolerated in the input instruction, but
  1069. * _wrong_ specification is not.
  1070. *
  1071. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1072. * three-operand instructions such as SHLD: it implies that the
  1073. * first two operands must match in size, but that the third is
  1074. * required to be _unspecified_.
  1075. *
  1076. * IF_SB invokes Size Byte: operands with unspecified size in the
  1077. * template are really bytes, and so no non-byte specification in
  1078. * the input instruction will be tolerated. IF_SW similarly invokes
  1079. * Size Word, and IF_SD invokes Size Doubleword.
  1080. *
  1081. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1082. * that any operand with unspecified size in the template is
  1083. * required to have unspecified size in the instruction too...)
  1084. }
  1085. var
  1086. i,j,asize,oprs : longint;
  1087. siz : array[0..3] of longint;
  1088. begin
  1089. Matches:=100;
  1090. writeln(getstring,'---');
  1091. { Check the opcode and operands }
  1092. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1093. begin
  1094. Matches:=0;
  1095. exit;
  1096. end;
  1097. { Check that no spurious colons or TOs are present }
  1098. for i:=0 to p^.ops-1 do
  1099. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1100. begin
  1101. Matches:=0;
  1102. exit;
  1103. end;
  1104. { Check that the operand flags all match up }
  1105. for i:=0 to p^.ops-1 do
  1106. begin
  1107. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1108. ((p^.optypes[i] and OT_SIZE_MASK) and
  1109. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1110. begin
  1111. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1112. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1113. begin
  1114. Matches:=0;
  1115. exit;
  1116. end
  1117. else
  1118. Matches:=1;
  1119. end;
  1120. end;
  1121. { check postfixes:
  1122. the existance of a certain postfix requires a
  1123. particular code }
  1124. { update condition flags
  1125. or floating point single }
  1126. if (oppostfix=PF_S) and
  1127. not(p^.code[0] in [#$04]) then
  1128. begin
  1129. Matches:=0;
  1130. exit;
  1131. end;
  1132. { floating point size }
  1133. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1134. not(p^.code[0] in []) then
  1135. begin
  1136. Matches:=0;
  1137. exit;
  1138. end;
  1139. { multiple load/store address modes }
  1140. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1141. not(p^.code[0] in [
  1142. // ldr,str,ldrb,strb
  1143. #$17,
  1144. // stm,ldm
  1145. #$26
  1146. ]) then
  1147. begin
  1148. Matches:=0;
  1149. exit;
  1150. end;
  1151. { we shouldn't see any opsize prefixes here }
  1152. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1153. begin
  1154. Matches:=0;
  1155. exit;
  1156. end;
  1157. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1158. begin
  1159. Matches:=0;
  1160. exit;
  1161. end;
  1162. { Check operand sizes }
  1163. { as default an untyped size can get all the sizes, this is different
  1164. from nasm, but else we need to do a lot checking which opcodes want
  1165. size or not with the automatic size generation }
  1166. asize:=longint($ffffffff);
  1167. (*
  1168. if (p^.flags and IF_SB)<>0 then
  1169. asize:=OT_BITS8
  1170. else if (p^.flags and IF_SW)<>0 then
  1171. asize:=OT_BITS16
  1172. else if (p^.flags and IF_SD)<>0 then
  1173. asize:=OT_BITS32;
  1174. if (p^.flags and IF_ARMASK)<>0 then
  1175. begin
  1176. siz[0]:=0;
  1177. siz[1]:=0;
  1178. siz[2]:=0;
  1179. if (p^.flags and IF_AR0)<>0 then
  1180. siz[0]:=asize
  1181. else if (p^.flags and IF_AR1)<>0 then
  1182. siz[1]:=asize
  1183. else if (p^.flags and IF_AR2)<>0 then
  1184. siz[2]:=asize;
  1185. end
  1186. else
  1187. begin
  1188. { we can leave because the size for all operands is forced to be
  1189. the same
  1190. but not if IF_SB IF_SW or IF_SD is set PM }
  1191. if asize=-1 then
  1192. exit;
  1193. siz[0]:=asize;
  1194. siz[1]:=asize;
  1195. siz[2]:=asize;
  1196. end;
  1197. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1198. begin
  1199. if (p^.flags and IF_SM2)<>0 then
  1200. oprs:=2
  1201. else
  1202. oprs:=p^.ops;
  1203. for i:=0 to oprs-1 do
  1204. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1205. begin
  1206. for j:=0 to oprs-1 do
  1207. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1208. break;
  1209. end;
  1210. end
  1211. else
  1212. oprs:=2;
  1213. { Check operand sizes }
  1214. for i:=0 to p^.ops-1 do
  1215. begin
  1216. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1217. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1218. { Immediates can always include smaller size }
  1219. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1220. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1221. Matches:=2;
  1222. end;
  1223. *)
  1224. end;
  1225. function taicpu.calcsize(p:PInsEntry):shortint;
  1226. begin
  1227. result:=4;
  1228. end;
  1229. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1230. begin
  1231. end;
  1232. procedure taicpu.Swapoperands;
  1233. begin
  1234. end;
  1235. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1236. var
  1237. i : longint;
  1238. begin
  1239. result:=false;
  1240. { Things which may only be done once, not when a second pass is done to
  1241. optimize }
  1242. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1243. begin
  1244. { create the .ot fields }
  1245. create_ot(objdata);
  1246. { set the file postion }
  1247. current_filepos:=fileinfo;
  1248. end
  1249. else
  1250. begin
  1251. { we've already an insentry so it's valid }
  1252. result:=true;
  1253. exit;
  1254. end;
  1255. { Lookup opcode in the table }
  1256. InsSize:=-1;
  1257. i:=instabcache^[opcode];
  1258. if i=-1 then
  1259. begin
  1260. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1261. exit;
  1262. end;
  1263. insentry:=@instab[i];
  1264. while (insentry^.opcode=opcode) do
  1265. begin
  1266. if matches(insentry)=100 then
  1267. begin
  1268. result:=true;
  1269. exit;
  1270. end;
  1271. inc(i);
  1272. insentry:=@instab[i];
  1273. end;
  1274. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1275. { No instruction found, set insentry to nil and inssize to -1 }
  1276. insentry:=nil;
  1277. inssize:=-1;
  1278. end;
  1279. procedure taicpu.gencode(objdata:TObjData);
  1280. var
  1281. bytes : dword;
  1282. i_field : byte;
  1283. procedure setshifterop(op : byte);
  1284. begin
  1285. case oper[op]^.typ of
  1286. top_const:
  1287. begin
  1288. i_field:=1;
  1289. bytes:=bytes or (oper[op]^.val and $fff);
  1290. end;
  1291. top_reg:
  1292. begin
  1293. i_field:=0;
  1294. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1295. { does a real shifter op follow? }
  1296. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1297. begin
  1298. end;
  1299. end;
  1300. else
  1301. internalerror(2005091103);
  1302. end;
  1303. end;
  1304. begin
  1305. bytes:=$0;
  1306. { evaluate and set condition code }
  1307. { condition code allowed? }
  1308. { setup rest of the instruction }
  1309. case insentry^.code[0] of
  1310. #$08:
  1311. begin
  1312. { set instruction code }
  1313. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1314. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1315. { set destination }
  1316. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1317. { create shifter op }
  1318. setshifterop(1);
  1319. { set i field }
  1320. bytes:=bytes or (i_field shl 25);
  1321. { set s if necessary }
  1322. if oppostfix=PF_S then
  1323. bytes:=bytes or (1 shl 20);
  1324. end;
  1325. #$ff:
  1326. internalerror(2005091101);
  1327. else
  1328. internalerror(2005091102);
  1329. end;
  1330. { we're finished, write code }
  1331. objdata.writebytes(bytes,sizeof(bytes));
  1332. end;
  1333. end.
  1334. {$ifdef dummy}
  1335. (*
  1336. static void gencode (long segment, long offset, int bits,
  1337. insn *ins, char *codes, long insn_end)
  1338. {
  1339. int has_S_code; /* S - setflag */
  1340. int has_B_code; /* B - setflag */
  1341. int has_T_code; /* T - setflag */
  1342. int has_W_code; /* ! => W flag */
  1343. int has_F_code; /* ^ => S flag */
  1344. int keep;
  1345. unsigned char c;
  1346. unsigned char bytes[4];
  1347. long data, size;
  1348. static int cc_code[] = /* bit pattern of cc */
  1349. { /* order as enum in */
  1350. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1351. 0x0A, 0x0C, 0x08, 0x0D,
  1352. 0x09, 0x0B, 0x04, 0x01,
  1353. 0x05, 0x07, 0x06,
  1354. };
  1355. (*
  1356. #ifdef DEBUG
  1357. static char *CC[] =
  1358. { /* condition code names */
  1359. "AL", "CC", "CS", "EQ",
  1360. "GE", "GT", "HI", "LE",
  1361. "LS", "LT", "MI", "NE",
  1362. "PL", "VC", "VS", "",
  1363. "S"
  1364. };
  1365. *)
  1366. has_S_code = (ins->condition & C_SSETFLAG);
  1367. has_B_code = (ins->condition & C_BSETFLAG);
  1368. has_T_code = (ins->condition & C_TSETFLAG);
  1369. has_W_code = (ins->condition & C_EXSETFLAG);
  1370. has_F_code = (ins->condition & C_FSETFLAG);
  1371. ins->condition = (ins->condition & 0x0F);
  1372. (*
  1373. if (rt_debug)
  1374. {
  1375. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1376. CC[ins->condition & 0x0F]);
  1377. if (has_S_code)
  1378. printf ("S");
  1379. if (has_B_code)
  1380. printf ("B");
  1381. if (has_T_code)
  1382. printf ("T");
  1383. if (has_W_code)
  1384. printf ("!");
  1385. if (has_F_code)
  1386. printf ("^");
  1387. printf ("\n");
  1388. c = *codes;
  1389. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1390. bytes[0] = 0xB;
  1391. bytes[1] = 0xE;
  1392. bytes[2] = 0xE;
  1393. bytes[3] = 0xF;
  1394. }
  1395. *)
  1396. // First condition code in upper nibble
  1397. if (ins->condition < C_NONE)
  1398. {
  1399. c = cc_code[ins->condition] << 4;
  1400. }
  1401. else
  1402. {
  1403. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1404. }
  1405. switch (keep = *codes)
  1406. {
  1407. case 1:
  1408. // B, BL
  1409. ++codes;
  1410. c |= *codes++;
  1411. bytes[0] = c;
  1412. if (ins->oprs[0].segment != segment)
  1413. {
  1414. // fais une relocation
  1415. c = 1;
  1416. data = 0; // Let the linker locate ??
  1417. }
  1418. else
  1419. {
  1420. c = 0;
  1421. data = ins->oprs[0].offset - (offset + 8);
  1422. if (data % 4)
  1423. {
  1424. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1425. }
  1426. }
  1427. if (data >= 0x1000)
  1428. {
  1429. errfunc (ERR_NONFATAL, "too long offset");
  1430. }
  1431. data = data >> 2;
  1432. bytes[1] = (data >> 16) & 0xFF;
  1433. bytes[2] = (data >> 8) & 0xFF;
  1434. bytes[3] = (data ) & 0xFF;
  1435. if (c == 1)
  1436. {
  1437. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1438. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1439. }
  1440. else
  1441. {
  1442. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1443. }
  1444. return;
  1445. case 2:
  1446. // SWI
  1447. ++codes;
  1448. c |= *codes++;
  1449. bytes[0] = c;
  1450. data = ins->oprs[0].offset;
  1451. bytes[1] = (data >> 16) & 0xFF;
  1452. bytes[2] = (data >> 8) & 0xFF;
  1453. bytes[3] = (data) & 0xFF;
  1454. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1455. return;
  1456. case 3:
  1457. // BX
  1458. ++codes;
  1459. c |= *codes++;
  1460. bytes[0] = c;
  1461. bytes[1] = *codes++;
  1462. bytes[2] = *codes++;
  1463. bytes[3] = *codes++;
  1464. c = regval (&ins->oprs[0],1);
  1465. if (c == 15) // PC
  1466. {
  1467. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1468. }
  1469. else if (c > 15)
  1470. {
  1471. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1472. }
  1473. bytes[3] |= (c & 0x0F);
  1474. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1475. return;
  1476. case 4: // AND Rd,Rn,Rm
  1477. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1478. case 6: // AND Rd,Rn,Rm,<shift>imm
  1479. case 7: // AND Rd,Rn,<shift>imm
  1480. ++codes;
  1481. #ifdef DEBUG
  1482. if (rt_debug)
  1483. {
  1484. printf (" decode - '0x%02X'\n", keep);
  1485. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1486. }
  1487. #endif
  1488. bytes[0] = c | *codes;
  1489. ++codes;
  1490. bytes[1] = *codes;
  1491. if (has_S_code)
  1492. bytes[1] |= 0x10;
  1493. c = regval (&ins->oprs[1],1);
  1494. // Rn in low nibble
  1495. bytes[1] |= c;
  1496. // Rd in high nibble
  1497. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1498. if (keep != 7)
  1499. {
  1500. // Rm in low nibble
  1501. bytes[3] = regval (&ins->oprs[2],1);
  1502. }
  1503. // Shifts if any
  1504. if (keep == 5 || keep == 6)
  1505. {
  1506. // Shift in bytes 2 and 3
  1507. if (keep == 5)
  1508. {
  1509. // Rs
  1510. c = regval (&ins->oprs[3],1);
  1511. bytes[2] |= c;
  1512. c = 0x10; // Set bit 4 in byte[3]
  1513. }
  1514. if (keep == 6)
  1515. {
  1516. c = (ins->oprs[3].offset) & 0x1F;
  1517. // #imm
  1518. bytes[2] |= c >> 1;
  1519. if (c & 0x01)
  1520. {
  1521. bytes[3] |= 0x80;
  1522. }
  1523. c = 0; // Clr bit 4 in byte[3]
  1524. }
  1525. // <shift>
  1526. c |= shiftval (&ins->oprs[3]) << 5;
  1527. bytes[3] |= c;
  1528. }
  1529. // reg,reg,imm
  1530. if (keep == 7)
  1531. {
  1532. int shimm;
  1533. shimm = imm_shift (ins->oprs[2].offset);
  1534. if (shimm == -1)
  1535. {
  1536. errfunc (ERR_NONFATAL, "cannot create that constant");
  1537. }
  1538. bytes[3] = shimm & 0xFF;
  1539. bytes[2] |= (shimm & 0xF00) >> 8;
  1540. }
  1541. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1542. return;
  1543. case 8: // MOV Rd,Rm
  1544. case 9: // MOV Rd,Rm,<shift>Rs
  1545. case 0xA: // MOV Rd,Rm,<shift>imm
  1546. case 0xB: // MOV Rd,<shift>imm
  1547. ++codes;
  1548. #ifdef DEBUG
  1549. if (rt_debug)
  1550. {
  1551. printf (" decode - '0x%02X'\n", keep);
  1552. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1553. }
  1554. #endif
  1555. bytes[0] = c | *codes;
  1556. ++codes;
  1557. bytes[1] = *codes;
  1558. if (has_S_code)
  1559. bytes[1] |= 0x10;
  1560. // Rd in high nibble
  1561. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1562. if (keep != 0x0B)
  1563. {
  1564. // Rm in low nibble
  1565. bytes[3] = regval (&ins->oprs[1],1);
  1566. }
  1567. // Shifts if any
  1568. if (keep == 0x09 || keep == 0x0A)
  1569. {
  1570. // Shift in bytes 2 and 3
  1571. if (keep == 0x09)
  1572. {
  1573. // Rs
  1574. c = regval (&ins->oprs[2],1);
  1575. bytes[2] |= c;
  1576. c = 0x10; // Set bit 4 in byte[3]
  1577. }
  1578. if (keep == 0x0A)
  1579. {
  1580. c = (ins->oprs[2].offset) & 0x1F;
  1581. // #imm
  1582. bytes[2] |= c >> 1;
  1583. if (c & 0x01)
  1584. {
  1585. bytes[3] |= 0x80;
  1586. }
  1587. c = 0; // Clr bit 4 in byte[3]
  1588. }
  1589. // <shift>
  1590. c |= shiftval (&ins->oprs[2]) << 5;
  1591. bytes[3] |= c;
  1592. }
  1593. // reg,imm
  1594. if (keep == 0x0B)
  1595. {
  1596. int shimm;
  1597. shimm = imm_shift (ins->oprs[1].offset);
  1598. if (shimm == -1)
  1599. {
  1600. errfunc (ERR_NONFATAL, "cannot create that constant");
  1601. }
  1602. bytes[3] = shimm & 0xFF;
  1603. bytes[2] |= (shimm & 0xF00) >> 8;
  1604. }
  1605. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1606. return;
  1607. case 0xC: // CMP Rn,Rm
  1608. case 0xD: // CMP Rn,Rm,<shift>Rs
  1609. case 0xE: // CMP Rn,Rm,<shift>imm
  1610. case 0xF: // CMP Rn,<shift>imm
  1611. ++codes;
  1612. bytes[0] = c | *codes++;
  1613. bytes[1] = *codes;
  1614. // Implicit S code
  1615. bytes[1] |= 0x10;
  1616. c = regval (&ins->oprs[0],1);
  1617. // Rn in low nibble
  1618. bytes[1] |= c;
  1619. // No destination
  1620. bytes[2] = 0;
  1621. if (keep != 0x0B)
  1622. {
  1623. // Rm in low nibble
  1624. bytes[3] = regval (&ins->oprs[1],1);
  1625. }
  1626. // Shifts if any
  1627. if (keep == 0x0D || keep == 0x0E)
  1628. {
  1629. // Shift in bytes 2 and 3
  1630. if (keep == 0x0D)
  1631. {
  1632. // Rs
  1633. c = regval (&ins->oprs[2],1);
  1634. bytes[2] |= c;
  1635. c = 0x10; // Set bit 4 in byte[3]
  1636. }
  1637. if (keep == 0x0E)
  1638. {
  1639. c = (ins->oprs[2].offset) & 0x1F;
  1640. // #imm
  1641. bytes[2] |= c >> 1;
  1642. if (c & 0x01)
  1643. {
  1644. bytes[3] |= 0x80;
  1645. }
  1646. c = 0; // Clr bit 4 in byte[3]
  1647. }
  1648. // <shift>
  1649. c |= shiftval (&ins->oprs[2]) << 5;
  1650. bytes[3] |= c;
  1651. }
  1652. // reg,imm
  1653. if (keep == 0x0F)
  1654. {
  1655. int shimm;
  1656. shimm = imm_shift (ins->oprs[1].offset);
  1657. if (shimm == -1)
  1658. {
  1659. errfunc (ERR_NONFATAL, "cannot create that constant");
  1660. }
  1661. bytes[3] = shimm & 0xFF;
  1662. bytes[2] |= (shimm & 0xF00) >> 8;
  1663. }
  1664. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1665. return;
  1666. case 0x10: // MRS Rd,<psr>
  1667. ++codes;
  1668. bytes[0] = c | *codes++;
  1669. bytes[1] = *codes++;
  1670. // Rd
  1671. c = regval (&ins->oprs[0],1);
  1672. bytes[2] = c << 4;
  1673. bytes[3] = 0;
  1674. c = ins->oprs[1].basereg;
  1675. if (c == R_CPSR || c == R_SPSR)
  1676. {
  1677. if (c == R_SPSR)
  1678. {
  1679. bytes[1] |= 0x40;
  1680. }
  1681. }
  1682. else
  1683. {
  1684. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1685. }
  1686. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1687. return;
  1688. case 0x11: // MSR <psr>,Rm
  1689. case 0x12: // MSR <psrf>,Rm
  1690. case 0x13: // MSR <psrf>,#expression
  1691. ++codes;
  1692. bytes[0] = c | *codes++;
  1693. bytes[1] = *codes++;
  1694. bytes[2] = *codes;
  1695. if (keep == 0x11 || keep == 0x12)
  1696. {
  1697. // Rm
  1698. c = regval (&ins->oprs[1],1);
  1699. bytes[3] = c;
  1700. }
  1701. else
  1702. {
  1703. int shimm;
  1704. shimm = imm_shift (ins->oprs[1].offset);
  1705. if (shimm == -1)
  1706. {
  1707. errfunc (ERR_NONFATAL, "cannot create that constant");
  1708. }
  1709. bytes[3] = shimm & 0xFF;
  1710. bytes[2] |= (shimm & 0xF00) >> 8;
  1711. }
  1712. c = ins->oprs[0].basereg;
  1713. if ( keep == 0x11)
  1714. {
  1715. if ( c == R_CPSR || c == R_SPSR)
  1716. {
  1717. if ( c== R_SPSR)
  1718. {
  1719. bytes[1] |= 0x40;
  1720. }
  1721. }
  1722. else
  1723. {
  1724. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1725. }
  1726. }
  1727. else
  1728. {
  1729. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  1730. {
  1731. if ( c== R_SPSR_FLG)
  1732. {
  1733. bytes[1] |= 0x40;
  1734. }
  1735. }
  1736. else
  1737. {
  1738. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  1739. }
  1740. }
  1741. break;
  1742. case 0x14: // MUL Rd,Rm,Rs
  1743. case 0x15: // MULA Rd,Rm,Rs,Rn
  1744. ++codes;
  1745. bytes[0] = c | *codes++;
  1746. bytes[1] = *codes++;
  1747. bytes[3] = *codes;
  1748. // Rd
  1749. bytes[1] |= regval (&ins->oprs[0],1);
  1750. if (has_S_code)
  1751. bytes[1] |= 0x10;
  1752. // Rm
  1753. bytes[3] |= regval (&ins->oprs[1],1);
  1754. // Rs
  1755. bytes[2] = regval (&ins->oprs[2],1);
  1756. if (keep == 0x15)
  1757. {
  1758. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  1759. }
  1760. break;
  1761. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  1762. ++codes;
  1763. bytes[0] = c | *codes++;
  1764. bytes[1] = *codes++;
  1765. bytes[3] = *codes;
  1766. // RdHi
  1767. bytes[1] |= regval (&ins->oprs[1],1);
  1768. if (has_S_code)
  1769. bytes[1] |= 0x10;
  1770. // RdLo
  1771. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1772. // Rm
  1773. bytes[3] |= regval (&ins->oprs[2],1);
  1774. // Rs
  1775. bytes[2] |= regval (&ins->oprs[3],1);
  1776. break;
  1777. case 0x17: // LDR Rd, expression
  1778. ++codes;
  1779. bytes[0] = c | *codes++;
  1780. bytes[1] = *codes++;
  1781. // Rd
  1782. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1783. if (has_B_code)
  1784. bytes[1] |= 0x40;
  1785. if (has_T_code)
  1786. {
  1787. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1788. }
  1789. if (has_W_code)
  1790. {
  1791. errfunc (ERR_NONFATAL, "'!' not allowed");
  1792. }
  1793. // Rn - implicit R15
  1794. bytes[1] |= 0xF;
  1795. if (ins->oprs[1].segment != segment)
  1796. {
  1797. errfunc (ERR_NONFATAL, "label not in same segment");
  1798. }
  1799. data = ins->oprs[1].offset - (offset + 8);
  1800. if (data < 0)
  1801. {
  1802. data = -data;
  1803. }
  1804. else
  1805. {
  1806. bytes[1] |= 0x80;
  1807. }
  1808. if (data >= 0x1000)
  1809. {
  1810. errfunc (ERR_NONFATAL, "too long offset");
  1811. }
  1812. bytes[2] |= ((data & 0xF00) >> 8);
  1813. bytes[3] = data & 0xFF;
  1814. break;
  1815. case 0x18: // LDR Rd, [Rn]
  1816. ++codes;
  1817. bytes[0] = c | *codes++;
  1818. bytes[1] = *codes++;
  1819. // Rd
  1820. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1821. if (has_B_code)
  1822. bytes[1] |= 0x40;
  1823. if (has_T_code)
  1824. {
  1825. bytes[1] |= 0x20; // write-back
  1826. }
  1827. else
  1828. {
  1829. bytes[0] |= 0x01; // implicit pre-index mode
  1830. }
  1831. if (has_W_code)
  1832. {
  1833. bytes[1] |= 0x20; // write-back
  1834. }
  1835. // Rn
  1836. c = regval (&ins->oprs[1],1);
  1837. bytes[1] |= c;
  1838. if (c == 0x15) // R15
  1839. data = -8;
  1840. else
  1841. data = 0;
  1842. if (data < 0)
  1843. {
  1844. data = -data;
  1845. }
  1846. else
  1847. {
  1848. bytes[1] |= 0x80;
  1849. }
  1850. bytes[2] |= ((data & 0xF00) >> 8);
  1851. bytes[3] = data & 0xFF;
  1852. break;
  1853. case 0x19: // LDR Rd, [Rn,#expression]
  1854. case 0x20: // LDR Rd, [Rn,Rm]
  1855. case 0x21: // LDR Rd, [Rn,Rm,shift]
  1856. ++codes;
  1857. bytes[0] = c | *codes++;
  1858. bytes[1] = *codes++;
  1859. // Rd
  1860. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1861. if (has_B_code)
  1862. bytes[1] |= 0x40;
  1863. // Rn
  1864. c = regval (&ins->oprs[1],1);
  1865. bytes[1] |= c;
  1866. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1867. {
  1868. bytes[0] |= 0x01; // pre-index mode
  1869. if (has_W_code)
  1870. {
  1871. bytes[1] |= 0x20;
  1872. }
  1873. if (has_T_code)
  1874. {
  1875. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1876. }
  1877. }
  1878. else
  1879. {
  1880. if (has_T_code) // Forced write-back in post-index mode
  1881. {
  1882. bytes[1] |= 0x20;
  1883. }
  1884. if (has_W_code)
  1885. {
  1886. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1887. }
  1888. }
  1889. if (keep == 0x19)
  1890. {
  1891. data = ins->oprs[2].offset;
  1892. if (data < 0)
  1893. {
  1894. data = -data;
  1895. }
  1896. else
  1897. {
  1898. bytes[1] |= 0x80;
  1899. }
  1900. if (data >= 0x1000)
  1901. {
  1902. errfunc (ERR_NONFATAL, "too long offset");
  1903. }
  1904. bytes[2] |= ((data & 0xF00) >> 8);
  1905. bytes[3] = data & 0xFF;
  1906. }
  1907. else
  1908. {
  1909. if (ins->oprs[2].minus == 0)
  1910. {
  1911. bytes[1] |= 0x80;
  1912. }
  1913. c = regval (&ins->oprs[2],1);
  1914. bytes[3] = c;
  1915. if (keep == 0x21)
  1916. {
  1917. c = ins->oprs[3].offset;
  1918. if (c > 0x1F)
  1919. {
  1920. errfunc (ERR_NONFATAL, "too large shiftvalue");
  1921. c = c & 0x1F;
  1922. }
  1923. bytes[2] |= c >> 1;
  1924. if (c & 0x01)
  1925. {
  1926. bytes[3] |= 0x80;
  1927. }
  1928. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  1929. }
  1930. }
  1931. break;
  1932. case 0x22: // LDRH Rd, expression
  1933. ++codes;
  1934. bytes[0] = c | 0x01; // Implicit pre-index
  1935. bytes[1] = *codes++;
  1936. // Rd
  1937. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1938. // Rn - implicit R15
  1939. bytes[1] |= 0xF;
  1940. if (ins->oprs[1].segment != segment)
  1941. {
  1942. errfunc (ERR_NONFATAL, "label not in same segment");
  1943. }
  1944. data = ins->oprs[1].offset - (offset + 8);
  1945. if (data < 0)
  1946. {
  1947. data = -data;
  1948. }
  1949. else
  1950. {
  1951. bytes[1] |= 0x80;
  1952. }
  1953. if (data >= 0x100)
  1954. {
  1955. errfunc (ERR_NONFATAL, "too long offset");
  1956. }
  1957. bytes[3] = *codes++;
  1958. bytes[2] |= ((data & 0xF0) >> 4);
  1959. bytes[3] |= data & 0xF;
  1960. break;
  1961. case 0x23: // LDRH Rd, Rn
  1962. ++codes;
  1963. bytes[0] = c | 0x01; // Implicit pre-index
  1964. bytes[1] = *codes++;
  1965. // Rd
  1966. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1967. // Rn
  1968. c = regval (&ins->oprs[1],1);
  1969. bytes[1] |= c;
  1970. if (c == 0x15) // R15
  1971. data = -8;
  1972. else
  1973. data = 0;
  1974. if (data < 0)
  1975. {
  1976. data = -data;
  1977. }
  1978. else
  1979. {
  1980. bytes[1] |= 0x80;
  1981. }
  1982. if (data >= 0x100)
  1983. {
  1984. errfunc (ERR_NONFATAL, "too long offset");
  1985. }
  1986. bytes[3] = *codes++;
  1987. bytes[2] |= ((data & 0xF0) >> 4);
  1988. bytes[3] |= data & 0xF;
  1989. break;
  1990. case 0x24: // LDRH Rd, Rn, expression
  1991. case 0x25: // LDRH Rd, Rn, Rm
  1992. ++codes;
  1993. bytes[0] = c;
  1994. bytes[1] = *codes++;
  1995. // Rd
  1996. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1997. // Rn
  1998. c = regval (&ins->oprs[1],1);
  1999. bytes[1] |= c;
  2000. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  2001. {
  2002. bytes[0] |= 0x01; // pre-index mode
  2003. if (has_W_code)
  2004. {
  2005. bytes[1] |= 0x20;
  2006. }
  2007. }
  2008. else
  2009. {
  2010. if (has_W_code)
  2011. {
  2012. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  2013. }
  2014. }
  2015. bytes[3] = *codes++;
  2016. if (keep == 0x24)
  2017. {
  2018. data = ins->oprs[2].offset;
  2019. if (data < 0)
  2020. {
  2021. data = -data;
  2022. }
  2023. else
  2024. {
  2025. bytes[1] |= 0x80;
  2026. }
  2027. if (data >= 0x100)
  2028. {
  2029. errfunc (ERR_NONFATAL, "too long offset");
  2030. }
  2031. bytes[2] |= ((data & 0xF0) >> 4);
  2032. bytes[3] |= data & 0xF;
  2033. }
  2034. else
  2035. {
  2036. if (ins->oprs[2].minus == 0)
  2037. {
  2038. bytes[1] |= 0x80;
  2039. }
  2040. c = regval (&ins->oprs[2],1);
  2041. bytes[3] |= c;
  2042. }
  2043. break;
  2044. case 0x26: // LDM/STM Rn, {reg-list}
  2045. ++codes;
  2046. bytes[0] = c;
  2047. bytes[0] |= ( *codes >> 4) & 0xF;
  2048. bytes[1] = ( *codes << 4) & 0xF0;
  2049. ++codes;
  2050. if (has_W_code)
  2051. {
  2052. bytes[1] |= 0x20;
  2053. }
  2054. if (has_F_code)
  2055. {
  2056. bytes[1] |= 0x40;
  2057. }
  2058. // Rn
  2059. bytes[1] |= regval (&ins->oprs[0],1);
  2060. data = ins->oprs[1].basereg;
  2061. bytes[2] = ((data >> 8) & 0xFF);
  2062. bytes[3] = (data & 0xFF);
  2063. break;
  2064. case 0x27: // SWP Rd, Rm, [Rn]
  2065. ++codes;
  2066. bytes[0] = c;
  2067. bytes[0] |= *codes++;
  2068. bytes[1] = regval (&ins->oprs[2],1);
  2069. if (has_B_code)
  2070. {
  2071. bytes[1] |= 0x40;
  2072. }
  2073. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2074. bytes[3] = *codes++;
  2075. bytes[3] |= regval (&ins->oprs[1],1);
  2076. break;
  2077. default:
  2078. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2079. bytes[0] = c;
  2080. // And a fix nibble
  2081. ++codes;
  2082. bytes[0] |= *codes++;
  2083. if ( *codes == 0x01) // An I bit
  2084. {
  2085. }
  2086. if ( *codes == 0x02) // An I bit
  2087. {
  2088. }
  2089. ++codes;
  2090. }
  2091. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2092. }
  2093. *)
  2094. {$endif dummy
  2095. }