aasmcpu.pas 96 KB

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