aasmcpu.pas 97 KB

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