aasmcpu.pas 99 KB

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