aasmcpu.pas 96 KB

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