aasmcpu.pas 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991
  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. 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_REGF = $00201020; { coproc register }
  74. OT_MEMORY = $00204000; { register number in 'basereg' }
  75. OT_MEM8 = $00204001;
  76. OT_MEM16 = $00204002;
  77. OT_MEM32 = $00204004;
  78. OT_MEM64 = $00204008;
  79. OT_MEM80 = $00204010;
  80. { word/byte load/store }
  81. OT_AM2 = $00010000;
  82. { misc ld/st operations }
  83. OT_AM3 = $00020000;
  84. { multiple ld/st operations }
  85. OT_AM4 = $00040000;
  86. { co proc. ld/st operations }
  87. OT_AM5 = $00080000;
  88. OT_AMMASK = $000f0000;
  89. { IT instruction }
  90. OT_CONDITION = $00100000;
  91. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  92. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  93. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  94. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  95. OT_FPUREG = $01000000; { floating point stack registers }
  96. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  97. { a mask for the following }
  98. OT_MEM_OFFS = $00604000; { special type of EA }
  99. { simple [address] offset }
  100. OT_ONENESS = $00800000; { special type of immediate operand }
  101. { so UNITY == IMMEDIATE | ONENESS }
  102. OT_UNITY = $00802000; { for shift/rotate instructions }
  103. instabentries = {$i armnop.inc}
  104. maxinfolen = 5;
  105. IF_NONE = $00000000;
  106. IF_ARMMASK = $000F0000;
  107. IF_ARM7 = $00070000;
  108. IF_FPMASK = $00F00000;
  109. IF_FPA = $00100000;
  110. { if the instruction can change in a second pass }
  111. IF_PASS2 = longint($80000000);
  112. type
  113. TInsTabCache=array[TasmOp] of longint;
  114. PInsTabCache=^TInsTabCache;
  115. tinsentry = record
  116. opcode : tasmop;
  117. ops : byte;
  118. optypes : array[0..3] of longint;
  119. code : array[0..maxinfolen] of char;
  120. flags : longint;
  121. end;
  122. pinsentry=^tinsentry;
  123. const
  124. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  125. var
  126. InsTabCache : PInsTabCache;
  127. type
  128. taicpu = class(tai_cpu_abstract_sym)
  129. oppostfix : TOpPostfix;
  130. wideformat : boolean;
  131. roundingmode : troundingmode;
  132. procedure loadshifterop(opidx:longint;const so:tshifterop);
  133. procedure loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean=false);
  134. procedure loadconditioncode(opidx:longint;const cond:tasmcond);
  135. procedure loadmodeflags(opidx:longint;const flags:tcpumodeflags);
  136. procedure loadspecialreg(opidx:longint;const areg:tregister; const aflags:tspecialregflags);
  137. constructor op_none(op : tasmop);
  138. constructor op_reg(op : tasmop;_op1 : tregister);
  139. constructor op_ref(op : tasmop;const _op1 : treference);
  140. constructor op_const(op : tasmop;_op1 : longint);
  141. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  142. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  143. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  144. constructor op_regset(op:tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  145. constructor op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  146. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  147. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  148. constructor op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  149. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  150. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  151. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  152. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  153. { SFM/LFM }
  154. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  155. { ITxxx }
  156. constructor op_cond(op: tasmop; cond: tasmcond);
  157. { CPSxx }
  158. constructor op_modeflags(op: tasmop; flags: tcpumodeflags);
  159. constructor op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  160. { MSR }
  161. constructor op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  162. { *M*LL }
  163. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  164. { this is for Jmp instructions }
  165. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  166. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  167. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  168. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  169. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  170. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  171. function spilling_get_operation_type(opnr: longint): topertype;override;
  172. { assembler }
  173. public
  174. { the next will reset all instructions that can change in pass 2 }
  175. procedure ResetPass1;override;
  176. procedure ResetPass2;override;
  177. function CheckIfValid:boolean;
  178. function GetString:string;
  179. function Pass1(objdata:TObjData):longint;override;
  180. procedure Pass2(objdata:TObjData);override;
  181. protected
  182. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  183. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  184. procedure ppubuildderefimploper(var o:toper);override;
  185. procedure ppuderefoper(var o:toper);override;
  186. private
  187. { next fields are filled in pass1, so pass2 is faster }
  188. inssize : shortint;
  189. insoffset : longint;
  190. LastInsOffset : longint; { need to be public to be reset }
  191. insentry : PInsEntry;
  192. function InsEnd:longint;
  193. procedure create_ot(objdata:TObjData);
  194. function Matches(p:PInsEntry):longint;
  195. function calcsize(p:PInsEntry):shortint;
  196. procedure gencode(objdata:TObjData);
  197. function NeedAddrPrefix(opidx:byte):boolean;
  198. procedure Swapoperands;
  199. function FindInsentry(objdata:TObjData):boolean;
  200. end;
  201. tai_align = class(tai_align_abstract)
  202. { nothing to add }
  203. end;
  204. tai_thumb_func = class(tai)
  205. constructor create;
  206. end;
  207. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  208. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  209. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  210. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  211. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  212. { inserts pc relative symbols at places where they are reachable
  213. and transforms special instructions to valid instruction encodings }
  214. procedure finalizearmcode(list,listtoinsert : TAsmList);
  215. { inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
  216. procedure InsertPData;
  217. procedure InitAsm;
  218. procedure DoneAsm;
  219. implementation
  220. uses
  221. itcpugas,aoptcpu;
  222. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  223. begin
  224. allocate_oper(opidx+1);
  225. with oper[opidx]^ do
  226. begin
  227. if typ<>top_shifterop then
  228. begin
  229. clearop(opidx);
  230. new(shifterop);
  231. end;
  232. shifterop^:=so;
  233. typ:=top_shifterop;
  234. if assigned(add_reg_instruction_hook) then
  235. add_reg_instruction_hook(self,shifterop^.rs);
  236. end;
  237. end;
  238. procedure taicpu.loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean);
  239. var
  240. i : byte;
  241. begin
  242. allocate_oper(opidx+1);
  243. with oper[opidx]^ do
  244. begin
  245. if typ<>top_regset then
  246. begin
  247. clearop(opidx);
  248. new(regset);
  249. end;
  250. regset^:=s;
  251. regtyp:=regsetregtype;
  252. subreg:=regsetsubregtype;
  253. usermode:=ausermode;
  254. typ:=top_regset;
  255. case regsetregtype of
  256. R_INTREGISTER:
  257. for i:=RS_R0 to RS_R15 do
  258. begin
  259. if assigned(add_reg_instruction_hook) and (i in regset^) then
  260. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,regsetsubregtype));
  261. end;
  262. R_MMREGISTER:
  263. { both RS_S0 and RS_D0 range from 0 to 31 }
  264. for i:=RS_D0 to RS_D31 do
  265. begin
  266. if assigned(add_reg_instruction_hook) and (i in regset^) then
  267. add_reg_instruction_hook(self,newreg(R_MMREGISTER,i,regsetsubregtype));
  268. end;
  269. end;
  270. end;
  271. end;
  272. procedure taicpu.loadconditioncode(opidx:longint;const cond:tasmcond);
  273. begin
  274. allocate_oper(opidx+1);
  275. with oper[opidx]^ do
  276. begin
  277. if typ<>top_conditioncode then
  278. clearop(opidx);
  279. cc:=cond;
  280. typ:=top_conditioncode;
  281. end;
  282. end;
  283. procedure taicpu.loadmodeflags(opidx: longint; const flags: tcpumodeflags);
  284. begin
  285. allocate_oper(opidx+1);
  286. with oper[opidx]^ do
  287. begin
  288. if typ<>top_modeflags then
  289. clearop(opidx);
  290. modeflags:=flags;
  291. typ:=top_modeflags;
  292. end;
  293. end;
  294. procedure taicpu.loadspecialreg(opidx: longint; const areg: tregister; const aflags: tspecialregflags);
  295. begin
  296. allocate_oper(opidx+1);
  297. with oper[opidx]^ do
  298. begin
  299. if typ<>top_specialreg then
  300. clearop(opidx);
  301. specialreg:=areg;
  302. specialflags:=aflags;
  303. typ:=top_specialreg;
  304. end;
  305. end;
  306. {*****************************************************************************
  307. taicpu Constructors
  308. *****************************************************************************}
  309. constructor taicpu.op_none(op : tasmop);
  310. begin
  311. inherited create(op);
  312. end;
  313. { for pld }
  314. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  315. begin
  316. inherited create(op);
  317. ops:=1;
  318. loadref(0,_op1);
  319. end;
  320. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  321. begin
  322. inherited create(op);
  323. ops:=1;
  324. loadreg(0,_op1);
  325. end;
  326. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  327. begin
  328. inherited create(op);
  329. ops:=1;
  330. loadconst(0,aint(_op1));
  331. end;
  332. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  333. begin
  334. inherited create(op);
  335. ops:=2;
  336. loadreg(0,_op1);
  337. loadreg(1,_op2);
  338. end;
  339. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  340. begin
  341. inherited create(op);
  342. ops:=2;
  343. loadreg(0,_op1);
  344. loadconst(1,aint(_op2));
  345. end;
  346. constructor taicpu.op_regset(op: tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  347. begin
  348. inherited create(op);
  349. ops:=1;
  350. loadregset(0,regtype,subreg,_op1);
  351. end;
  352. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  353. begin
  354. inherited create(op);
  355. ops:=2;
  356. loadref(0,_op1);
  357. loadregset(1,regtype,subreg,_op2);
  358. end;
  359. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  360. begin
  361. inherited create(op);
  362. ops:=2;
  363. loadreg(0,_op1);
  364. loadref(1,_op2);
  365. end;
  366. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  367. begin
  368. inherited create(op);
  369. ops:=3;
  370. loadreg(0,_op1);
  371. loadreg(1,_op2);
  372. loadreg(2,_op3);
  373. end;
  374. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  375. begin
  376. inherited create(op);
  377. ops:=4;
  378. loadreg(0,_op1);
  379. loadreg(1,_op2);
  380. loadreg(2,_op3);
  381. loadreg(3,_op4);
  382. end;
  383. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  384. begin
  385. inherited create(op);
  386. ops:=3;
  387. loadreg(0,_op1);
  388. loadreg(1,_op2);
  389. loadconst(2,aint(_op3));
  390. end;
  391. constructor taicpu.op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  392. begin
  393. inherited create(op);
  394. ops:=3;
  395. loadreg(0,_op1);
  396. loadconst(1,aint(_op2));
  397. loadconst(2,aint(_op3));
  398. end;
  399. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  400. begin
  401. inherited create(op);
  402. ops:=3;
  403. loadreg(0,_op1);
  404. loadconst(1,_op2);
  405. loadref(2,_op3);
  406. end;
  407. constructor taicpu.op_cond(op: tasmop; cond: tasmcond);
  408. begin
  409. inherited create(op);
  410. ops:=1;
  411. loadconditioncode(0, cond);
  412. end;
  413. constructor taicpu.op_modeflags(op: tasmop; flags: tcpumodeflags);
  414. begin
  415. inherited create(op);
  416. ops := 1;
  417. loadmodeflags(0,flags);
  418. end;
  419. constructor taicpu.op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  420. begin
  421. inherited create(op);
  422. ops := 2;
  423. loadmodeflags(0,flags);
  424. loadconst(1,a);
  425. end;
  426. constructor taicpu.op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  427. begin
  428. inherited create(op);
  429. ops:=2;
  430. loadspecialreg(0,specialreg,specialregflags);
  431. loadreg(1,_op2);
  432. end;
  433. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  434. begin
  435. inherited create(op);
  436. ops:=3;
  437. loadreg(0,_op1);
  438. loadreg(1,_op2);
  439. loadsymbol(0,_op3,_op3ofs);
  440. end;
  441. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  442. begin
  443. inherited create(op);
  444. ops:=3;
  445. loadreg(0,_op1);
  446. loadreg(1,_op2);
  447. loadref(2,_op3);
  448. end;
  449. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  450. begin
  451. inherited create(op);
  452. ops:=3;
  453. loadreg(0,_op1);
  454. loadreg(1,_op2);
  455. loadshifterop(2,_op3);
  456. end;
  457. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  458. begin
  459. inherited create(op);
  460. ops:=4;
  461. loadreg(0,_op1);
  462. loadreg(1,_op2);
  463. loadreg(2,_op3);
  464. loadshifterop(3,_op4);
  465. end;
  466. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  467. begin
  468. inherited create(op);
  469. condition:=cond;
  470. ops:=1;
  471. loadsymbol(0,_op1,0);
  472. end;
  473. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  474. begin
  475. inherited create(op);
  476. ops:=1;
  477. loadsymbol(0,_op1,0);
  478. end;
  479. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  480. begin
  481. inherited create(op);
  482. ops:=1;
  483. loadsymbol(0,_op1,_op1ofs);
  484. end;
  485. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  486. begin
  487. inherited create(op);
  488. ops:=2;
  489. loadreg(0,_op1);
  490. loadsymbol(1,_op2,_op2ofs);
  491. end;
  492. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  493. begin
  494. inherited create(op);
  495. ops:=2;
  496. loadsymbol(0,_op1,_op1ofs);
  497. loadref(1,_op2);
  498. end;
  499. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  500. begin
  501. { allow the register allocator to remove unnecessary moves }
  502. result:=(
  503. ((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  504. ((opcode=A_MVF) and (regtype = R_FPUREGISTER)) or
  505. ((opcode in [A_FCPYS, A_FCPYD]) and (regtype = R_MMREGISTER))
  506. ) and
  507. (oppostfix in [PF_None,PF_D]) and
  508. (condition=C_None) and
  509. (ops=2) and
  510. (oper[0]^.typ=top_reg) and
  511. (oper[1]^.typ=top_reg) and
  512. (oper[0]^.reg=oper[1]^.reg);
  513. end;
  514. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  515. var
  516. op: tasmop;
  517. begin
  518. case getregtype(r) of
  519. R_INTREGISTER :
  520. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  521. R_FPUREGISTER :
  522. { use lfm because we don't know the current internal format
  523. and avoid exceptions
  524. }
  525. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  526. R_MMREGISTER :
  527. begin
  528. case getsubreg(r) of
  529. R_SUBFD:
  530. op:=A_FLDD;
  531. R_SUBFS:
  532. op:=A_FLDS;
  533. else
  534. internalerror(2009112905);
  535. end;
  536. result:=taicpu.op_reg_ref(op,r,ref);
  537. end;
  538. else
  539. internalerror(200401041);
  540. end;
  541. end;
  542. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  543. var
  544. op: tasmop;
  545. begin
  546. case getregtype(r) of
  547. R_INTREGISTER :
  548. result:=taicpu.op_reg_ref(A_STR,r,ref);
  549. R_FPUREGISTER :
  550. { use sfm because we don't know the current internal format
  551. and avoid exceptions
  552. }
  553. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  554. R_MMREGISTER :
  555. begin
  556. case getsubreg(r) of
  557. R_SUBFD:
  558. op:=A_FSTD;
  559. R_SUBFS:
  560. op:=A_FSTS;
  561. else
  562. internalerror(2009112904);
  563. end;
  564. result:=taicpu.op_reg_ref(op,r,ref);
  565. end;
  566. else
  567. internalerror(200401041);
  568. end;
  569. end;
  570. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  571. begin
  572. case opcode of
  573. A_ADC,A_ADD,A_AND,A_BIC,
  574. A_EOR,A_CLZ,A_RBIT,
  575. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  576. A_LDRSH,A_LDRT,
  577. A_MOV,A_MVN,A_MLA,A_MUL,
  578. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  579. A_SWP,A_SWPB,
  580. A_LDF,A_FLT,A_FIX,
  581. A_ADF,A_DVF,A_FDV,A_FML,
  582. A_RFS,A_RFC,A_RDF,
  583. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  584. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  585. A_LFM,
  586. A_FLDS,A_FLDD,
  587. A_FMRX,A_FMXR,A_FMSTAT,
  588. A_FMSR,A_FMRS,A_FMDRR,
  589. A_FCPYS,A_FCPYD,A_FCVTSD,A_FCVTDS,
  590. A_FABSS,A_FABSD,A_FSQRTS,A_FSQRTD,A_FMULS,A_FMULD,
  591. A_FADDS,A_FADDD,A_FSUBS,A_FSUBD,A_FDIVS,A_FDIVD,
  592. A_FMACS,A_FMACD,A_FMSCS,A_FMSCD,A_FNMACS,A_FNMACD,
  593. A_FNMSCS,A_FNMSCD,A_FNMULS,A_FNMULD,
  594. A_FMDHR,A_FMRDH,A_FMDLR,A_FMRDL,
  595. A_FNEGS,A_FNEGD,
  596. A_FSITOS,A_FSITOD,A_FTOSIS,A_FTOSID,
  597. A_FTOUIS,A_FTOUID,A_FUITOS,A_FUITOD,
  598. A_SXTB16,A_UXTB16,
  599. A_UXTB,A_UXTH,A_SXTB,A_SXTH,
  600. A_NEG:
  601. if opnr=0 then
  602. result:=operand_write
  603. else
  604. result:=operand_read;
  605. A_BKPT,A_B,A_BL,A_BLX,A_BX,
  606. A_CMN,A_CMP,A_TEQ,A_TST,
  607. A_CMF,A_CMFE,A_WFS,A_CNF,
  608. A_FCMPS,A_FCMPD,A_FCMPES,A_FCMPED,A_FCMPEZS,A_FCMPEZD,
  609. A_FCMPZS,A_FCMPZD:
  610. result:=operand_read;
  611. A_SMLAL,A_UMLAL:
  612. if opnr in [0,1] then
  613. result:=operand_readwrite
  614. else
  615. result:=operand_read;
  616. A_SMULL,A_UMULL,
  617. A_FMRRD:
  618. if opnr in [0,1] then
  619. result:=operand_write
  620. else
  621. result:=operand_read;
  622. A_STR,A_STRB,A_STRBT,
  623. A_STRH,A_STRT,A_STF,A_SFM,
  624. A_FSTS,A_FSTD:
  625. { important is what happens with the involved registers }
  626. if opnr=0 then
  627. result := operand_read
  628. else
  629. { check for pre/post indexed }
  630. result := operand_read;
  631. //Thumb2
  632. A_LSL, A_LSR, A_ROR, A_ASR, A_SDIV, A_UDIV, A_MOVW, A_MOVT, A_MLS, A_BFI:
  633. if opnr in [0] then
  634. result:=operand_write
  635. else
  636. result:=operand_read;
  637. A_BFC:
  638. if opnr in [0] then
  639. result:=operand_readwrite
  640. else
  641. result:=operand_read;
  642. A_LDREX:
  643. if opnr in [0] then
  644. result:=operand_write
  645. else
  646. result:=operand_read;
  647. A_STREX:
  648. if opnr in [0,1,2] then
  649. result:=operand_write;
  650. else
  651. internalerror(200403151);
  652. end;
  653. end;
  654. procedure BuildInsTabCache;
  655. var
  656. i : longint;
  657. begin
  658. new(instabcache);
  659. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  660. i:=0;
  661. while (i<InsTabEntries) do
  662. begin
  663. if InsTabCache^[InsTab[i].Opcode]=-1 then
  664. InsTabCache^[InsTab[i].Opcode]:=i;
  665. inc(i);
  666. end;
  667. end;
  668. procedure InitAsm;
  669. begin
  670. if not assigned(instabcache) then
  671. BuildInsTabCache;
  672. end;
  673. procedure DoneAsm;
  674. begin
  675. if assigned(instabcache) then
  676. begin
  677. dispose(instabcache);
  678. instabcache:=nil;
  679. end;
  680. end;
  681. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  682. begin
  683. i.oppostfix:=pf;
  684. result:=i;
  685. end;
  686. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  687. begin
  688. i.roundingmode:=rm;
  689. result:=i;
  690. end;
  691. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  692. begin
  693. i.condition:=c;
  694. result:=i;
  695. end;
  696. Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
  697. Begin
  698. Current:=tai(Current.Next);
  699. While Assigned(Current) And (Current.typ In SkipInstr) Do
  700. Current:=tai(Current.Next);
  701. Next:=Current;
  702. If Assigned(Next) And Not(Next.typ In SkipInstr) Then
  703. Result:=True
  704. Else
  705. Begin
  706. Next:=Nil;
  707. Result:=False;
  708. End;
  709. End;
  710. (*
  711. function armconstequal(hp1,hp2: tai): boolean;
  712. begin
  713. result:=false;
  714. if hp1.typ<>hp2.typ then
  715. exit;
  716. case hp1.typ of
  717. tai_const:
  718. result:=
  719. (tai_const(hp2).sym=tai_const(hp).sym) and
  720. (tai_const(hp2).value=tai_const(hp).value) and
  721. (tai(hp2.previous).typ=ait_label);
  722. tai_const:
  723. result:=
  724. (tai_const(hp2).sym=tai_const(hp).sym) and
  725. (tai_const(hp2).value=tai_const(hp).value) and
  726. (tai(hp2.previous).typ=ait_label);
  727. end;
  728. end;
  729. *)
  730. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  731. var
  732. curinspos,
  733. penalty,
  734. lastinspos,
  735. { increased for every data element > 4 bytes inserted }
  736. currentsize,
  737. extradataoffset,
  738. limit: longint;
  739. curop : longint;
  740. curtai : tai;
  741. curdatatai,hp,hp2 : tai;
  742. curdata : TAsmList;
  743. l : tasmlabel;
  744. doinsert,
  745. removeref : boolean;
  746. multiplier : byte;
  747. begin
  748. curdata:=TAsmList.create;
  749. lastinspos:=-1;
  750. curinspos:=0;
  751. extradataoffset:=0;
  752. if current_settings.cputype in cpu_thumb then
  753. begin
  754. multiplier:=2;
  755. limit:=504;
  756. end
  757. else
  758. begin
  759. limit:=1016;
  760. multiplier:=1;
  761. end;
  762. curtai:=tai(list.first);
  763. doinsert:=false;
  764. while assigned(curtai) do
  765. begin
  766. { instruction? }
  767. case curtai.typ of
  768. ait_instruction:
  769. begin
  770. { walk through all operand of the instruction }
  771. for curop:=0 to taicpu(curtai).ops-1 do
  772. begin
  773. { reference? }
  774. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  775. begin
  776. { pc relative symbol? }
  777. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  778. if assigned(curdatatai) and
  779. { move only if we're at the first reference of a label }
  780. not(tai_label(curdatatai).moved) then
  781. begin
  782. tai_label(curdatatai).moved:=true;
  783. { check if symbol already used. }
  784. { if yes, reuse the symbol }
  785. hp:=tai(curdatatai.next);
  786. removeref:=false;
  787. if assigned(hp) then
  788. begin
  789. case hp.typ of
  790. ait_const:
  791. begin
  792. if (tai_const(hp).consttype=aitconst_64bit) then
  793. inc(extradataoffset,multiplier);
  794. end;
  795. ait_comp_64bit,
  796. ait_real_64bit:
  797. begin
  798. inc(extradataoffset,multiplier);
  799. end;
  800. ait_real_80bit:
  801. begin
  802. inc(extradataoffset,2*multiplier);
  803. end;
  804. end;
  805. if (hp.typ=ait_const) then
  806. begin
  807. hp2:=tai(curdata.first);
  808. while assigned(hp2) do
  809. begin
  810. { if armconstequal(hp2,hp) then }
  811. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  812. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  813. then
  814. begin
  815. with taicpu(curtai).oper[curop]^.ref^ do
  816. begin
  817. symboldata:=hp2.previous;
  818. symbol:=tai_label(hp2.previous).labsym;
  819. end;
  820. removeref:=true;
  821. break;
  822. end;
  823. hp2:=tai(hp2.next);
  824. end;
  825. end;
  826. end;
  827. { move or remove symbol reference }
  828. repeat
  829. hp:=tai(curdatatai.next);
  830. listtoinsert.remove(curdatatai);
  831. if removeref then
  832. curdatatai.free
  833. else
  834. curdata.concat(curdatatai);
  835. curdatatai:=hp;
  836. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  837. if lastinspos=-1 then
  838. lastinspos:=curinspos;
  839. end;
  840. end;
  841. end;
  842. inc(curinspos,multiplier);
  843. end;
  844. ait_align:
  845. begin
  846. { code is always 4 byte aligned, so we don't have to take care of .align 2 which would
  847. requires also incrementing curinspos by 1 }
  848. inc(curinspos,(tai_align(curtai).aligntype div 4)*multiplier);
  849. end;
  850. ait_const:
  851. begin
  852. inc(curinspos,multiplier);
  853. if (tai_const(curtai).consttype=aitconst_64bit) then
  854. inc(curinspos,multiplier);
  855. end;
  856. ait_real_32bit:
  857. begin
  858. inc(curinspos,multiplier);
  859. end;
  860. ait_comp_64bit,
  861. ait_real_64bit:
  862. begin
  863. inc(curinspos,2*multiplier);
  864. end;
  865. ait_real_80bit:
  866. begin
  867. inc(curinspos,3*multiplier);
  868. end;
  869. end;
  870. { special case for case jump tables }
  871. if SimpleGetNextInstruction(curtai,hp) and
  872. (tai(hp).typ=ait_instruction) and
  873. (taicpu(hp).opcode=A_LDR) and
  874. (taicpu(hp).oper[0]^.typ=top_reg) and
  875. (taicpu(hp).oper[0]^.reg=NR_PC) then
  876. begin
  877. penalty:=1*multiplier;
  878. hp:=tai(hp.next);
  879. { skip register allocations and comments inserted by the optimizer }
  880. while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc]) do
  881. hp:=tai(hp.next);
  882. while assigned(hp) and (hp.typ=ait_const) do
  883. begin
  884. inc(penalty,multiplier);
  885. hp:=tai(hp.next);
  886. end;
  887. end
  888. else
  889. penalty:=0;
  890. { FLD/FST VFP instructions have a limit of +/- 1024, not 4096 }
  891. if SimpleGetNextInstruction(curtai,hp) and
  892. (tai(hp).typ=ait_instruction) and
  893. ((taicpu(hp).opcode=A_FLDS) or
  894. (taicpu(hp).opcode=A_FLDD)) then
  895. limit:=254;
  896. { don't miss an insert }
  897. doinsert:=doinsert or
  898. (not(curdata.empty) and
  899. (curinspos-lastinspos+penalty+extradataoffset>limit));
  900. { split only at real instructions else the test below fails }
  901. if doinsert and (curtai.typ=ait_instruction) and
  902. (
  903. { don't split loads of pc to lr and the following move }
  904. not(
  905. (taicpu(curtai).opcode=A_MOV) and
  906. (taicpu(curtai).oper[0]^.typ=top_reg) and
  907. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  908. (taicpu(curtai).oper[1]^.typ=top_reg) and
  909. (taicpu(curtai).oper[1]^.reg=NR_PC)
  910. )
  911. ) then
  912. begin
  913. lastinspos:=-1;
  914. extradataoffset:=0;
  915. if current_settings.cputype in cpu_thumb then
  916. limit:=508
  917. else
  918. limit:=1016;
  919. doinsert:=false;
  920. hp:=tai(curtai.next);
  921. current_asmdata.getjumplabel(l);
  922. { align thumb in thumb .text section to 4 bytes }
  923. if not(curdata.empty) and (current_settings.cputype in cpu_thumb) then
  924. curdata.Insert(tai_align.Create(4));
  925. curdata.insert(taicpu.op_sym(A_B,l));
  926. curdata.concat(tai_label.create(l));
  927. list.insertlistafter(curtai,curdata);
  928. curtai:=hp;
  929. end
  930. else
  931. curtai:=tai(curtai.next);
  932. end;
  933. { align thumb in thumb .text section to 4 bytes }
  934. if not(curdata.empty) and (current_settings.cputype in cpu_thumb+cpu_thumb2) then
  935. curdata.Insert(tai_align.Create(4));
  936. list.concatlist(curdata);
  937. curdata.free;
  938. end;
  939. procedure ensurethumb2encodings(list: TAsmList);
  940. var
  941. curtai: tai;
  942. op2reg: TRegister;
  943. begin
  944. { Do Thumb-2 16bit -> 32bit transformations }
  945. curtai:=tai(list.first);
  946. while assigned(curtai) do
  947. begin
  948. case curtai.typ of
  949. ait_instruction:
  950. begin
  951. case taicpu(curtai).opcode of
  952. A_ADD:
  953. begin
  954. { Set wide flag for ADD Rd,Rn,Rm where registers are over R7(high register set) }
  955. if taicpu(curtai).ops = 3 then
  956. begin
  957. if taicpu(curtai).oper[2]^.typ in [top_reg,top_shifterop] then
  958. begin
  959. if taicpu(curtai).oper[2]^.typ = top_reg then
  960. op2reg := taicpu(curtai).oper[2]^.reg
  961. else if taicpu(curtai).oper[2]^.shifterop^.rs <> NR_NO then
  962. op2reg := taicpu(curtai).oper[2]^.shifterop^.rs
  963. else
  964. op2reg := NR_NO;
  965. if op2reg <> NR_NO then
  966. begin
  967. if (taicpu(curtai).oper[0]^.reg >= NR_R8) or
  968. (taicpu(curtai).oper[1]^.reg >= NR_R8) or
  969. (op2reg >= NR_R8) then
  970. begin
  971. taicpu(curtai).wideformat:=true;
  972. { Handle special cases where register rules are violated by optimizer/user }
  973. { if d == 13 || (d == 15 && S == ‘0’) || n == 15 || m IN [13,15] then UNPREDICTABLE; }
  974. { Transform ADD.W Rx, Ry, R13 into ADD.W Rx, R13, Ry }
  975. if (op2reg = NR_R13) and (taicpu(curtai).oper[2]^.typ = top_reg) then
  976. begin
  977. taicpu(curtai).oper[2]^.reg := taicpu(curtai).oper[1]^.reg;
  978. taicpu(curtai).oper[1]^.reg := op2reg;
  979. end;
  980. end;
  981. end;
  982. end;
  983. end;
  984. end;
  985. end;
  986. end;
  987. end;
  988. curtai:=tai(curtai.Next);
  989. end;
  990. end;
  991. function getMergedInstruction(FirstOp,LastOp:TAsmOp;InvertLast:boolean) : TAsmOp;
  992. const
  993. opTable: array[A_IT..A_ITTTT] of string =
  994. ('T','TE','TT','TEE','TTE','TET','TTT',
  995. 'TEEE','TTEE','TETE','TTTE',
  996. 'TEET','TTET','TETT','TTTT');
  997. invertedOpTable: array[A_IT..A_ITTTT] of string =
  998. ('E','ET','EE','ETT','EET','ETE','EEE',
  999. 'ETTT','EETT','ETET','EEET',
  1000. 'ETTE','EETE','ETEE','EEEE');
  1001. var
  1002. resStr : string;
  1003. i : TAsmOp;
  1004. begin
  1005. if InvertLast then
  1006. resStr := opTable[FirstOp]+invertedOpTable[LastOp]
  1007. else
  1008. resStr := opTable[FirstOp]+opTable[LastOp];
  1009. if length(resStr) > 4 then
  1010. internalerror(2012100805);
  1011. for i := low(opTable) to high(opTable) do
  1012. if opTable[i] = resStr then
  1013. exit(i);
  1014. internalerror(2012100806);
  1015. end;
  1016. procedure foldITInstructions(list: TAsmList);
  1017. var
  1018. curtai,hp1 : tai;
  1019. levels,i : LongInt;
  1020. begin
  1021. curtai:=tai(list.First);
  1022. while assigned(curtai) do
  1023. begin
  1024. case curtai.typ of
  1025. ait_instruction:
  1026. if IsIT(taicpu(curtai).opcode) then
  1027. begin
  1028. levels := GetITLevels(taicpu(curtai).opcode);
  1029. if levels < 4 then
  1030. begin
  1031. i:=levels;
  1032. hp1:=tai(curtai.Next);
  1033. while assigned(hp1) and
  1034. (i > 0) do
  1035. begin
  1036. if hp1.typ=ait_instruction then
  1037. begin
  1038. dec(i);
  1039. if (i = 0) and
  1040. mustbelast(hp1) then
  1041. begin
  1042. hp1:=nil;
  1043. break;
  1044. end;
  1045. end;
  1046. hp1:=tai(hp1.Next);
  1047. end;
  1048. if assigned(hp1) then
  1049. begin
  1050. // We are pointing at the first instruction after the IT block
  1051. while assigned(hp1) and
  1052. (hp1.typ<>ait_instruction) do
  1053. hp1:=tai(hp1.Next);
  1054. if assigned(hp1) and
  1055. (hp1.typ=ait_instruction) and
  1056. IsIT(taicpu(hp1).opcode) then
  1057. begin
  1058. if (levels+GetITLevels(taicpu(hp1).opcode) <= 4) and
  1059. ((taicpu(curtai).oper[0]^.cc=taicpu(hp1).oper[0]^.cc) or
  1060. (taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc))) then
  1061. begin
  1062. taicpu(curtai).opcode:=getMergedInstruction(taicpu(curtai).opcode,
  1063. taicpu(hp1).opcode,
  1064. taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc));
  1065. list.Remove(hp1);
  1066. hp1.Free;
  1067. end;
  1068. end;
  1069. end;
  1070. end;
  1071. end;
  1072. end;
  1073. curtai:=tai(curtai.Next);
  1074. end;
  1075. end;
  1076. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1077. begin
  1078. { Do Thumb-2 16bit -> 32bit transformations }
  1079. if current_settings.cputype in cpu_thumb2 then
  1080. begin
  1081. ensurethumb2encodings(list);
  1082. foldITInstructions(list);
  1083. end;
  1084. insertpcrelativedata(list, listtoinsert);
  1085. end;
  1086. procedure InsertPData;
  1087. var
  1088. prolog: TAsmList;
  1089. begin
  1090. prolog:=TAsmList.create;
  1091. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  1092. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  1093. prolog.concat(Tai_const.Create_32bit(0));
  1094. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  1095. { dummy function }
  1096. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  1097. current_asmdata.asmlists[al_start].insertList(prolog);
  1098. prolog.Free;
  1099. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  1100. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  1101. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  1102. end;
  1103. (*
  1104. Floating point instruction format information, taken from the linux kernel
  1105. ARM Floating Point Instruction Classes
  1106. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1107. |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
  1108. |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)
  1109. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1110. |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
  1111. |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
  1112. |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
  1113. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1114. CPDT data transfer instructions
  1115. LDF, STF, LFM (copro 2), SFM (copro 2)
  1116. CPDO dyadic arithmetic instructions
  1117. ADF, MUF, SUF, RSF, DVF, RDF,
  1118. POW, RPW, RMF, FML, FDV, FRD, POL
  1119. CPDO monadic arithmetic instructions
  1120. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1121. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1122. CPRT joint arithmetic/data transfer instructions
  1123. FIX (arithmetic followed by load/store)
  1124. FLT (load/store followed by arithmetic)
  1125. CMF, CNF CMFE, CNFE (comparisons)
  1126. WFS, RFS (write/read floating point status register)
  1127. WFC, RFC (write/read floating point control register)
  1128. cond condition codes
  1129. P pre/post index bit: 0 = postindex, 1 = preindex
  1130. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1131. W write back bit: 1 = update base register (Rn)
  1132. L load/store bit: 0 = store, 1 = load
  1133. Rn base register
  1134. Rd destination/source register
  1135. Fd floating point destination register
  1136. Fn floating point source register
  1137. Fm floating point source register or floating point constant
  1138. uv transfer length (TABLE 1)
  1139. wx register count (TABLE 2)
  1140. abcd arithmetic opcode (TABLES 3 & 4)
  1141. ef destination size (rounding precision) (TABLE 5)
  1142. gh rounding mode (TABLE 6)
  1143. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1144. i constant bit: 1 = constant (TABLE 6)
  1145. */
  1146. /*
  1147. TABLE 1
  1148. +-------------------------+---+---+---------+---------+
  1149. | Precision | u | v | FPSR.EP | length |
  1150. +-------------------------+---+---+---------+---------+
  1151. | Single | 0 | 0 | x | 1 words |
  1152. | Double | 1 | 1 | x | 2 words |
  1153. | Extended | 1 | 1 | x | 3 words |
  1154. | Packed decimal | 1 | 1 | 0 | 3 words |
  1155. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1156. +-------------------------+---+---+---------+---------+
  1157. Note: x = don't care
  1158. */
  1159. /*
  1160. TABLE 2
  1161. +---+---+---------------------------------+
  1162. | w | x | Number of registers to transfer |
  1163. +---+---+---------------------------------+
  1164. | 0 | 1 | 1 |
  1165. | 1 | 0 | 2 |
  1166. | 1 | 1 | 3 |
  1167. | 0 | 0 | 4 |
  1168. +---+---+---------------------------------+
  1169. */
  1170. /*
  1171. TABLE 3: Dyadic Floating Point Opcodes
  1172. +---+---+---+---+----------+-----------------------+-----------------------+
  1173. | a | b | c | d | Mnemonic | Description | Operation |
  1174. +---+---+---+---+----------+-----------------------+-----------------------+
  1175. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1176. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1177. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1178. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1179. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1180. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1181. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1182. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1183. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1184. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1185. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1186. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1187. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1188. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1189. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1190. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1191. +---+---+---+---+----------+-----------------------+-----------------------+
  1192. Note: POW, RPW, POL are deprecated, and are available for backwards
  1193. compatibility only.
  1194. */
  1195. /*
  1196. TABLE 4: Monadic Floating Point Opcodes
  1197. +---+---+---+---+----------+-----------------------+-----------------------+
  1198. | a | b | c | d | Mnemonic | Description | Operation |
  1199. +---+---+---+---+----------+-----------------------+-----------------------+
  1200. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1201. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1202. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1203. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1204. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1205. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1206. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1207. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1208. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1209. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1210. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1211. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1212. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1213. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1214. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1215. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1216. +---+---+---+---+----------+-----------------------+-----------------------+
  1217. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1218. available for backwards compatibility only.
  1219. */
  1220. /*
  1221. TABLE 5
  1222. +-------------------------+---+---+
  1223. | Rounding Precision | e | f |
  1224. +-------------------------+---+---+
  1225. | IEEE Single precision | 0 | 0 |
  1226. | IEEE Double precision | 0 | 1 |
  1227. | IEEE Extended precision | 1 | 0 |
  1228. | undefined (trap) | 1 | 1 |
  1229. +-------------------------+---+---+
  1230. */
  1231. /*
  1232. TABLE 5
  1233. +---------------------------------+---+---+
  1234. | Rounding Mode | g | h |
  1235. +---------------------------------+---+---+
  1236. | Round to nearest (default) | 0 | 0 |
  1237. | Round toward plus infinity | 0 | 1 |
  1238. | Round toward negative infinity | 1 | 0 |
  1239. | Round toward zero | 1 | 1 |
  1240. +---------------------------------+---+---+
  1241. *)
  1242. function taicpu.GetString:string;
  1243. var
  1244. i : longint;
  1245. s : string;
  1246. addsize : boolean;
  1247. begin
  1248. s:='['+gas_op2str[opcode];
  1249. for i:=0 to ops-1 do
  1250. begin
  1251. with oper[i]^ do
  1252. begin
  1253. if i=0 then
  1254. s:=s+' '
  1255. else
  1256. s:=s+',';
  1257. { type }
  1258. addsize:=false;
  1259. if (ot and OT_VREG)=OT_VREG then
  1260. s:=s+'vreg'
  1261. else
  1262. if (ot and OT_FPUREG)=OT_FPUREG then
  1263. s:=s+'fpureg'
  1264. else
  1265. if (ot and OT_REGISTER)=OT_REGISTER then
  1266. begin
  1267. s:=s+'reg';
  1268. addsize:=true;
  1269. end
  1270. else
  1271. if (ot and OT_REGLIST)=OT_REGLIST then
  1272. begin
  1273. s:=s+'reglist';
  1274. addsize:=false;
  1275. end
  1276. else
  1277. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1278. begin
  1279. s:=s+'imm';
  1280. addsize:=true;
  1281. end
  1282. else
  1283. if (ot and OT_MEMORY)=OT_MEMORY then
  1284. begin
  1285. s:=s+'mem';
  1286. addsize:=true;
  1287. if (ot and OT_AM2)<>0 then
  1288. s:=s+' am2 ';
  1289. end
  1290. else
  1291. s:=s+'???';
  1292. { size }
  1293. if addsize then
  1294. begin
  1295. if (ot and OT_BITS8)<>0 then
  1296. s:=s+'8'
  1297. else
  1298. if (ot and OT_BITS16)<>0 then
  1299. s:=s+'24'
  1300. else
  1301. if (ot and OT_BITS32)<>0 then
  1302. s:=s+'32'
  1303. else
  1304. if (ot and OT_BITSSHIFTER)<>0 then
  1305. s:=s+'shifter'
  1306. else
  1307. s:=s+'??';
  1308. { signed }
  1309. if (ot and OT_SIGNED)<>0 then
  1310. s:=s+'s';
  1311. end;
  1312. end;
  1313. end;
  1314. GetString:=s+']';
  1315. end;
  1316. procedure taicpu.ResetPass1;
  1317. begin
  1318. { we need to reset everything here, because the choosen insentry
  1319. can be invalid for a new situation where the previously optimized
  1320. insentry is not correct }
  1321. InsEntry:=nil;
  1322. InsSize:=0;
  1323. LastInsOffset:=-1;
  1324. end;
  1325. procedure taicpu.ResetPass2;
  1326. begin
  1327. { we are here in a second pass, check if the instruction can be optimized }
  1328. if assigned(InsEntry) and
  1329. ((InsEntry^.flags and IF_PASS2)<>0) then
  1330. begin
  1331. InsEntry:=nil;
  1332. InsSize:=0;
  1333. end;
  1334. LastInsOffset:=-1;
  1335. end;
  1336. function taicpu.CheckIfValid:boolean;
  1337. begin
  1338. Result:=False; { unimplemented }
  1339. end;
  1340. function taicpu.Pass1(objdata:TObjData):longint;
  1341. var
  1342. ldr2op : array[PF_B..PF_T] of tasmop = (
  1343. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  1344. str2op : array[PF_B..PF_T] of tasmop = (
  1345. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  1346. begin
  1347. Pass1:=0;
  1348. { Save the old offset and set the new offset }
  1349. InsOffset:=ObjData.CurrObjSec.Size;
  1350. { Error? }
  1351. if (Insentry=nil) and (InsSize=-1) then
  1352. exit;
  1353. { set the file postion }
  1354. current_filepos:=fileinfo;
  1355. { tranlate LDR+postfix to complete opcode }
  1356. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  1357. begin
  1358. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  1359. opcode:=ldr2op[oppostfix]
  1360. else
  1361. internalerror(2005091001);
  1362. if opcode=A_None then
  1363. internalerror(2005091004);
  1364. { postfix has been added to opcode }
  1365. oppostfix:=PF_None;
  1366. end
  1367. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  1368. begin
  1369. if (oppostfix in [low(str2op)..high(str2op)]) then
  1370. opcode:=str2op[oppostfix]
  1371. else
  1372. internalerror(2005091002);
  1373. if opcode=A_None then
  1374. internalerror(2005091003);
  1375. { postfix has been added to opcode }
  1376. oppostfix:=PF_None;
  1377. end;
  1378. { Get InsEntry }
  1379. if FindInsEntry(objdata) then
  1380. begin
  1381. InsSize:=4;
  1382. LastInsOffset:=InsOffset;
  1383. Pass1:=InsSize;
  1384. exit;
  1385. end;
  1386. LastInsOffset:=-1;
  1387. end;
  1388. procedure taicpu.Pass2(objdata:TObjData);
  1389. begin
  1390. { error in pass1 ? }
  1391. if insentry=nil then
  1392. exit;
  1393. current_filepos:=fileinfo;
  1394. { Generate the instruction }
  1395. GenCode(objdata);
  1396. end;
  1397. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1398. begin
  1399. end;
  1400. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1401. begin
  1402. end;
  1403. procedure taicpu.ppubuildderefimploper(var o:toper);
  1404. begin
  1405. end;
  1406. procedure taicpu.ppuderefoper(var o:toper);
  1407. begin
  1408. end;
  1409. function taicpu.InsEnd:longint;
  1410. begin
  1411. Result:=0; { unimplemented }
  1412. end;
  1413. procedure taicpu.create_ot(objdata:TObjData);
  1414. var
  1415. i,l,relsize : longint;
  1416. dummy : byte;
  1417. currsym : TObjSymbol;
  1418. begin
  1419. if ops=0 then
  1420. exit;
  1421. { update oper[].ot field }
  1422. for i:=0 to ops-1 do
  1423. with oper[i]^ do
  1424. begin
  1425. case typ of
  1426. top_regset:
  1427. begin
  1428. ot:=OT_REGLIST;
  1429. end;
  1430. top_reg :
  1431. begin
  1432. case getregtype(reg) of
  1433. R_INTREGISTER:
  1434. ot:=OT_REG32 or OT_SHIFTEROP;
  1435. R_FPUREGISTER:
  1436. ot:=OT_FPUREG;
  1437. else
  1438. internalerror(2005090901);
  1439. end;
  1440. end;
  1441. top_ref :
  1442. begin
  1443. if ref^.refaddr=addr_no then
  1444. begin
  1445. { create ot field }
  1446. { we should get the size here dependend on the
  1447. instruction }
  1448. if (ot and OT_SIZE_MASK)=0 then
  1449. ot:=OT_MEMORY or OT_BITS32
  1450. else
  1451. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1452. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1453. ot:=ot or OT_MEM_OFFS;
  1454. { if we need to fix a reference, we do it here }
  1455. { pc relative addressing }
  1456. if (ref^.base=NR_NO) and
  1457. (ref^.index=NR_NO) and
  1458. (ref^.shiftmode=SM_None)
  1459. { at least we should check if the destination symbol
  1460. is in a text section }
  1461. { and
  1462. (ref^.symbol^.owner="text") } then
  1463. ref^.base:=NR_PC;
  1464. { determine possible address modes }
  1465. if (ref^.base<>NR_NO) and
  1466. (
  1467. (
  1468. (ref^.index=NR_NO) and
  1469. (ref^.shiftmode=SM_None) and
  1470. (ref^.offset>=-4097) and
  1471. (ref^.offset<=4097)
  1472. ) or
  1473. (
  1474. (ref^.shiftmode=SM_None) and
  1475. (ref^.offset=0)
  1476. ) or
  1477. (
  1478. (ref^.index<>NR_NO) and
  1479. (ref^.shiftmode<>SM_None) and
  1480. (ref^.shiftimm<=31) and
  1481. (ref^.offset=0)
  1482. )
  1483. ) then
  1484. ot:=ot or OT_AM2;
  1485. if (ref^.index<>NR_NO) and
  1486. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1487. (
  1488. (ref^.base=NR_NO) and
  1489. (ref^.shiftmode=SM_None) and
  1490. (ref^.offset=0)
  1491. ) then
  1492. ot:=ot or OT_AM4;
  1493. end
  1494. else
  1495. begin
  1496. l:=ref^.offset;
  1497. currsym:=ObjData.symbolref(ref^.symbol);
  1498. if assigned(currsym) then
  1499. inc(l,currsym.address);
  1500. relsize:=(InsOffset+2)-l;
  1501. if (relsize<-33554428) or (relsize>33554428) then
  1502. ot:=OT_IMM32
  1503. else
  1504. ot:=OT_IMM24;
  1505. end;
  1506. end;
  1507. top_local :
  1508. begin
  1509. { we should get the size here dependend on the
  1510. instruction }
  1511. if (ot and OT_SIZE_MASK)=0 then
  1512. ot:=OT_MEMORY or OT_BITS32
  1513. else
  1514. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1515. end;
  1516. top_const :
  1517. begin
  1518. ot:=OT_IMMEDIATE;
  1519. if is_shifter_const(val,dummy) then
  1520. ot:=OT_IMMSHIFTER
  1521. else
  1522. ot:=OT_IMM32
  1523. end;
  1524. top_none :
  1525. begin
  1526. { generated when there was an error in the
  1527. assembler reader. It never happends when generating
  1528. assembler }
  1529. end;
  1530. top_shifterop:
  1531. begin
  1532. ot:=OT_SHIFTEROP;
  1533. end;
  1534. else
  1535. internalerror(200402261);
  1536. end;
  1537. end;
  1538. end;
  1539. function taicpu.Matches(p:PInsEntry):longint;
  1540. { * IF_SM stands for Size Match: any operand whose size is not
  1541. * explicitly specified by the template is `really' intended to be
  1542. * the same size as the first size-specified operand.
  1543. * Non-specification is tolerated in the input instruction, but
  1544. * _wrong_ specification is not.
  1545. *
  1546. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1547. * three-operand instructions such as SHLD: it implies that the
  1548. * first two operands must match in size, but that the third is
  1549. * required to be _unspecified_.
  1550. *
  1551. * IF_SB invokes Size Byte: operands with unspecified size in the
  1552. * template are really bytes, and so no non-byte specification in
  1553. * the input instruction will be tolerated. IF_SW similarly invokes
  1554. * Size Word, and IF_SD invokes Size Doubleword.
  1555. *
  1556. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1557. * that any operand with unspecified size in the template is
  1558. * required to have unspecified size in the instruction too...)
  1559. }
  1560. var
  1561. i{,j,asize,oprs} : longint;
  1562. {siz : array[0..3] of longint;}
  1563. begin
  1564. Matches:=100;
  1565. writeln(getstring,'---');
  1566. { Check the opcode and operands }
  1567. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1568. begin
  1569. Matches:=0;
  1570. exit;
  1571. end;
  1572. { Check that no spurious colons or TOs are present }
  1573. for i:=0 to p^.ops-1 do
  1574. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1575. begin
  1576. Matches:=0;
  1577. exit;
  1578. end;
  1579. { Check that the operand flags all match up }
  1580. for i:=0 to p^.ops-1 do
  1581. begin
  1582. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1583. ((p^.optypes[i] and OT_SIZE_MASK) and
  1584. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1585. begin
  1586. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1587. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1588. begin
  1589. Matches:=0;
  1590. exit;
  1591. end
  1592. else
  1593. Matches:=1;
  1594. end;
  1595. end;
  1596. { check postfixes:
  1597. the existance of a certain postfix requires a
  1598. particular code }
  1599. { update condition flags
  1600. or floating point single }
  1601. if (oppostfix=PF_S) and
  1602. not(p^.code[0] in [#$04]) then
  1603. begin
  1604. Matches:=0;
  1605. exit;
  1606. end;
  1607. { floating point size }
  1608. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1609. not(p^.code[0] in []) then
  1610. begin
  1611. Matches:=0;
  1612. exit;
  1613. end;
  1614. { multiple load/store address modes }
  1615. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1616. not(p^.code[0] in [
  1617. // ldr,str,ldrb,strb
  1618. #$17,
  1619. // stm,ldm
  1620. #$26
  1621. ]) then
  1622. begin
  1623. Matches:=0;
  1624. exit;
  1625. end;
  1626. { we shouldn't see any opsize prefixes here }
  1627. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1628. begin
  1629. Matches:=0;
  1630. exit;
  1631. end;
  1632. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1633. begin
  1634. Matches:=0;
  1635. exit;
  1636. end;
  1637. { Check operand sizes }
  1638. { as default an untyped size can get all the sizes, this is different
  1639. from nasm, but else we need to do a lot checking which opcodes want
  1640. size or not with the automatic size generation }
  1641. (*
  1642. asize:=longint($ffffffff);
  1643. if (p^.flags and IF_SB)<>0 then
  1644. asize:=OT_BITS8
  1645. else if (p^.flags and IF_SW)<>0 then
  1646. asize:=OT_BITS16
  1647. else if (p^.flags and IF_SD)<>0 then
  1648. asize:=OT_BITS32;
  1649. if (p^.flags and IF_ARMASK)<>0 then
  1650. begin
  1651. siz[0]:=0;
  1652. siz[1]:=0;
  1653. siz[2]:=0;
  1654. if (p^.flags and IF_AR0)<>0 then
  1655. siz[0]:=asize
  1656. else if (p^.flags and IF_AR1)<>0 then
  1657. siz[1]:=asize
  1658. else if (p^.flags and IF_AR2)<>0 then
  1659. siz[2]:=asize;
  1660. end
  1661. else
  1662. begin
  1663. { we can leave because the size for all operands is forced to be
  1664. the same
  1665. but not if IF_SB IF_SW or IF_SD is set PM }
  1666. if asize=-1 then
  1667. exit;
  1668. siz[0]:=asize;
  1669. siz[1]:=asize;
  1670. siz[2]:=asize;
  1671. end;
  1672. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1673. begin
  1674. if (p^.flags and IF_SM2)<>0 then
  1675. oprs:=2
  1676. else
  1677. oprs:=p^.ops;
  1678. for i:=0 to oprs-1 do
  1679. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1680. begin
  1681. for j:=0 to oprs-1 do
  1682. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1683. break;
  1684. end;
  1685. end
  1686. else
  1687. oprs:=2;
  1688. { Check operand sizes }
  1689. for i:=0 to p^.ops-1 do
  1690. begin
  1691. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1692. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1693. { Immediates can always include smaller size }
  1694. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1695. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1696. Matches:=2;
  1697. end;
  1698. *)
  1699. end;
  1700. function taicpu.calcsize(p:PInsEntry):shortint;
  1701. begin
  1702. result:=4;
  1703. end;
  1704. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1705. begin
  1706. Result:=False; { unimplemented }
  1707. end;
  1708. procedure taicpu.Swapoperands;
  1709. begin
  1710. end;
  1711. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1712. var
  1713. i : longint;
  1714. begin
  1715. result:=false;
  1716. { Things which may only be done once, not when a second pass is done to
  1717. optimize }
  1718. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1719. begin
  1720. { create the .ot fields }
  1721. create_ot(objdata);
  1722. { set the file postion }
  1723. current_filepos:=fileinfo;
  1724. end
  1725. else
  1726. begin
  1727. { we've already an insentry so it's valid }
  1728. result:=true;
  1729. exit;
  1730. end;
  1731. { Lookup opcode in the table }
  1732. InsSize:=-1;
  1733. i:=instabcache^[opcode];
  1734. if i=-1 then
  1735. begin
  1736. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1737. exit;
  1738. end;
  1739. insentry:=@instab[i];
  1740. while (insentry^.opcode=opcode) do
  1741. begin
  1742. if matches(insentry)=100 then
  1743. begin
  1744. result:=true;
  1745. exit;
  1746. end;
  1747. inc(i);
  1748. insentry:=@instab[i];
  1749. end;
  1750. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1751. { No instruction found, set insentry to nil and inssize to -1 }
  1752. insentry:=nil;
  1753. inssize:=-1;
  1754. end;
  1755. procedure taicpu.gencode(objdata:TObjData);
  1756. var
  1757. bytes : dword;
  1758. i_field : byte;
  1759. procedure setshifterop(op : byte);
  1760. begin
  1761. case oper[op]^.typ of
  1762. top_const:
  1763. begin
  1764. i_field:=1;
  1765. bytes:=bytes or dword(oper[op]^.val and $fff);
  1766. end;
  1767. top_reg:
  1768. begin
  1769. i_field:=0;
  1770. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1771. { does a real shifter op follow? }
  1772. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1773. begin
  1774. end;
  1775. end;
  1776. else
  1777. internalerror(2005091103);
  1778. end;
  1779. end;
  1780. begin
  1781. bytes:=$0;
  1782. { evaluate and set condition code }
  1783. { condition code allowed? }
  1784. { setup rest of the instruction }
  1785. case insentry^.code[0] of
  1786. #$08:
  1787. begin
  1788. { set instruction code }
  1789. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1790. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1791. { set destination }
  1792. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1793. { create shifter op }
  1794. setshifterop(1);
  1795. { set i field }
  1796. bytes:=bytes or (i_field shl 25);
  1797. { set s if necessary }
  1798. if oppostfix=PF_S then
  1799. bytes:=bytes or (1 shl 20);
  1800. end;
  1801. #$ff:
  1802. internalerror(2005091101);
  1803. else
  1804. internalerror(2005091102);
  1805. end;
  1806. { we're finished, write code }
  1807. objdata.writebytes(bytes,sizeof(bytes));
  1808. end;
  1809. {$ifdef dummy}
  1810. (*
  1811. static void gencode (long segment, long offset, int bits,
  1812. insn *ins, char *codes, long insn_end)
  1813. {
  1814. int has_S_code; /* S - setflag */
  1815. int has_B_code; /* B - setflag */
  1816. int has_T_code; /* T - setflag */
  1817. int has_W_code; /* ! => W flag */
  1818. int has_F_code; /* ^ => S flag */
  1819. int keep;
  1820. unsigned char c;
  1821. unsigned char bytes[4];
  1822. long data, size;
  1823. static int cc_code[] = /* bit pattern of cc */
  1824. { /* order as enum in */
  1825. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1826. 0x0A, 0x0C, 0x08, 0x0D,
  1827. 0x09, 0x0B, 0x04, 0x01,
  1828. 0x05, 0x07, 0x06,
  1829. };
  1830. #ifdef DEBUG
  1831. static char *CC[] =
  1832. { /* condition code names */
  1833. "AL", "CC", "CS", "EQ",
  1834. "GE", "GT", "HI", "LE",
  1835. "LS", "LT", "MI", "NE",
  1836. "PL", "VC", "VS", "",
  1837. "S"
  1838. };
  1839. has_S_code = (ins->condition & C_SSETFLAG);
  1840. has_B_code = (ins->condition & C_BSETFLAG);
  1841. has_T_code = (ins->condition & C_TSETFLAG);
  1842. has_W_code = (ins->condition & C_EXSETFLAG);
  1843. has_F_code = (ins->condition & C_FSETFLAG);
  1844. ins->condition = (ins->condition & 0x0F);
  1845. if (rt_debug)
  1846. {
  1847. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1848. CC[ins->condition & 0x0F]);
  1849. if (has_S_code)
  1850. printf ("S");
  1851. if (has_B_code)
  1852. printf ("B");
  1853. if (has_T_code)
  1854. printf ("T");
  1855. if (has_W_code)
  1856. printf ("!");
  1857. if (has_F_code)
  1858. printf ("^");
  1859. printf ("\n");
  1860. c = *codes;
  1861. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1862. bytes[0] = 0xB;
  1863. bytes[1] = 0xE;
  1864. bytes[2] = 0xE;
  1865. bytes[3] = 0xF;
  1866. }
  1867. // First condition code in upper nibble
  1868. if (ins->condition < C_NONE)
  1869. {
  1870. c = cc_code[ins->condition] << 4;
  1871. }
  1872. else
  1873. {
  1874. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1875. }
  1876. switch (keep = *codes)
  1877. {
  1878. case 1:
  1879. // B, BL
  1880. ++codes;
  1881. c |= *codes++;
  1882. bytes[0] = c;
  1883. if (ins->oprs[0].segment != segment)
  1884. {
  1885. // fais une relocation
  1886. c = 1;
  1887. data = 0; // Let the linker locate ??
  1888. }
  1889. else
  1890. {
  1891. c = 0;
  1892. data = ins->oprs[0].offset - (offset + 8);
  1893. if (data % 4)
  1894. {
  1895. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1896. }
  1897. }
  1898. if (data >= 0x1000)
  1899. {
  1900. errfunc (ERR_NONFATAL, "too long offset");
  1901. }
  1902. data = data >> 2;
  1903. bytes[1] = (data >> 16) & 0xFF;
  1904. bytes[2] = (data >> 8) & 0xFF;
  1905. bytes[3] = (data ) & 0xFF;
  1906. if (c == 1)
  1907. {
  1908. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1909. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1910. }
  1911. else
  1912. {
  1913. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1914. }
  1915. return;
  1916. case 2:
  1917. // SWI
  1918. ++codes;
  1919. c |= *codes++;
  1920. bytes[0] = c;
  1921. data = ins->oprs[0].offset;
  1922. bytes[1] = (data >> 16) & 0xFF;
  1923. bytes[2] = (data >> 8) & 0xFF;
  1924. bytes[3] = (data) & 0xFF;
  1925. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1926. return;
  1927. case 3:
  1928. // BX
  1929. ++codes;
  1930. c |= *codes++;
  1931. bytes[0] = c;
  1932. bytes[1] = *codes++;
  1933. bytes[2] = *codes++;
  1934. bytes[3] = *codes++;
  1935. c = regval (&ins->oprs[0],1);
  1936. if (c == 15) // PC
  1937. {
  1938. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1939. }
  1940. else if (c > 15)
  1941. {
  1942. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1943. }
  1944. bytes[3] |= (c & 0x0F);
  1945. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1946. return;
  1947. case 4: // AND Rd,Rn,Rm
  1948. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1949. case 6: // AND Rd,Rn,Rm,<shift>imm
  1950. case 7: // AND Rd,Rn,<shift>imm
  1951. ++codes;
  1952. #ifdef DEBUG
  1953. if (rt_debug)
  1954. {
  1955. printf (" decode - '0x%02X'\n", keep);
  1956. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1957. }
  1958. #endif
  1959. bytes[0] = c | *codes;
  1960. ++codes;
  1961. bytes[1] = *codes;
  1962. if (has_S_code)
  1963. bytes[1] |= 0x10;
  1964. c = regval (&ins->oprs[1],1);
  1965. // Rn in low nibble
  1966. bytes[1] |= c;
  1967. // Rd in high nibble
  1968. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1969. if (keep != 7)
  1970. {
  1971. // Rm in low nibble
  1972. bytes[3] = regval (&ins->oprs[2],1);
  1973. }
  1974. // Shifts if any
  1975. if (keep == 5 || keep == 6)
  1976. {
  1977. // Shift in bytes 2 and 3
  1978. if (keep == 5)
  1979. {
  1980. // Rs
  1981. c = regval (&ins->oprs[3],1);
  1982. bytes[2] |= c;
  1983. c = 0x10; // Set bit 4 in byte[3]
  1984. }
  1985. if (keep == 6)
  1986. {
  1987. c = (ins->oprs[3].offset) & 0x1F;
  1988. // #imm
  1989. bytes[2] |= c >> 1;
  1990. if (c & 0x01)
  1991. {
  1992. bytes[3] |= 0x80;
  1993. }
  1994. c = 0; // Clr bit 4 in byte[3]
  1995. }
  1996. // <shift>
  1997. c |= shiftval (&ins->oprs[3]) << 5;
  1998. bytes[3] |= c;
  1999. }
  2000. // reg,reg,imm
  2001. if (keep == 7)
  2002. {
  2003. int shimm;
  2004. shimm = imm_shift (ins->oprs[2].offset);
  2005. if (shimm == -1)
  2006. {
  2007. errfunc (ERR_NONFATAL, "cannot create that constant");
  2008. }
  2009. bytes[3] = shimm & 0xFF;
  2010. bytes[2] |= (shimm & 0xF00) >> 8;
  2011. }
  2012. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2013. return;
  2014. case 8: // MOV Rd,Rm
  2015. case 9: // MOV Rd,Rm,<shift>Rs
  2016. case 0xA: // MOV Rd,Rm,<shift>imm
  2017. case 0xB: // MOV Rd,<shift>imm
  2018. ++codes;
  2019. #ifdef DEBUG
  2020. if (rt_debug)
  2021. {
  2022. printf (" decode - '0x%02X'\n", keep);
  2023. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  2024. }
  2025. #endif
  2026. bytes[0] = c | *codes;
  2027. ++codes;
  2028. bytes[1] = *codes;
  2029. if (has_S_code)
  2030. bytes[1] |= 0x10;
  2031. // Rd in high nibble
  2032. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2033. if (keep != 0x0B)
  2034. {
  2035. // Rm in low nibble
  2036. bytes[3] = regval (&ins->oprs[1],1);
  2037. }
  2038. // Shifts if any
  2039. if (keep == 0x09 || keep == 0x0A)
  2040. {
  2041. // Shift in bytes 2 and 3
  2042. if (keep == 0x09)
  2043. {
  2044. // Rs
  2045. c = regval (&ins->oprs[2],1);
  2046. bytes[2] |= c;
  2047. c = 0x10; // Set bit 4 in byte[3]
  2048. }
  2049. if (keep == 0x0A)
  2050. {
  2051. c = (ins->oprs[2].offset) & 0x1F;
  2052. // #imm
  2053. bytes[2] |= c >> 1;
  2054. if (c & 0x01)
  2055. {
  2056. bytes[3] |= 0x80;
  2057. }
  2058. c = 0; // Clr bit 4 in byte[3]
  2059. }
  2060. // <shift>
  2061. c |= shiftval (&ins->oprs[2]) << 5;
  2062. bytes[3] |= c;
  2063. }
  2064. // reg,imm
  2065. if (keep == 0x0B)
  2066. {
  2067. int shimm;
  2068. shimm = imm_shift (ins->oprs[1].offset);
  2069. if (shimm == -1)
  2070. {
  2071. errfunc (ERR_NONFATAL, "cannot create that constant");
  2072. }
  2073. bytes[3] = shimm & 0xFF;
  2074. bytes[2] |= (shimm & 0xF00) >> 8;
  2075. }
  2076. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2077. return;
  2078. case 0xC: // CMP Rn,Rm
  2079. case 0xD: // CMP Rn,Rm,<shift>Rs
  2080. case 0xE: // CMP Rn,Rm,<shift>imm
  2081. case 0xF: // CMP Rn,<shift>imm
  2082. ++codes;
  2083. bytes[0] = c | *codes++;
  2084. bytes[1] = *codes;
  2085. // Implicit S code
  2086. bytes[1] |= 0x10;
  2087. c = regval (&ins->oprs[0],1);
  2088. // Rn in low nibble
  2089. bytes[1] |= c;
  2090. // No destination
  2091. bytes[2] = 0;
  2092. if (keep != 0x0B)
  2093. {
  2094. // Rm in low nibble
  2095. bytes[3] = regval (&ins->oprs[1],1);
  2096. }
  2097. // Shifts if any
  2098. if (keep == 0x0D || keep == 0x0E)
  2099. {
  2100. // Shift in bytes 2 and 3
  2101. if (keep == 0x0D)
  2102. {
  2103. // Rs
  2104. c = regval (&ins->oprs[2],1);
  2105. bytes[2] |= c;
  2106. c = 0x10; // Set bit 4 in byte[3]
  2107. }
  2108. if (keep == 0x0E)
  2109. {
  2110. c = (ins->oprs[2].offset) & 0x1F;
  2111. // #imm
  2112. bytes[2] |= c >> 1;
  2113. if (c & 0x01)
  2114. {
  2115. bytes[3] |= 0x80;
  2116. }
  2117. c = 0; // Clr bit 4 in byte[3]
  2118. }
  2119. // <shift>
  2120. c |= shiftval (&ins->oprs[2]) << 5;
  2121. bytes[3] |= c;
  2122. }
  2123. // reg,imm
  2124. if (keep == 0x0F)
  2125. {
  2126. int shimm;
  2127. shimm = imm_shift (ins->oprs[1].offset);
  2128. if (shimm == -1)
  2129. {
  2130. errfunc (ERR_NONFATAL, "cannot create that constant");
  2131. }
  2132. bytes[3] = shimm & 0xFF;
  2133. bytes[2] |= (shimm & 0xF00) >> 8;
  2134. }
  2135. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2136. return;
  2137. case 0x10: // MRS Rd,<psr>
  2138. ++codes;
  2139. bytes[0] = c | *codes++;
  2140. bytes[1] = *codes++;
  2141. // Rd
  2142. c = regval (&ins->oprs[0],1);
  2143. bytes[2] = c << 4;
  2144. bytes[3] = 0;
  2145. c = ins->oprs[1].basereg;
  2146. if (c == R_CPSR || c == R_SPSR)
  2147. {
  2148. if (c == R_SPSR)
  2149. {
  2150. bytes[1] |= 0x40;
  2151. }
  2152. }
  2153. else
  2154. {
  2155. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  2156. }
  2157. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2158. return;
  2159. case 0x11: // MSR <psr>,Rm
  2160. case 0x12: // MSR <psrf>,Rm
  2161. case 0x13: // MSR <psrf>,#expression
  2162. ++codes;
  2163. bytes[0] = c | *codes++;
  2164. bytes[1] = *codes++;
  2165. bytes[2] = *codes;
  2166. if (keep == 0x11 || keep == 0x12)
  2167. {
  2168. // Rm
  2169. c = regval (&ins->oprs[1],1);
  2170. bytes[3] = c;
  2171. }
  2172. else
  2173. {
  2174. int shimm;
  2175. shimm = imm_shift (ins->oprs[1].offset);
  2176. if (shimm == -1)
  2177. {
  2178. errfunc (ERR_NONFATAL, "cannot create that constant");
  2179. }
  2180. bytes[3] = shimm & 0xFF;
  2181. bytes[2] |= (shimm & 0xF00) >> 8;
  2182. }
  2183. c = ins->oprs[0].basereg;
  2184. if ( keep == 0x11)
  2185. {
  2186. if ( c == R_CPSR || c == R_SPSR)
  2187. {
  2188. if ( c== R_SPSR)
  2189. {
  2190. bytes[1] |= 0x40;
  2191. }
  2192. }
  2193. else
  2194. {
  2195. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  2196. }
  2197. }
  2198. else
  2199. {
  2200. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  2201. {
  2202. if ( c== R_SPSR_FLG)
  2203. {
  2204. bytes[1] |= 0x40;
  2205. }
  2206. }
  2207. else
  2208. {
  2209. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  2210. }
  2211. }
  2212. break;
  2213. case 0x14: // MUL Rd,Rm,Rs
  2214. case 0x15: // MULA Rd,Rm,Rs,Rn
  2215. ++codes;
  2216. bytes[0] = c | *codes++;
  2217. bytes[1] = *codes++;
  2218. bytes[3] = *codes;
  2219. // Rd
  2220. bytes[1] |= regval (&ins->oprs[0],1);
  2221. if (has_S_code)
  2222. bytes[1] |= 0x10;
  2223. // Rm
  2224. bytes[3] |= regval (&ins->oprs[1],1);
  2225. // Rs
  2226. bytes[2] = regval (&ins->oprs[2],1);
  2227. if (keep == 0x15)
  2228. {
  2229. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  2230. }
  2231. break;
  2232. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  2233. ++codes;
  2234. bytes[0] = c | *codes++;
  2235. bytes[1] = *codes++;
  2236. bytes[3] = *codes;
  2237. // RdHi
  2238. bytes[1] |= regval (&ins->oprs[1],1);
  2239. if (has_S_code)
  2240. bytes[1] |= 0x10;
  2241. // RdLo
  2242. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2243. // Rm
  2244. bytes[3] |= regval (&ins->oprs[2],1);
  2245. // Rs
  2246. bytes[2] |= regval (&ins->oprs[3],1);
  2247. break;
  2248. case 0x17: // LDR Rd, expression
  2249. ++codes;
  2250. bytes[0] = c | *codes++;
  2251. bytes[1] = *codes++;
  2252. // Rd
  2253. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2254. if (has_B_code)
  2255. bytes[1] |= 0x40;
  2256. if (has_T_code)
  2257. {
  2258. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  2259. }
  2260. if (has_W_code)
  2261. {
  2262. errfunc (ERR_NONFATAL, "'!' not allowed");
  2263. }
  2264. // Rn - implicit R15
  2265. bytes[1] |= 0xF;
  2266. if (ins->oprs[1].segment != segment)
  2267. {
  2268. errfunc (ERR_NONFATAL, "label not in same segment");
  2269. }
  2270. data = ins->oprs[1].offset - (offset + 8);
  2271. if (data < 0)
  2272. {
  2273. data = -data;
  2274. }
  2275. else
  2276. {
  2277. bytes[1] |= 0x80;
  2278. }
  2279. if (data >= 0x1000)
  2280. {
  2281. errfunc (ERR_NONFATAL, "too long offset");
  2282. }
  2283. bytes[2] |= ((data & 0xF00) >> 8);
  2284. bytes[3] = data & 0xFF;
  2285. break;
  2286. case 0x18: // LDR Rd, [Rn]
  2287. ++codes;
  2288. bytes[0] = c | *codes++;
  2289. bytes[1] = *codes++;
  2290. // Rd
  2291. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2292. if (has_B_code)
  2293. bytes[1] |= 0x40;
  2294. if (has_T_code)
  2295. {
  2296. bytes[1] |= 0x20; // write-back
  2297. }
  2298. else
  2299. {
  2300. bytes[0] |= 0x01; // implicit pre-index mode
  2301. }
  2302. if (has_W_code)
  2303. {
  2304. bytes[1] |= 0x20; // write-back
  2305. }
  2306. // Rn
  2307. c = regval (&ins->oprs[1],1);
  2308. bytes[1] |= c;
  2309. if (c == 0x15) // R15
  2310. data = -8;
  2311. else
  2312. data = 0;
  2313. if (data < 0)
  2314. {
  2315. data = -data;
  2316. }
  2317. else
  2318. {
  2319. bytes[1] |= 0x80;
  2320. }
  2321. bytes[2] |= ((data & 0xF00) >> 8);
  2322. bytes[3] = data & 0xFF;
  2323. break;
  2324. case 0x19: // LDR Rd, [Rn,#expression]
  2325. case 0x20: // LDR Rd, [Rn,Rm]
  2326. case 0x21: // LDR Rd, [Rn,Rm,shift]
  2327. ++codes;
  2328. bytes[0] = c | *codes++;
  2329. bytes[1] = *codes++;
  2330. // Rd
  2331. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2332. if (has_B_code)
  2333. bytes[1] |= 0x40;
  2334. // Rn
  2335. c = regval (&ins->oprs[1],1);
  2336. bytes[1] |= c;
  2337. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  2338. {
  2339. bytes[0] |= 0x01; // pre-index mode
  2340. if (has_W_code)
  2341. {
  2342. bytes[1] |= 0x20;
  2343. }
  2344. if (has_T_code)
  2345. {
  2346. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  2347. }
  2348. }
  2349. else
  2350. {
  2351. if (has_T_code) // Forced write-back in post-index mode
  2352. {
  2353. bytes[1] |= 0x20;
  2354. }
  2355. if (has_W_code)
  2356. {
  2357. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  2358. }
  2359. }
  2360. if (keep == 0x19)
  2361. {
  2362. data = ins->oprs[2].offset;
  2363. if (data < 0)
  2364. {
  2365. data = -data;
  2366. }
  2367. else
  2368. {
  2369. bytes[1] |= 0x80;
  2370. }
  2371. if (data >= 0x1000)
  2372. {
  2373. errfunc (ERR_NONFATAL, "too long offset");
  2374. }
  2375. bytes[2] |= ((data & 0xF00) >> 8);
  2376. bytes[3] = data & 0xFF;
  2377. }
  2378. else
  2379. {
  2380. if (ins->oprs[2].minus == 0)
  2381. {
  2382. bytes[1] |= 0x80;
  2383. }
  2384. c = regval (&ins->oprs[2],1);
  2385. bytes[3] = c;
  2386. if (keep == 0x21)
  2387. {
  2388. c = ins->oprs[3].offset;
  2389. if (c > 0x1F)
  2390. {
  2391. errfunc (ERR_NONFATAL, "too large shiftvalue");
  2392. c = c & 0x1F;
  2393. }
  2394. bytes[2] |= c >> 1;
  2395. if (c & 0x01)
  2396. {
  2397. bytes[3] |= 0x80;
  2398. }
  2399. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  2400. }
  2401. }
  2402. break;
  2403. case 0x22: // LDRH Rd, expression
  2404. ++codes;
  2405. bytes[0] = c | 0x01; // Implicit pre-index
  2406. bytes[1] = *codes++;
  2407. // Rd
  2408. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2409. // Rn - implicit R15
  2410. bytes[1] |= 0xF;
  2411. if (ins->oprs[1].segment != segment)
  2412. {
  2413. errfunc (ERR_NONFATAL, "label not in same segment");
  2414. }
  2415. data = ins->oprs[1].offset - (offset + 8);
  2416. if (data < 0)
  2417. {
  2418. data = -data;
  2419. }
  2420. else
  2421. {
  2422. bytes[1] |= 0x80;
  2423. }
  2424. if (data >= 0x100)
  2425. {
  2426. errfunc (ERR_NONFATAL, "too long offset");
  2427. }
  2428. bytes[3] = *codes++;
  2429. bytes[2] |= ((data & 0xF0) >> 4);
  2430. bytes[3] |= data & 0xF;
  2431. break;
  2432. case 0x23: // LDRH Rd, Rn
  2433. ++codes;
  2434. bytes[0] = c | 0x01; // Implicit pre-index
  2435. bytes[1] = *codes++;
  2436. // Rd
  2437. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2438. // Rn
  2439. c = regval (&ins->oprs[1],1);
  2440. bytes[1] |= c;
  2441. if (c == 0x15) // R15
  2442. data = -8;
  2443. else
  2444. data = 0;
  2445. if (data < 0)
  2446. {
  2447. data = -data;
  2448. }
  2449. else
  2450. {
  2451. bytes[1] |= 0x80;
  2452. }
  2453. if (data >= 0x100)
  2454. {
  2455. errfunc (ERR_NONFATAL, "too long offset");
  2456. }
  2457. bytes[3] = *codes++;
  2458. bytes[2] |= ((data & 0xF0) >> 4);
  2459. bytes[3] |= data & 0xF;
  2460. break;
  2461. case 0x24: // LDRH Rd, Rn, expression
  2462. case 0x25: // LDRH Rd, Rn, Rm
  2463. ++codes;
  2464. bytes[0] = c;
  2465. bytes[1] = *codes++;
  2466. // Rd
  2467. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2468. // Rn
  2469. c = regval (&ins->oprs[1],1);
  2470. bytes[1] |= c;
  2471. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  2472. {
  2473. bytes[0] |= 0x01; // pre-index mode
  2474. if (has_W_code)
  2475. {
  2476. bytes[1] |= 0x20;
  2477. }
  2478. }
  2479. else
  2480. {
  2481. if (has_W_code)
  2482. {
  2483. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  2484. }
  2485. }
  2486. bytes[3] = *codes++;
  2487. if (keep == 0x24)
  2488. {
  2489. data = ins->oprs[2].offset;
  2490. if (data < 0)
  2491. {
  2492. data = -data;
  2493. }
  2494. else
  2495. {
  2496. bytes[1] |= 0x80;
  2497. }
  2498. if (data >= 0x100)
  2499. {
  2500. errfunc (ERR_NONFATAL, "too long offset");
  2501. }
  2502. bytes[2] |= ((data & 0xF0) >> 4);
  2503. bytes[3] |= data & 0xF;
  2504. }
  2505. else
  2506. {
  2507. if (ins->oprs[2].minus == 0)
  2508. {
  2509. bytes[1] |= 0x80;
  2510. }
  2511. c = regval (&ins->oprs[2],1);
  2512. bytes[3] |= c;
  2513. }
  2514. break;
  2515. case 0x26: // LDM/STM Rn, {reg-list}
  2516. ++codes;
  2517. bytes[0] = c;
  2518. bytes[0] |= ( *codes >> 4) & 0xF;
  2519. bytes[1] = ( *codes << 4) & 0xF0;
  2520. ++codes;
  2521. if (has_W_code)
  2522. {
  2523. bytes[1] |= 0x20;
  2524. }
  2525. if (has_F_code)
  2526. {
  2527. bytes[1] |= 0x40;
  2528. }
  2529. // Rn
  2530. bytes[1] |= regval (&ins->oprs[0],1);
  2531. data = ins->oprs[1].basereg;
  2532. bytes[2] = ((data >> 8) & 0xFF);
  2533. bytes[3] = (data & 0xFF);
  2534. break;
  2535. case 0x27: // SWP Rd, Rm, [Rn]
  2536. ++codes;
  2537. bytes[0] = c;
  2538. bytes[0] |= *codes++;
  2539. bytes[1] = regval (&ins->oprs[2],1);
  2540. if (has_B_code)
  2541. {
  2542. bytes[1] |= 0x40;
  2543. }
  2544. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2545. bytes[3] = *codes++;
  2546. bytes[3] |= regval (&ins->oprs[1],1);
  2547. break;
  2548. default:
  2549. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2550. bytes[0] = c;
  2551. // And a fix nibble
  2552. ++codes;
  2553. bytes[0] |= *codes++;
  2554. if ( *codes == 0x01) // An I bit
  2555. {
  2556. }
  2557. if ( *codes == 0x02) // An I bit
  2558. {
  2559. }
  2560. ++codes;
  2561. }
  2562. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2563. }
  2564. *)
  2565. {$endif dummy}
  2566. constructor tai_thumb_func.create;
  2567. begin
  2568. inherited create;
  2569. typ:=ait_thumb_func;
  2570. end;
  2571. begin
  2572. cai_align:=tai_align;
  2573. end.