aasmcpu.pas 90 KB

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