aasmcpu.pas 109 KB

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