c67-gen.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  1. /*
  2. * TMS320C67xx code generator for TCC
  3. *
  4. * Copyright (c) 2001, 2002 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. //#define ASSEMBLY_LISTING_C67
  21. /* number of available registers */
  22. #define NB_REGS 24
  23. /* a register can belong to several classes. The classes must be
  24. sorted from more general to more precise (see gv2() code which does
  25. assumptions on it). */
  26. #define RC_INT 0x0001 /* generic integer register */
  27. #define RC_FLOAT 0x0002 /* generic float register */
  28. #define RC_EAX 0x0004
  29. #define RC_ST0 0x0008
  30. #define RC_ECX 0x0010
  31. #define RC_EDX 0x0020
  32. #define RC_INT_BSIDE 0x00000040 /* generic integer register on b side */
  33. #define RC_C67_A4 0x00000100
  34. #define RC_C67_A5 0x00000200
  35. #define RC_C67_B4 0x00000400
  36. #define RC_C67_B5 0x00000800
  37. #define RC_C67_A6 0x00001000
  38. #define RC_C67_A7 0x00002000
  39. #define RC_C67_B6 0x00004000
  40. #define RC_C67_B7 0x00008000
  41. #define RC_C67_A8 0x00010000
  42. #define RC_C67_A9 0x00020000
  43. #define RC_C67_B8 0x00040000
  44. #define RC_C67_B9 0x00080000
  45. #define RC_C67_A10 0x00100000
  46. #define RC_C67_A11 0x00200000
  47. #define RC_C67_B10 0x00400000
  48. #define RC_C67_B11 0x00800000
  49. #define RC_C67_A12 0x01000000
  50. #define RC_C67_A13 0x02000000
  51. #define RC_C67_B12 0x04000000
  52. #define RC_C67_B13 0x08000000
  53. #define RC_IRET RC_C67_A4 /* function return: integer register */
  54. #define RC_LRET RC_C67_A5 /* function return: second integer register */
  55. #define RC_FRET RC_C67_A4 /* function return: float register */
  56. /* pretty names for the registers */
  57. enum {
  58. TREG_EAX = 0, // really A2
  59. TREG_ECX, // really A3
  60. TREG_EDX, // really B0
  61. TREG_ST0, // really B1
  62. TREG_C67_A4,
  63. TREG_C67_A5,
  64. TREG_C67_B4,
  65. TREG_C67_B5,
  66. TREG_C67_A6,
  67. TREG_C67_A7,
  68. TREG_C67_B6,
  69. TREG_C67_B7,
  70. TREG_C67_A8,
  71. TREG_C67_A9,
  72. TREG_C67_B8,
  73. TREG_C67_B9,
  74. TREG_C67_A10,
  75. TREG_C67_A11,
  76. TREG_C67_B10,
  77. TREG_C67_B11,
  78. TREG_C67_A12,
  79. TREG_C67_A13,
  80. TREG_C67_B12,
  81. TREG_C67_B13,
  82. };
  83. int reg_classes[NB_REGS] = {
  84. /* eax */ RC_INT | RC_FLOAT | RC_EAX,
  85. // only allow even regs for floats (allow for doubles)
  86. /* ecx */ RC_INT | RC_ECX,
  87. /* edx */ RC_INT | RC_INT_BSIDE | RC_FLOAT | RC_EDX,
  88. // only allow even regs for floats (allow for doubles)
  89. /* st0 */ RC_INT | RC_INT_BSIDE | RC_ST0,
  90. /* A4 */ RC_C67_A4,
  91. /* A5 */ RC_C67_A5,
  92. /* B4 */ RC_C67_B4,
  93. /* B5 */ RC_C67_B5,
  94. /* A6 */ RC_C67_A6,
  95. /* A7 */ RC_C67_A7,
  96. /* B6 */ RC_C67_B6,
  97. /* B7 */ RC_C67_B7,
  98. /* A8 */ RC_C67_A8,
  99. /* A9 */ RC_C67_A9,
  100. /* B8 */ RC_C67_B8,
  101. /* B9 */ RC_C67_B9,
  102. /* A10 */ RC_C67_A10,
  103. /* A11 */ RC_C67_A11,
  104. /* B10 */ RC_C67_B10,
  105. /* B11 */ RC_C67_B11,
  106. /* A12 */ RC_C67_A10,
  107. /* A13 */ RC_C67_A11,
  108. /* B12 */ RC_C67_B10,
  109. /* B13 */ RC_C67_B11
  110. };
  111. /* return registers for function */
  112. #define REG_IRET TREG_C67_A4 /* single word int return register */
  113. #define REG_LRET TREG_C67_A5 /* second word return register (for long long) */
  114. #define REG_FRET TREG_C67_A4 /* float return register */
  115. #define ALWAYS_ASSERT(x) \
  116. do {\
  117. if (!(x))\
  118. error("internal compiler error file at %s:%d", __FILE__, __LINE__);\
  119. } while (0)
  120. // although tcc thinks it is passing parameters on the stack,
  121. // the C67 really passes up to the first 10 params in special
  122. // regs or regs pairs (for 64 bit params). So keep track of
  123. // the stack offsets so we can translate to the appropriate
  124. // reg (pair)
  125. #define NoCallArgsPassedOnStack 10
  126. int NoOfCurFuncArgs;
  127. int TranslateStackToReg[NoCallArgsPassedOnStack];
  128. int ParamLocOnStack[NoCallArgsPassedOnStack];
  129. int TotalBytesPushedOnStack;
  130. /* defined if function parameters must be evaluated in reverse order */
  131. //#define INVERT_FUNC_PARAMS
  132. /* defined if structures are passed as pointers. Otherwise structures
  133. are directly pushed on stack. */
  134. //#define FUNC_STRUCT_PARAM_AS_PTR
  135. /* pointer size, in bytes */
  136. #define PTR_SIZE 4
  137. /* long double size and alignment, in bytes */
  138. #define LDOUBLE_SIZE 12
  139. #define LDOUBLE_ALIGN 4
  140. /* maximum alignment (for aligned attribute support) */
  141. #define MAX_ALIGN 8
  142. /******************************************************/
  143. /* ELF defines */
  144. #define EM_TCC_TARGET EM_C60
  145. /* relocation type for 32 bit data relocation */
  146. #define R_DATA_32 R_C60_32
  147. #define R_JMP_SLOT R_C60_JMP_SLOT
  148. #define R_COPY R_C60_COPY
  149. #define ELF_START_ADDR 0x00000400
  150. #define ELF_PAGE_SIZE 0x1000
  151. /******************************************************/
  152. static unsigned long func_sub_sp_offset;
  153. static int func_ret_sub;
  154. static BOOL C67_invert_test;
  155. static int C67_compare_reg;
  156. #ifdef ASSEMBLY_LISTING_C67
  157. FILE *f = NULL;
  158. #endif
  159. void C67_g(int c)
  160. {
  161. int ind1;
  162. #ifdef ASSEMBLY_LISTING_C67
  163. fprintf(f, " %08X", c);
  164. #endif
  165. ind1 = ind + 4;
  166. if (ind1 > (int) cur_text_section->data_allocated)
  167. section_realloc(cur_text_section, ind1);
  168. cur_text_section->data[ind] = c & 0xff;
  169. cur_text_section->data[ind + 1] = (c >> 8) & 0xff;
  170. cur_text_section->data[ind + 2] = (c >> 16) & 0xff;
  171. cur_text_section->data[ind + 3] = (c >> 24) & 0xff;
  172. ind = ind1;
  173. }
  174. /* output a symbol and patch all calls to it */
  175. void gsym_addr(int t, int a)
  176. {
  177. int n, *ptr;
  178. while (t) {
  179. ptr = (int *) (cur_text_section->data + t);
  180. {
  181. Sym *sym;
  182. // extract 32 bit address from MVKH/MVKL
  183. n = ((*ptr >> 7) & 0xffff);
  184. n |= ((*(ptr + 1) >> 7) & 0xffff) << 16;
  185. // define a label that will be relocated
  186. sym = get_sym_ref(&char_pointer_type, cur_text_section, a, 0);
  187. greloc(cur_text_section, sym, t, R_C60LO16);
  188. greloc(cur_text_section, sym, t + 4, R_C60HI16);
  189. // clear out where the pointer was
  190. *ptr &= ~(0xffff << 7);
  191. *(ptr + 1) &= ~(0xffff << 7);
  192. }
  193. t = n;
  194. }
  195. }
  196. void gsym(int t)
  197. {
  198. gsym_addr(t, ind);
  199. }
  200. // these are regs that tcc doesn't really know about,
  201. // but assign them unique values so the mapping routines
  202. // can distinquish them
  203. #define C67_A0 105
  204. #define C67_SP 106
  205. #define C67_B3 107
  206. #define C67_FP 108
  207. #define C67_B2 109
  208. #define C67_CREG_ZERO -1 // Special code for no condition reg test
  209. int ConvertRegToRegClass(int r)
  210. {
  211. // only works for A4-B13
  212. return RC_C67_A4 << (r - TREG_C67_A4);
  213. }
  214. // map TCC reg to C67 reg number
  215. int C67_map_regn(int r)
  216. {
  217. if (r == 0) // normal tcc regs
  218. return 0x2; // A2
  219. else if (r == 1) // normal tcc regs
  220. return 3; // A3
  221. else if (r == 2) // normal tcc regs
  222. return 0; // B0
  223. else if (r == 3) // normal tcc regs
  224. return 1; // B1
  225. else if (r >= TREG_C67_A4 && r <= TREG_C67_B13) // these form a pattern of alt pairs
  226. return (((r & 0xfffffffc) >> 1) | (r & 1)) + 2;
  227. else if (r == C67_A0)
  228. return 0; // set to A0 (offset reg)
  229. else if (r == C67_B2)
  230. return 2; // set to B2 (offset reg)
  231. else if (r == C67_B3)
  232. return 3; // set to B3 (return address reg)
  233. else if (r == C67_SP)
  234. return 15; // set to SP (B15) (offset reg)
  235. else if (r == C67_FP)
  236. return 15; // set to FP (A15) (offset reg)
  237. else if (r == C67_CREG_ZERO)
  238. return 0; // Special code for no condition reg test
  239. else
  240. ALWAYS_ASSERT(FALSE);
  241. return 0;
  242. }
  243. // mapping from tcc reg number to
  244. // C67 register to condition code field
  245. //
  246. // valid condition code regs are:
  247. //
  248. // tcc reg 2 ->B0 -> 1
  249. // tcc reg 3 ->B1 -> 2
  250. // tcc reg 0 -> A2 -> 5
  251. // tcc reg 1 -> A3 -> X
  252. // tcc reg B2 -> 3
  253. int C67_map_regc(int r)
  254. {
  255. if (r == 0) // normal tcc regs
  256. return 0x5;
  257. else if (r == 2) // normal tcc regs
  258. return 0x1;
  259. else if (r == 3) // normal tcc regs
  260. return 0x2;
  261. else if (r == C67_B2) // normal tcc regs
  262. return 0x3;
  263. else if (r == C67_CREG_ZERO)
  264. return 0; // Special code for no condition reg test
  265. else
  266. ALWAYS_ASSERT(FALSE);
  267. return 0;
  268. }
  269. // map TCC reg to C67 reg side A or B
  270. int C67_map_regs(int r)
  271. {
  272. if (r == 0) // normal tcc regs
  273. return 0x0;
  274. else if (r == 1) // normal tcc regs
  275. return 0x0;
  276. else if (r == 2) // normal tcc regs
  277. return 0x1;
  278. else if (r == 3) // normal tcc regs
  279. return 0x1;
  280. else if (r >= TREG_C67_A4 && r <= TREG_C67_B13) // these form a pattern of alt pairs
  281. return (r & 2) >> 1;
  282. else if (r == C67_A0)
  283. return 0; // set to A side
  284. else if (r == C67_B2)
  285. return 1; // set to B side
  286. else if (r == C67_B3)
  287. return 1; // set to B side
  288. else if (r == C67_SP)
  289. return 0x1; // set to SP (B15) B side
  290. else if (r == C67_FP)
  291. return 0x0; // set to FP (A15) A side
  292. else
  293. ALWAYS_ASSERT(FALSE);
  294. return 0;
  295. }
  296. int C67_map_S12(char *s)
  297. {
  298. if (strstr(s, ".S1") != NULL)
  299. return 0;
  300. else if (strcmp(s, ".S2"))
  301. return 1;
  302. else
  303. ALWAYS_ASSERT(FALSE);
  304. return 0;
  305. }
  306. int C67_map_D12(char *s)
  307. {
  308. if (strstr(s, ".D1") != NULL)
  309. return 0;
  310. else if (strcmp(s, ".D2"))
  311. return 1;
  312. else
  313. ALWAYS_ASSERT(FALSE);
  314. return 0;
  315. }
  316. void C67_asm(char *s, int a, int b, int c)
  317. {
  318. BOOL xpath;
  319. #ifdef ASSEMBLY_LISTING_C67
  320. if (!f) {
  321. f = fopen("TCC67_out.txt", "wt");
  322. }
  323. fprintf(f, "%04X ", ind);
  324. #endif
  325. if (strstr(s, "MVKL") == s) {
  326. C67_g((C67_map_regn(b) << 23) |
  327. ((a & 0xffff) << 7) | (0x0a << 2) | (C67_map_regs(b) << 1));
  328. } else if (strstr(s, "MVKH") == s) {
  329. C67_g((C67_map_regn(b) << 23) |
  330. (((a >> 16) & 0xffff) << 7) |
  331. (0x1a << 2) | (C67_map_regs(b) << 1));
  332. } else if (strstr(s, "STW.D SP POST DEC") == s) {
  333. C67_g((C67_map_regn(a) << 23) | //src
  334. (15 << 18) | //SP B15
  335. (2 << 13) | //ucst5 (must keep 8 byte boundary !!)
  336. (0xa << 9) | //mode a = post dec ucst
  337. (0 << 8) | //r (LDDW bit 0)
  338. (1 << 7) | //y D1/D2 use B side
  339. (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  340. (1 << 2) | //opcode
  341. (C67_map_regs(a) << 1) | //side of src
  342. (0 << 0)); //parallel
  343. } else if (strstr(s, "STB.D *+SP[A0]") == s) {
  344. C67_g((C67_map_regn(a) << 23) | //src
  345. (15 << 18) | //base reg A15
  346. (0 << 13) | //offset reg A0
  347. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  348. (0 << 8) | //r (LDDW bit 0)
  349. (0 << 7) | //y D1/D2 A side
  350. (3 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  351. (1 << 2) | //opcode
  352. (C67_map_regs(a) << 1) | //side of src
  353. (0 << 0)); //parallel
  354. } else if (strstr(s, "STH.D *+SP[A0]") == s) {
  355. C67_g((C67_map_regn(a) << 23) | //src
  356. (15 << 18) | //base reg A15
  357. (0 << 13) | //offset reg A0
  358. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  359. (0 << 8) | //r (LDDW bit 0)
  360. (0 << 7) | //y D1/D2 A side
  361. (5 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  362. (1 << 2) | //opcode
  363. (C67_map_regs(a) << 1) | //side of src
  364. (0 << 0)); //parallel
  365. } else if (strstr(s, "STB.D *+SP[A0]") == s) {
  366. C67_g((C67_map_regn(a) << 23) | //src
  367. (15 << 18) | //base reg A15
  368. (0 << 13) | //offset reg A0
  369. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  370. (0 << 8) | //r (LDDW bit 0)
  371. (0 << 7) | //y D1/D2 A side
  372. (3 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  373. (1 << 2) | //opcode
  374. (C67_map_regs(a) << 1) | //side of src
  375. (0 << 0)); //parallel
  376. } else if (strstr(s, "STH.D *+SP[A0]") == s) {
  377. C67_g((C67_map_regn(a) << 23) | //src
  378. (15 << 18) | //base reg A15
  379. (0 << 13) | //offset reg A0
  380. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  381. (0 << 8) | //r (LDDW bit 0)
  382. (0 << 7) | //y D1/D2 A side
  383. (5 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  384. (1 << 2) | //opcode
  385. (C67_map_regs(a) << 1) | //side of src
  386. (0 << 0)); //parallel
  387. } else if (strstr(s, "STW.D *+SP[A0]") == s) {
  388. C67_g((C67_map_regn(a) << 23) | //src
  389. (15 << 18) | //base reg A15
  390. (0 << 13) | //offset reg A0
  391. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  392. (0 << 8) | //r (LDDW bit 0)
  393. (0 << 7) | //y D1/D2 A side
  394. (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  395. (1 << 2) | //opcode
  396. (C67_map_regs(a) << 1) | //side of src
  397. (0 << 0)); //parallel
  398. } else if (strstr(s, "STW.D *") == s) {
  399. C67_g((C67_map_regn(a) << 23) | //src
  400. (C67_map_regn(b) << 18) | //base reg A0
  401. (0 << 13) | //cst5
  402. (1 << 9) | //mode 1 = pos cst offset
  403. (0 << 8) | //r (LDDW bit 0)
  404. (C67_map_regs(b) << 7) | //y D1/D2 base reg side
  405. (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  406. (1 << 2) | //opcode
  407. (C67_map_regs(a) << 1) | //side of src
  408. (0 << 0)); //parallel
  409. } else if (strstr(s, "STH.D *") == s) {
  410. C67_g((C67_map_regn(a) << 23) | //src
  411. (C67_map_regn(b) << 18) | //base reg A0
  412. (0 << 13) | //cst5
  413. (1 << 9) | //mode 1 = pos cst offset
  414. (0 << 8) | //r (LDDW bit 0)
  415. (C67_map_regs(b) << 7) | //y D1/D2 base reg side
  416. (5 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  417. (1 << 2) | //opcode
  418. (C67_map_regs(a) << 1) | //side of src
  419. (0 << 0)); //parallel
  420. } else if (strstr(s, "STB.D *") == s) {
  421. C67_g((C67_map_regn(a) << 23) | //src
  422. (C67_map_regn(b) << 18) | //base reg A0
  423. (0 << 13) | //cst5
  424. (1 << 9) | //mode 1 = pos cst offset
  425. (0 << 8) | //r (LDDW bit 0)
  426. (C67_map_regs(b) << 7) | //y D1/D2 base reg side
  427. (3 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  428. (1 << 2) | //opcode
  429. (C67_map_regs(a) << 1) | //side of src
  430. (0 << 0)); //parallel
  431. } else if (strstr(s, "STW.D +*") == s) {
  432. ALWAYS_ASSERT(c < 32);
  433. C67_g((C67_map_regn(a) << 23) | //src
  434. (C67_map_regn(b) << 18) | //base reg A0
  435. (c << 13) | //cst5
  436. (1 << 9) | //mode 1 = pos cst offset
  437. (0 << 8) | //r (LDDW bit 0)
  438. (C67_map_regs(b) << 7) | //y D1/D2 base reg side
  439. (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  440. (1 << 2) | //opcode
  441. (C67_map_regs(a) << 1) | //side of src
  442. (0 << 0)); //parallel
  443. } else if (strstr(s, "LDW.D SP PRE INC") == s) {
  444. C67_g((C67_map_regn(a) << 23) | //dst
  445. (15 << 18) | //base reg B15
  446. (2 << 13) | //ucst5 (must keep 8 byte boundary)
  447. (9 << 9) | //mode 9 = pre inc ucst5
  448. (0 << 8) | //r (LDDW bit 0)
  449. (1 << 7) | //y D1/D2 B side
  450. (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  451. (1 << 2) | //opcode
  452. (C67_map_regs(a) << 1) | //side of dst
  453. (0 << 0)); //parallel
  454. } else if (strstr(s, "LDDW.D SP PRE INC") == s) {
  455. C67_g((C67_map_regn(a) << 23) | //dst
  456. (15 << 18) | //base reg B15
  457. (1 << 13) | //ucst5 (must keep 8 byte boundary)
  458. (9 << 9) | //mode 9 = pre inc ucst5
  459. (1 << 8) | //r (LDDW bit 1)
  460. (1 << 7) | //y D1/D2 B side
  461. (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  462. (1 << 2) | //opcode
  463. (C67_map_regs(a) << 1) | //side of dst
  464. (0 << 0)); //parallel
  465. } else if (strstr(s, "LDW.D *+SP[A0]") == s) {
  466. C67_g((C67_map_regn(a) << 23) | //dst
  467. (15 << 18) | //base reg A15
  468. (0 << 13) | //offset reg A0
  469. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  470. (0 << 8) | //r (LDDW bit 0)
  471. (0 << 7) | //y D1/D2 A side
  472. (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  473. (1 << 2) | //opcode
  474. (C67_map_regs(a) << 1) | //side of dst
  475. (0 << 0)); //parallel
  476. } else if (strstr(s, "LDDW.D *+SP[A0]") == s) {
  477. C67_g((C67_map_regn(a) << 23) | //dst
  478. (15 << 18) | //base reg A15
  479. (0 << 13) | //offset reg A0
  480. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  481. (1 << 8) | //r (LDDW bit 1)
  482. (0 << 7) | //y D1/D2 A side
  483. (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  484. (1 << 2) | //opcode
  485. (C67_map_regs(a) << 1) | //side of dst
  486. (0 << 0)); //parallel
  487. } else if (strstr(s, "LDH.D *+SP[A0]") == s) {
  488. C67_g((C67_map_regn(a) << 23) | //dst
  489. (15 << 18) | //base reg A15
  490. (0 << 13) | //offset reg A0
  491. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  492. (0 << 8) | //r (LDDW bit 0)
  493. (0 << 7) | //y D1/D2 A side
  494. (4 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  495. (1 << 2) | //opcode
  496. (C67_map_regs(a) << 1) | //side of dst
  497. (0 << 0)); //parallel
  498. } else if (strstr(s, "LDB.D *+SP[A0]") == s) {
  499. C67_g((C67_map_regn(a) << 23) | //dst
  500. (15 << 18) | //base reg A15
  501. (0 << 13) | //offset reg A0
  502. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  503. (0 << 8) | //r (LDDW bit 0)
  504. (0 << 7) | //y D1/D2 A side
  505. (2 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  506. (1 << 2) | //opcode
  507. (C67_map_regs(a) << 1) | //side of dst
  508. (0 << 0)); //parallel
  509. } else if (strstr(s, "LDHU.D *+SP[A0]") == s) {
  510. C67_g((C67_map_regn(a) << 23) | //dst
  511. (15 << 18) | //base reg A15
  512. (0 << 13) | //offset reg A0
  513. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  514. (0 << 8) | //r (LDDW bit 0)
  515. (0 << 7) | //y D1/D2 A side
  516. (0 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  517. (1 << 2) | //opcode
  518. (C67_map_regs(a) << 1) | //side of dst
  519. (0 << 0)); //parallel
  520. } else if (strstr(s, "LDBU.D *+SP[A0]") == s) {
  521. C67_g((C67_map_regn(a) << 23) | //dst
  522. (15 << 18) | //base reg A15
  523. (0 << 13) | //offset reg A0
  524. (5 << 9) | //mode 5 = pos offset, base reg + off reg
  525. (0 << 8) | //r (LDDW bit 0)
  526. (0 << 7) | //y D1/D2 A side
  527. (1 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  528. (1 << 2) | //opcode
  529. (C67_map_regs(a) << 1) | //side of dst
  530. (0 << 0)); //parallel
  531. } else if (strstr(s, "LDW.D *") == s) {
  532. C67_g((C67_map_regn(b) << 23) | //dst
  533. (C67_map_regn(a) << 18) | //base reg A15
  534. (0 << 13) | //cst5
  535. (1 << 9) | //mode 1 = pos cst offset
  536. (0 << 8) | //r (LDDW bit 0)
  537. (C67_map_regs(a) << 7) | //y D1/D2 src side
  538. (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  539. (1 << 2) | //opcode
  540. (C67_map_regs(b) << 1) | //side of dst
  541. (0 << 0)); //parallel
  542. } else if (strstr(s, "LDDW.D *") == s) {
  543. C67_g((C67_map_regn(b) << 23) | //dst
  544. (C67_map_regn(a) << 18) | //base reg A15
  545. (0 << 13) | //cst5
  546. (1 << 9) | //mode 1 = pos cst offset
  547. (1 << 8) | //r (LDDW bit 1)
  548. (C67_map_regs(a) << 7) | //y D1/D2 src side
  549. (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  550. (1 << 2) | //opcode
  551. (C67_map_regs(b) << 1) | //side of dst
  552. (0 << 0)); //parallel
  553. } else if (strstr(s, "LDH.D *") == s) {
  554. C67_g((C67_map_regn(b) << 23) | //dst
  555. (C67_map_regn(a) << 18) | //base reg A15
  556. (0 << 13) | //cst5
  557. (1 << 9) | //mode 1 = pos cst offset
  558. (0 << 8) | //r (LDDW bit 0)
  559. (C67_map_regs(a) << 7) | //y D1/D2 src side
  560. (4 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  561. (1 << 2) | //opcode
  562. (C67_map_regs(b) << 1) | //side of dst
  563. (0 << 0)); //parallel
  564. } else if (strstr(s, "LDB.D *") == s) {
  565. C67_g((C67_map_regn(b) << 23) | //dst
  566. (C67_map_regn(a) << 18) | //base reg A15
  567. (0 << 13) | //cst5
  568. (1 << 9) | //mode 1 = pos cst offset
  569. (0 << 8) | //r (LDDW bit 0)
  570. (C67_map_regs(a) << 7) | //y D1/D2 src side
  571. (2 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  572. (1 << 2) | //opcode
  573. (C67_map_regs(b) << 1) | //side of dst
  574. (0 << 0)); //parallel
  575. } else if (strstr(s, "LDHU.D *") == s) {
  576. C67_g((C67_map_regn(b) << 23) | //dst
  577. (C67_map_regn(a) << 18) | //base reg A15
  578. (0 << 13) | //cst5
  579. (1 << 9) | //mode 1 = pos cst offset
  580. (0 << 8) | //r (LDDW bit 0)
  581. (C67_map_regs(a) << 7) | //y D1/D2 src side
  582. (0 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  583. (1 << 2) | //opcode
  584. (C67_map_regs(b) << 1) | //side of dst
  585. (0 << 0)); //parallel
  586. } else if (strstr(s, "LDBU.D *") == s) {
  587. C67_g((C67_map_regn(b) << 23) | //dst
  588. (C67_map_regn(a) << 18) | //base reg A15
  589. (0 << 13) | //cst5
  590. (1 << 9) | //mode 1 = pos cst offset
  591. (0 << 8) | //r (LDDW bit 0)
  592. (C67_map_regs(a) << 7) | //y D1/D2 src side
  593. (1 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  594. (1 << 2) | //opcode
  595. (C67_map_regs(b) << 1) | //side of dst
  596. (0 << 0)); //parallel
  597. } else if (strstr(s, "LDW.D +*") == s) {
  598. C67_g((C67_map_regn(b) << 23) | //dst
  599. (C67_map_regn(a) << 18) | //base reg A15
  600. (1 << 13) | //cst5
  601. (1 << 9) | //mode 1 = pos cst offset
  602. (0 << 8) | //r (LDDW bit 0)
  603. (C67_map_regs(a) << 7) | //y D1/D2 src side
  604. (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU
  605. (1 << 2) | //opcode
  606. (C67_map_regs(b) << 1) | //side of dst
  607. (0 << 0)); //parallel
  608. } else if (strstr(s, "CMPLTSP") == s) {
  609. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  610. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  611. C67_g((C67_map_regn(c) << 23) | //dst
  612. (C67_map_regn(b) << 18) | //src2
  613. (C67_map_regn(a) << 13) | //src1
  614. (xpath << 12) | //x use cross path for src2
  615. (0x3a << 6) | //opcode
  616. (0x8 << 2) | //opcode fixed
  617. (C67_map_regs(c) << 1) | //side for reg c
  618. (0 << 0)); //parallel
  619. } else if (strstr(s, "CMPGTSP") == s) {
  620. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  621. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  622. C67_g((C67_map_regn(c) << 23) | //dst
  623. (C67_map_regn(b) << 18) | //src2
  624. (C67_map_regn(a) << 13) | //src1
  625. (xpath << 12) | //x use cross path for src2
  626. (0x39 << 6) | //opcode
  627. (0x8 << 2) | //opcode fixed
  628. (C67_map_regs(c) << 1) | //side for reg c
  629. (0 << 0)); //parallel
  630. } else if (strstr(s, "CMPEQSP") == s) {
  631. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  632. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  633. C67_g((C67_map_regn(c) << 23) | //dst
  634. (C67_map_regn(b) << 18) | //src2
  635. (C67_map_regn(a) << 13) | //src1
  636. (xpath << 12) | //x use cross path for src2
  637. (0x38 << 6) | //opcode
  638. (0x8 << 2) | //opcode fixed
  639. (C67_map_regs(c) << 1) | //side for reg c
  640. (0 << 0)); //parallel
  641. }
  642. else if (strstr(s, "CMPLTDP") == s) {
  643. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  644. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  645. C67_g((C67_map_regn(c) << 23) | //dst
  646. (C67_map_regn(b) << 18) | //src2
  647. (C67_map_regn(a) << 13) | //src1
  648. (xpath << 12) | //x use cross path for src2
  649. (0x2a << 6) | //opcode
  650. (0x8 << 2) | //opcode fixed
  651. (C67_map_regs(c) << 1) | //side for reg c
  652. (0 << 0)); //parallel
  653. } else if (strstr(s, "CMPGTDP") == s) {
  654. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  655. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  656. C67_g((C67_map_regn(c) << 23) | //dst
  657. (C67_map_regn(b) << 18) | //src2
  658. (C67_map_regn(a) << 13) | //src1
  659. (xpath << 12) | //x use cross path for src2
  660. (0x29 << 6) | //opcode
  661. (0x8 << 2) | //opcode fixed
  662. (C67_map_regs(c) << 1) | //side for reg c
  663. (0 << 0)); //parallel
  664. } else if (strstr(s, "CMPEQDP") == s) {
  665. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  666. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  667. C67_g((C67_map_regn(c) << 23) | //dst
  668. (C67_map_regn(b) << 18) | //src2
  669. (C67_map_regn(a) << 13) | //src1
  670. (xpath << 12) | //x use cross path for src2
  671. (0x28 << 6) | //opcode
  672. (0x8 << 2) | //opcode fixed
  673. (C67_map_regs(c) << 1) | //side for reg c
  674. (0 << 0)); //parallel
  675. } else if (strstr(s, "CMPLT") == s) {
  676. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  677. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  678. C67_g((C67_map_regn(c) << 23) | //dst
  679. (C67_map_regn(b) << 18) | //src2
  680. (C67_map_regn(a) << 13) | //src1
  681. (xpath << 12) | //x use cross path for src2
  682. (0x57 << 5) | //opcode
  683. (0x6 << 2) | //opcode fixed
  684. (C67_map_regs(c) << 1) | //side for reg c
  685. (0 << 0)); //parallel
  686. } else if (strstr(s, "CMPGT") == s) {
  687. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  688. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  689. C67_g((C67_map_regn(c) << 23) | //dst
  690. (C67_map_regn(b) << 18) | //src2
  691. (C67_map_regn(a) << 13) | //src1
  692. (xpath << 12) | //x use cross path for src2
  693. (0x47 << 5) | //opcode
  694. (0x6 << 2) | //opcode fixed
  695. (C67_map_regs(c) << 1) | //side for reg c
  696. (0 << 0)); //parallel
  697. } else if (strstr(s, "CMPEQ") == s) {
  698. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  699. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  700. C67_g((C67_map_regn(c) << 23) | //dst
  701. (C67_map_regn(b) << 18) | //src2
  702. (C67_map_regn(a) << 13) | //src1
  703. (xpath << 12) | //x use cross path for src2
  704. (0x53 << 5) | //opcode
  705. (0x6 << 2) | //opcode fixed
  706. (C67_map_regs(c) << 1) | //side for reg c
  707. (0 << 0)); //parallel
  708. } else if (strstr(s, "CMPLTU") == s) {
  709. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  710. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  711. C67_g((C67_map_regn(c) << 23) | //dst
  712. (C67_map_regn(b) << 18) | //src2
  713. (C67_map_regn(a) << 13) | //src1
  714. (xpath << 12) | //x use cross path for src2
  715. (0x5f << 5) | //opcode
  716. (0x6 << 2) | //opcode fixed
  717. (C67_map_regs(c) << 1) | //side for reg c
  718. (0 << 0)); //parallel
  719. } else if (strstr(s, "CMPGTU") == s) {
  720. xpath = C67_map_regs(a) ^ C67_map_regs(b);
  721. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  722. C67_g((C67_map_regn(c) << 23) | //dst
  723. (C67_map_regn(b) << 18) | //src2
  724. (C67_map_regn(a) << 13) | //src1
  725. (xpath << 12) | //x use cross path for src2
  726. (0x4f << 5) | //opcode
  727. (0x6 << 2) | //opcode fixed
  728. (C67_map_regs(c) << 1) | //side for reg c
  729. (0 << 0)); //parallel
  730. } else if (strstr(s, "B DISP") == s) {
  731. C67_g((0 << 29) | //creg
  732. (0 << 28) | //z
  733. (a << 7) | //cnst
  734. (0x4 << 2) | //opcode fixed
  735. (0 << 1) | //S0/S1
  736. (0 << 0)); //parallel
  737. } else if (strstr(s, "B.") == s) {
  738. xpath = C67_map_regs(c) ^ 1;
  739. C67_g((C67_map_regc(b) << 29) | //creg
  740. (a << 28) | //inv
  741. (0 << 23) | //dst
  742. (C67_map_regn(c) << 18) | //src2
  743. (0 << 13) | //
  744. (xpath << 12) | //x cross path if !B side
  745. (0xd << 6) | //opcode
  746. (0x8 << 2) | //opcode fixed
  747. (1 << 1) | //must be S2
  748. (0 << 0)); //parallel
  749. } else if (strstr(s, "MV.L") == s) {
  750. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  751. C67_g((0 << 29) | //creg
  752. (0 << 28) | //inv
  753. (C67_map_regn(c) << 23) | //dst
  754. (C67_map_regn(b) << 18) | //src2
  755. (0 << 13) | //src1 (cst5)
  756. (xpath << 12) | //x cross path if opposite sides
  757. (0x2 << 5) | //opcode
  758. (0x6 << 2) | //opcode fixed
  759. (C67_map_regs(c) << 1) | //side of dest
  760. (0 << 0)); //parallel
  761. } else if (strstr(s, "SPTRUNC.L") == s) {
  762. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  763. C67_g((0 << 29) | //creg
  764. (0 << 28) | //inv
  765. (C67_map_regn(c) << 23) | //dst
  766. (C67_map_regn(b) << 18) | //src2
  767. (0 << 13) | //src1 NA
  768. (xpath << 12) | //x cross path if opposite sides
  769. (0xb << 5) | //opcode
  770. (0x6 << 2) | //opcode fixed
  771. (C67_map_regs(c) << 1) | //side of dest
  772. (0 << 0)); //parallel
  773. } else if (strstr(s, "DPTRUNC.L") == s) {
  774. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  775. C67_g((0 << 29) | //creg
  776. (0 << 28) | //inv
  777. (C67_map_regn(c) << 23) | //dst
  778. ((C67_map_regn(b) + 1) << 18) | //src2 WEIRD CPU must specify odd reg for some reason
  779. (0 << 13) | //src1 NA
  780. (xpath << 12) | //x cross path if opposite sides
  781. (0x1 << 5) | //opcode
  782. (0x6 << 2) | //opcode fixed
  783. (C67_map_regs(c) << 1) | //side of dest
  784. (0 << 0)); //parallel
  785. } else if (strstr(s, "INTSP.L") == s) {
  786. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  787. C67_g((0 << 29) | //creg
  788. (0 << 28) | //inv
  789. (C67_map_regn(c) << 23) | //dst
  790. (C67_map_regn(b) << 18) | //src2
  791. (0 << 13) | //src1 NA
  792. (xpath << 12) | //x cross path if opposite sides
  793. (0x4a << 5) | //opcode
  794. (0x6 << 2) | //opcode fixed
  795. (C67_map_regs(c) << 1) | //side of dest
  796. (0 << 0)); //parallel
  797. } else if (strstr(s, "INTSPU.L") == s) {
  798. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  799. C67_g((0 << 29) | //creg
  800. (0 << 28) | //inv
  801. (C67_map_regn(c) << 23) | //dst
  802. (C67_map_regn(b) << 18) | //src2
  803. (0 << 13) | //src1 NA
  804. (xpath << 12) | //x cross path if opposite sides
  805. (0x49 << 5) | //opcode
  806. (0x6 << 2) | //opcode fixed
  807. (C67_map_regs(c) << 1) | //side of dest
  808. (0 << 0)); //parallel
  809. } else if (strstr(s, "INTDP.L") == s) {
  810. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  811. C67_g((0 << 29) | //creg
  812. (0 << 28) | //inv
  813. (C67_map_regn(c) << 23) | //dst
  814. (C67_map_regn(b) << 18) | //src2
  815. (0 << 13) | //src1 NA
  816. (xpath << 12) | //x cross path if opposite sides
  817. (0x39 << 5) | //opcode
  818. (0x6 << 2) | //opcode fixed
  819. (C67_map_regs(c) << 1) | //side of dest
  820. (0 << 0)); //parallel
  821. } else if (strstr(s, "INTDPU.L") == s) {
  822. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  823. C67_g((0 << 29) | //creg
  824. (0 << 28) | //inv
  825. (C67_map_regn(c) << 23) | //dst
  826. ((C67_map_regn(b) + 1) << 18) | //src2 WEIRD CPU must specify odd reg for some reason
  827. (0 << 13) | //src1 NA
  828. (xpath << 12) | //x cross path if opposite sides
  829. (0x3b << 5) | //opcode
  830. (0x6 << 2) | //opcode fixed
  831. (C67_map_regs(c) << 1) | //side of dest
  832. (0 << 0)); //parallel
  833. } else if (strstr(s, "SPDP.L") == s) {
  834. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  835. C67_g((0 << 29) | //creg
  836. (0 << 28) | //inv
  837. (C67_map_regn(c) << 23) | //dst
  838. (C67_map_regn(b) << 18) | //src2
  839. (0 << 13) | //src1 NA
  840. (xpath << 12) | //x cross path if opposite sides
  841. (0x2 << 6) | //opcode
  842. (0x8 << 2) | //opcode fixed
  843. (C67_map_regs(c) << 1) | //side of dest
  844. (0 << 0)); //parallel
  845. } else if (strstr(s, "DPSP.L") == s) {
  846. ALWAYS_ASSERT(C67_map_regs(b) == C67_map_regs(c));
  847. C67_g((0 << 29) | //creg
  848. (0 << 28) | //inv
  849. (C67_map_regn(c) << 23) | //dst
  850. ((C67_map_regn(b) + 1) << 18) | //src2 WEIRD CPU must specify odd reg for some reason
  851. (0 << 13) | //src1 NA
  852. (0 << 12) | //x cross path if opposite sides
  853. (0x9 << 5) | //opcode
  854. (0x6 << 2) | //opcode fixed
  855. (C67_map_regs(c) << 1) | //side of dest
  856. (0 << 0)); //parallel
  857. } else if (strstr(s, "ADD.L") == s) {
  858. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  859. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  860. C67_g((0 << 29) | //creg
  861. (0 << 28) | //inv
  862. (C67_map_regn(c) << 23) | //dst
  863. (C67_map_regn(b) << 18) | //src2 (possible x path)
  864. (C67_map_regn(a) << 13) | //src1
  865. (xpath << 12) | //x cross path if opposite sides
  866. (0x3 << 5) | //opcode
  867. (0x6 << 2) | //opcode fixed
  868. (C67_map_regs(c) << 1) | //side of dest
  869. (0 << 0)); //parallel
  870. } else if (strstr(s, "SUB.L") == s) {
  871. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  872. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  873. C67_g((0 << 29) | //creg
  874. (0 << 28) | //inv
  875. (C67_map_regn(c) << 23) | //dst
  876. (C67_map_regn(b) << 18) | //src2 (possible x path)
  877. (C67_map_regn(a) << 13) | //src1
  878. (xpath << 12) | //x cross path if opposite sides
  879. (0x7 << 5) | //opcode
  880. (0x6 << 2) | //opcode fixed
  881. (C67_map_regs(c) << 1) | //side of dest
  882. (0 << 0)); //parallel
  883. } else if (strstr(s, "OR.L") == s) {
  884. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  885. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  886. C67_g((0 << 29) | //creg
  887. (0 << 28) | //inv
  888. (C67_map_regn(c) << 23) | //dst
  889. (C67_map_regn(b) << 18) | //src2 (possible x path)
  890. (C67_map_regn(a) << 13) | //src1
  891. (xpath << 12) | //x cross path if opposite sides
  892. (0x7f << 5) | //opcode
  893. (0x6 << 2) | //opcode fixed
  894. (C67_map_regs(c) << 1) | //side of dest
  895. (0 << 0)); //parallel
  896. } else if (strstr(s, "AND.L") == s) {
  897. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  898. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  899. C67_g((0 << 29) | //creg
  900. (0 << 28) | //inv
  901. (C67_map_regn(c) << 23) | //dst
  902. (C67_map_regn(b) << 18) | //src2 (possible x path)
  903. (C67_map_regn(a) << 13) | //src1
  904. (xpath << 12) | //x cross path if opposite sides
  905. (0x7b << 5) | //opcode
  906. (0x6 << 2) | //opcode fixed
  907. (C67_map_regs(c) << 1) | //side of dest
  908. (0 << 0)); //parallel
  909. } else if (strstr(s, "XOR.L") == s) {
  910. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  911. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  912. C67_g((0 << 29) | //creg
  913. (0 << 28) | //inv
  914. (C67_map_regn(c) << 23) | //dst
  915. (C67_map_regn(b) << 18) | //src2 (possible x path)
  916. (C67_map_regn(a) << 13) | //src1
  917. (xpath << 12) | //x cross path if opposite sides
  918. (0x6f << 5) | //opcode
  919. (0x6 << 2) | //opcode fixed
  920. (C67_map_regs(c) << 1) | //side of dest
  921. (0 << 0)); //parallel
  922. } else if (strstr(s, "ADDSP.L") == s) {
  923. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  924. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  925. C67_g((0 << 29) | //creg
  926. (0 << 28) | //inv
  927. (C67_map_regn(c) << 23) | //dst
  928. (C67_map_regn(b) << 18) | //src2 (possible x path)
  929. (C67_map_regn(a) << 13) | //src1
  930. (xpath << 12) | //x cross path if opposite sides
  931. (0x10 << 5) | //opcode
  932. (0x6 << 2) | //opcode fixed
  933. (C67_map_regs(c) << 1) | //side of dest
  934. (0 << 0)); //parallel
  935. } else if (strstr(s, "ADDDP.L") == s) {
  936. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  937. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  938. C67_g((0 << 29) | //creg
  939. (0 << 28) | //inv
  940. (C67_map_regn(c) << 23) | //dst
  941. (C67_map_regn(b) << 18) | //src2 (possible x path)
  942. (C67_map_regn(a) << 13) | //src1
  943. (xpath << 12) | //x cross path if opposite sides
  944. (0x18 << 5) | //opcode
  945. (0x6 << 2) | //opcode fixed
  946. (C67_map_regs(c) << 1) | //side of dest
  947. (0 << 0)); //parallel
  948. } else if (strstr(s, "SUBSP.L") == s) {
  949. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  950. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  951. C67_g((0 << 29) | //creg
  952. (0 << 28) | //inv
  953. (C67_map_regn(c) << 23) | //dst
  954. (C67_map_regn(b) << 18) | //src2 (possible x path)
  955. (C67_map_regn(a) << 13) | //src1
  956. (xpath << 12) | //x cross path if opposite sides
  957. (0x11 << 5) | //opcode
  958. (0x6 << 2) | //opcode fixed
  959. (C67_map_regs(c) << 1) | //side of dest
  960. (0 << 0)); //parallel
  961. } else if (strstr(s, "SUBDP.L") == s) {
  962. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  963. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  964. C67_g((0 << 29) | //creg
  965. (0 << 28) | //inv
  966. (C67_map_regn(c) << 23) | //dst
  967. (C67_map_regn(b) << 18) | //src2 (possible x path)
  968. (C67_map_regn(a) << 13) | //src1
  969. (xpath << 12) | //x cross path if opposite sides
  970. (0x19 << 5) | //opcode
  971. (0x6 << 2) | //opcode fixed
  972. (C67_map_regs(c) << 1) | //side of dest
  973. (0 << 0)); //parallel
  974. } else if (strstr(s, "MPYSP.M") == s) {
  975. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  976. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  977. C67_g((0 << 29) | //creg
  978. (0 << 28) | //inv
  979. (C67_map_regn(c) << 23) | //dst
  980. (C67_map_regn(b) << 18) | //src2 (possible x path)
  981. (C67_map_regn(a) << 13) | //src1
  982. (xpath << 12) | //x cross path if opposite sides
  983. (0x1c << 7) | //opcode
  984. (0x0 << 2) | //opcode fixed
  985. (C67_map_regs(c) << 1) | //side of dest
  986. (0 << 0)); //parallel
  987. } else if (strstr(s, "MPYDP.M") == s) {
  988. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  989. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  990. C67_g((0 << 29) | //creg
  991. (0 << 28) | //inv
  992. (C67_map_regn(c) << 23) | //dst
  993. (C67_map_regn(b) << 18) | //src2 (possible x path)
  994. (C67_map_regn(a) << 13) | //src1
  995. (xpath << 12) | //x cross path if opposite sides
  996. (0x0e << 7) | //opcode
  997. (0x0 << 2) | //opcode fixed
  998. (C67_map_regs(c) << 1) | //side of dest
  999. (0 << 0)); //parallel
  1000. } else if (strstr(s, "MPYI.M") == s) {
  1001. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  1002. ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c));
  1003. C67_g((0 << 29) | //creg
  1004. (0 << 28) | //inv
  1005. (C67_map_regn(c) << 23) | //dst
  1006. (C67_map_regn(b) << 18) | //src2
  1007. (C67_map_regn(a) << 13) | //src1 (cst5)
  1008. (xpath << 12) | //x cross path if opposite sides
  1009. (0x4 << 7) | //opcode
  1010. (0x0 << 2) | //opcode fixed
  1011. (C67_map_regs(c) << 1) | //side of dest
  1012. (0 << 0)); //parallel
  1013. } else if (strstr(s, "SHR.S") == s) {
  1014. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  1015. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  1016. C67_g((0 << 29) | //creg
  1017. (0 << 28) | //inv
  1018. (C67_map_regn(c) << 23) | //dst
  1019. (C67_map_regn(b) << 18) | //src2
  1020. (C67_map_regn(a) << 13) | //src1
  1021. (xpath << 12) | //x cross path if opposite sides
  1022. (0x37 << 6) | //opcode
  1023. (0x8 << 2) | //opcode fixed
  1024. (C67_map_regs(c) << 1) | //side of dest
  1025. (0 << 0)); //parallel
  1026. } else if (strstr(s, "SHRU.S") == s) {
  1027. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  1028. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  1029. C67_g((0 << 29) | //creg
  1030. (0 << 28) | //inv
  1031. (C67_map_regn(c) << 23) | //dst
  1032. (C67_map_regn(b) << 18) | //src2
  1033. (C67_map_regn(a) << 13) | //src1
  1034. (xpath << 12) | //x cross path if opposite sides
  1035. (0x27 << 6) | //opcode
  1036. (0x8 << 2) | //opcode fixed
  1037. (C67_map_regs(c) << 1) | //side of dest
  1038. (0 << 0)); //parallel
  1039. } else if (strstr(s, "SHL.S") == s) {
  1040. xpath = C67_map_regs(b) ^ C67_map_regs(c);
  1041. ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a));
  1042. C67_g((0 << 29) | //creg
  1043. (0 << 28) | //inv
  1044. (C67_map_regn(c) << 23) | //dst
  1045. (C67_map_regn(b) << 18) | //src2
  1046. (C67_map_regn(a) << 13) | //src1
  1047. (xpath << 12) | //x cross path if opposite sides
  1048. (0x33 << 6) | //opcode
  1049. (0x8 << 2) | //opcode fixed
  1050. (C67_map_regs(c) << 1) | //side of dest
  1051. (0 << 0)); //parallel
  1052. } else if (strstr(s, "||ADDK") == s) {
  1053. xpath = 0; // no xpath required just use the side of the src/dst
  1054. C67_g((0 << 29) | //creg
  1055. (0 << 28) | //inv
  1056. (C67_map_regn(b) << 23) | //dst
  1057. (a << 07) | //scst16
  1058. (0x14 << 2) | //opcode fixed
  1059. (C67_map_regs(b) << 1) | //side of dst
  1060. (1 << 0)); //parallel
  1061. } else if (strstr(s, "ADDK") == s) {
  1062. xpath = 0; // no xpath required just use the side of the src/dst
  1063. C67_g((0 << 29) | //creg
  1064. (0 << 28) | //inv
  1065. (C67_map_regn(b) << 23) | //dst
  1066. (a << 07) | //scst16
  1067. (0x14 << 2) | //opcode fixed
  1068. (C67_map_regs(b) << 1) | //side of dst
  1069. (0 << 0)); //parallel
  1070. } else if (strstr(s, "NOP") == s) {
  1071. C67_g(((a - 1) << 13) | //no of cycles
  1072. (0 << 0)); //parallel
  1073. } else
  1074. ALWAYS_ASSERT(FALSE);
  1075. #ifdef ASSEMBLY_LISTING_C67
  1076. fprintf(f, " %s %d %d %d\n", s, a, b, c);
  1077. #endif
  1078. }
  1079. //r=reg to load, fr=from reg, symbol for relocation, constant
  1080. void C67_MVKL(int r, int fc)
  1081. {
  1082. C67_asm("MVKL.", fc, r, 0);
  1083. }
  1084. void C67_MVKH(int r, int fc)
  1085. {
  1086. C67_asm("MVKH.", fc, r, 0);
  1087. }
  1088. void C67_STB_SP_A0(int r)
  1089. {
  1090. C67_asm("STB.D *+SP[A0]", r, 0, 0); // STB r,*+SP[A0]
  1091. }
  1092. void C67_STH_SP_A0(int r)
  1093. {
  1094. C67_asm("STH.D *+SP[A0]", r, 0, 0); // STH r,*+SP[A0]
  1095. }
  1096. void C67_STW_SP_A0(int r)
  1097. {
  1098. C67_asm("STW.D *+SP[A0]", r, 0, 0); // STW r,*+SP[A0]
  1099. }
  1100. void C67_STB_PTR(int r, int r2)
  1101. {
  1102. C67_asm("STB.D *", r, r2, 0); // STB r, *r2
  1103. }
  1104. void C67_STH_PTR(int r, int r2)
  1105. {
  1106. C67_asm("STH.D *", r, r2, 0); // STH r, *r2
  1107. }
  1108. void C67_STW_PTR(int r, int r2)
  1109. {
  1110. C67_asm("STW.D *", r, r2, 0); // STW r, *r2
  1111. }
  1112. void C67_STW_PTR_PRE_INC(int r, int r2, int n)
  1113. {
  1114. C67_asm("STW.D +*", r, r2, n); // STW r, *+r2
  1115. }
  1116. void C67_PUSH(int r)
  1117. {
  1118. C67_asm("STW.D SP POST DEC", r, 0, 0); // STW r,*SP--
  1119. }
  1120. void C67_LDW_SP_A0(int r)
  1121. {
  1122. C67_asm("LDW.D *+SP[A0]", r, 0, 0); // LDW *+SP[A0],r
  1123. }
  1124. void C67_LDDW_SP_A0(int r)
  1125. {
  1126. C67_asm("LDDW.D *+SP[A0]", r, 0, 0); // LDDW *+SP[A0],r
  1127. }
  1128. void C67_LDH_SP_A0(int r)
  1129. {
  1130. C67_asm("LDH.D *+SP[A0]", r, 0, 0); // LDH *+SP[A0],r
  1131. }
  1132. void C67_LDB_SP_A0(int r)
  1133. {
  1134. C67_asm("LDB.D *+SP[A0]", r, 0, 0); // LDB *+SP[A0],r
  1135. }
  1136. void C67_LDHU_SP_A0(int r)
  1137. {
  1138. C67_asm("LDHU.D *+SP[A0]", r, 0, 0); // LDHU *+SP[A0],r
  1139. }
  1140. void C67_LDBU_SP_A0(int r)
  1141. {
  1142. C67_asm("LDBU.D *+SP[A0]", r, 0, 0); // LDBU *+SP[A0],r
  1143. }
  1144. void C67_LDW_PTR(int r, int r2)
  1145. {
  1146. C67_asm("LDW.D *", r, r2, 0); // LDW *r,r2
  1147. }
  1148. void C67_LDDW_PTR(int r, int r2)
  1149. {
  1150. C67_asm("LDDW.D *", r, r2, 0); // LDDW *r,r2
  1151. }
  1152. void C67_LDH_PTR(int r, int r2)
  1153. {
  1154. C67_asm("LDH.D *", r, r2, 0); // LDH *r,r2
  1155. }
  1156. void C67_LDB_PTR(int r, int r2)
  1157. {
  1158. C67_asm("LDB.D *", r, r2, 0); // LDB *r,r2
  1159. }
  1160. void C67_LDHU_PTR(int r, int r2)
  1161. {
  1162. C67_asm("LDHU.D *", r, r2, 0); // LDHU *r,r2
  1163. }
  1164. void C67_LDBU_PTR(int r, int r2)
  1165. {
  1166. C67_asm("LDBU.D *", r, r2, 0); // LDBU *r,r2
  1167. }
  1168. void C67_LDW_PTR_PRE_INC(int r, int r2)
  1169. {
  1170. C67_asm("LDW.D +*", r, r2, 0); // LDW *+r,r2
  1171. }
  1172. void C67_POP(int r)
  1173. {
  1174. C67_asm("LDW.D SP PRE INC", r, 0, 0); // LDW *++SP,r
  1175. }
  1176. void C67_POP_DW(int r)
  1177. {
  1178. C67_asm("LDDW.D SP PRE INC", r, 0, 0); // LDDW *++SP,r
  1179. }
  1180. void C67_CMPLT(int s1, int s2, int dst)
  1181. {
  1182. C67_asm("CMPLT.L1", s1, s2, dst);
  1183. }
  1184. void C67_CMPGT(int s1, int s2, int dst)
  1185. {
  1186. C67_asm("CMPGT.L1", s1, s2, dst);
  1187. }
  1188. void C67_CMPEQ(int s1, int s2, int dst)
  1189. {
  1190. C67_asm("CMPEQ.L1", s1, s2, dst);
  1191. }
  1192. void C67_CMPLTU(int s1, int s2, int dst)
  1193. {
  1194. C67_asm("CMPLTU.L1", s1, s2, dst);
  1195. }
  1196. void C67_CMPGTU(int s1, int s2, int dst)
  1197. {
  1198. C67_asm("CMPGTU.L1", s1, s2, dst);
  1199. }
  1200. void C67_CMPLTSP(int s1, int s2, int dst)
  1201. {
  1202. C67_asm("CMPLTSP.S1", s1, s2, dst);
  1203. }
  1204. void C67_CMPGTSP(int s1, int s2, int dst)
  1205. {
  1206. C67_asm("CMPGTSP.S1", s1, s2, dst);
  1207. }
  1208. void C67_CMPEQSP(int s1, int s2, int dst)
  1209. {
  1210. C67_asm("CMPEQSP.S1", s1, s2, dst);
  1211. }
  1212. void C67_CMPLTDP(int s1, int s2, int dst)
  1213. {
  1214. C67_asm("CMPLTDP.S1", s1, s2, dst);
  1215. }
  1216. void C67_CMPGTDP(int s1, int s2, int dst)
  1217. {
  1218. C67_asm("CMPGTDP.S1", s1, s2, dst);
  1219. }
  1220. void C67_CMPEQDP(int s1, int s2, int dst)
  1221. {
  1222. C67_asm("CMPEQDP.S1", s1, s2, dst);
  1223. }
  1224. void C67_IREG_B_REG(int inv, int r1, int r2) // [!R] B r2
  1225. {
  1226. C67_asm("B.S2", inv, r1, r2);
  1227. }
  1228. // call with how many 32 bit words to skip
  1229. // (0 would branch to the branch instruction)
  1230. void C67_B_DISP(int disp) // B +2 Branch with constant displacement
  1231. {
  1232. // Branch point is relative to the 8 word fetch packet
  1233. //
  1234. // we will assume the text section always starts on an 8 word (32 byte boundary)
  1235. //
  1236. // so add in how many words into the fetch packet the branch is
  1237. C67_asm("B DISP", disp + ((ind & 31) >> 2), 0, 0);
  1238. }
  1239. void C67_NOP(int n)
  1240. {
  1241. C67_asm("NOP", n, 0, 0);
  1242. }
  1243. void C67_ADDK(int n, int r)
  1244. {
  1245. ALWAYS_ASSERT(abs(n) < 32767);
  1246. C67_asm("ADDK", n, r, 0);
  1247. }
  1248. void C67_ADDK_PARALLEL(int n, int r)
  1249. {
  1250. ALWAYS_ASSERT(abs(n) < 32767);
  1251. C67_asm("||ADDK", n, r, 0);
  1252. }
  1253. void C67_Adjust_ADDK(int *inst, int n)
  1254. {
  1255. ALWAYS_ASSERT(abs(n) < 32767);
  1256. *inst = (*inst & (~(0xffff << 7))) | ((n & 0xffff) << 7);
  1257. }
  1258. void C67_MV(int r, int v)
  1259. {
  1260. C67_asm("MV.L", 0, r, v);
  1261. }
  1262. void C67_DPTRUNC(int r, int v)
  1263. {
  1264. C67_asm("DPTRUNC.L", 0, r, v);
  1265. }
  1266. void C67_SPTRUNC(int r, int v)
  1267. {
  1268. C67_asm("SPTRUNC.L", 0, r, v);
  1269. }
  1270. void C67_INTSP(int r, int v)
  1271. {
  1272. C67_asm("INTSP.L", 0, r, v);
  1273. }
  1274. void C67_INTDP(int r, int v)
  1275. {
  1276. C67_asm("INTDP.L", 0, r, v);
  1277. }
  1278. void C67_INTSPU(int r, int v)
  1279. {
  1280. C67_asm("INTSPU.L", 0, r, v);
  1281. }
  1282. void C67_INTDPU(int r, int v)
  1283. {
  1284. C67_asm("INTDPU.L", 0, r, v);
  1285. }
  1286. void C67_SPDP(int r, int v)
  1287. {
  1288. C67_asm("SPDP.L", 0, r, v);
  1289. }
  1290. void C67_DPSP(int r, int v) // note regs must be on the same side
  1291. {
  1292. C67_asm("DPSP.L", 0, r, v);
  1293. }
  1294. void C67_ADD(int r, int v)
  1295. {
  1296. C67_asm("ADD.L", v, r, v);
  1297. }
  1298. void C67_SUB(int r, int v)
  1299. {
  1300. C67_asm("SUB.L", v, r, v);
  1301. }
  1302. void C67_AND(int r, int v)
  1303. {
  1304. C67_asm("AND.L", v, r, v);
  1305. }
  1306. void C67_OR(int r, int v)
  1307. {
  1308. C67_asm("OR.L", v, r, v);
  1309. }
  1310. void C67_XOR(int r, int v)
  1311. {
  1312. C67_asm("XOR.L", v, r, v);
  1313. }
  1314. void C67_ADDSP(int r, int v)
  1315. {
  1316. C67_asm("ADDSP.L", v, r, v);
  1317. }
  1318. void C67_SUBSP(int r, int v)
  1319. {
  1320. C67_asm("SUBSP.L", v, r, v);
  1321. }
  1322. void C67_MPYSP(int r, int v)
  1323. {
  1324. C67_asm("MPYSP.M", v, r, v);
  1325. }
  1326. void C67_ADDDP(int r, int v)
  1327. {
  1328. C67_asm("ADDDP.L", v, r, v);
  1329. }
  1330. void C67_SUBDP(int r, int v)
  1331. {
  1332. C67_asm("SUBDP.L", v, r, v);
  1333. }
  1334. void C67_MPYDP(int r, int v)
  1335. {
  1336. C67_asm("MPYDP.M", v, r, v);
  1337. }
  1338. void C67_MPYI(int r, int v)
  1339. {
  1340. C67_asm("MPYI.M", v, r, v);
  1341. }
  1342. void C67_SHL(int r, int v)
  1343. {
  1344. C67_asm("SHL.S", r, v, v);
  1345. }
  1346. void C67_SHRU(int r, int v)
  1347. {
  1348. C67_asm("SHRU.S", r, v, v);
  1349. }
  1350. void C67_SHR(int r, int v)
  1351. {
  1352. C67_asm("SHR.S", r, v, v);
  1353. }
  1354. /* load 'r' from value 'sv' */
  1355. void load(int r, SValue * sv)
  1356. {
  1357. int v, t, ft, fc, fr, size = 0, element;
  1358. BOOL Unsigned = false;
  1359. SValue v1;
  1360. fr = sv->r;
  1361. ft = sv->type.t;
  1362. fc = sv->c.ul;
  1363. v = fr & VT_VALMASK;
  1364. if (fr & VT_LVAL) {
  1365. if (v == VT_LLOCAL) {
  1366. v1.type.t = VT_INT;
  1367. v1.r = VT_LOCAL | VT_LVAL;
  1368. v1.c.ul = fc;
  1369. load(r, &v1);
  1370. fr = r;
  1371. } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
  1372. error("long double not supported");
  1373. } else if ((ft & VT_TYPE) == VT_BYTE) {
  1374. size = 1;
  1375. } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
  1376. size = 1;
  1377. Unsigned = TRUE;
  1378. } else if ((ft & VT_TYPE) == VT_SHORT) {
  1379. size = 2;
  1380. } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
  1381. size = 2;
  1382. Unsigned = TRUE;
  1383. } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
  1384. size = 8;
  1385. } else {
  1386. size = 4;
  1387. }
  1388. // check if fc is a positive reference on the stack,
  1389. // if it is tcc is referencing what it thinks is a parameter
  1390. // on the stack, so check if it is really in a register.
  1391. if (v == VT_LOCAL && fc > 0) {
  1392. int stack_pos = 8;
  1393. for (t = 0; t < NoCallArgsPassedOnStack; t++) {
  1394. if (fc == stack_pos)
  1395. break;
  1396. stack_pos += TranslateStackToReg[t];
  1397. }
  1398. // param has been pushed on stack, get it like a local var
  1399. fc = ParamLocOnStack[t] - 8;
  1400. }
  1401. if ((fr & VT_VALMASK) < VT_CONST) // check for pure indirect
  1402. {
  1403. if (size == 1) {
  1404. if (Unsigned)
  1405. C67_LDBU_PTR(v, r); // LDBU *v,r
  1406. else
  1407. C67_LDB_PTR(v, r); // LDB *v,r
  1408. } else if (size == 2) {
  1409. if (Unsigned)
  1410. C67_LDHU_PTR(v, r); // LDHU *v,r
  1411. else
  1412. C67_LDH_PTR(v, r); // LDH *v,r
  1413. } else if (size == 4) {
  1414. C67_LDW_PTR(v, r); // LDW *v,r
  1415. } else if (size == 8) {
  1416. C67_LDDW_PTR(v, r); // LDDW *v,r
  1417. }
  1418. C67_NOP(4); // NOP 4
  1419. return;
  1420. } else if (fr & VT_SYM) {
  1421. greloc(cur_text_section, sv->sym, ind, R_C60LO16); // rem the inst need to be patched
  1422. greloc(cur_text_section, sv->sym, ind + 4, R_C60HI16);
  1423. C67_MVKL(C67_A0, fc); //r=reg to load, constant
  1424. C67_MVKH(C67_A0, fc); //r=reg to load, constant
  1425. if (size == 1) {
  1426. if (Unsigned)
  1427. C67_LDBU_PTR(C67_A0, r); // LDBU *A0,r
  1428. else
  1429. C67_LDB_PTR(C67_A0, r); // LDB *A0,r
  1430. } else if (size == 2) {
  1431. if (Unsigned)
  1432. C67_LDHU_PTR(C67_A0, r); // LDHU *A0,r
  1433. else
  1434. C67_LDH_PTR(C67_A0, r); // LDH *A0,r
  1435. } else if (size == 4) {
  1436. C67_LDW_PTR(C67_A0, r); // LDW *A0,r
  1437. } else if (size == 8) {
  1438. C67_LDDW_PTR(C67_A0, r); // LDDW *A0,r
  1439. }
  1440. C67_NOP(4); // NOP 4
  1441. return;
  1442. } else {
  1443. element = size;
  1444. // divide offset in bytes to create element index
  1445. C67_MVKL(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant
  1446. C67_MVKH(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant
  1447. if (size == 1) {
  1448. if (Unsigned)
  1449. C67_LDBU_SP_A0(r); // LDBU r, SP[A0]
  1450. else
  1451. C67_LDB_SP_A0(r); // LDB r, SP[A0]
  1452. } else if (size == 2) {
  1453. if (Unsigned)
  1454. C67_LDHU_SP_A0(r); // LDHU r, SP[A0]
  1455. else
  1456. C67_LDH_SP_A0(r); // LDH r, SP[A0]
  1457. } else if (size == 4) {
  1458. C67_LDW_SP_A0(r); // LDW r, SP[A0]
  1459. } else if (size == 8) {
  1460. C67_LDDW_SP_A0(r); // LDDW r, SP[A0]
  1461. }
  1462. C67_NOP(4); // NOP 4
  1463. return;
  1464. }
  1465. } else {
  1466. if (v == VT_CONST) {
  1467. if (fr & VT_SYM) {
  1468. greloc(cur_text_section, sv->sym, ind, R_C60LO16); // rem the inst need to be patched
  1469. greloc(cur_text_section, sv->sym, ind + 4, R_C60HI16);
  1470. }
  1471. C67_MVKL(r, fc); //r=reg to load, constant
  1472. C67_MVKH(r, fc); //r=reg to load, constant
  1473. } else if (v == VT_LOCAL) {
  1474. C67_MVKL(r, fc + 8); //r=reg to load, constant C67 stack points to next free
  1475. C67_MVKH(r, fc + 8); //r=reg to load, constant
  1476. C67_ADD(C67_FP, r); // MV v,r v -> r
  1477. } else if (v == VT_CMP) {
  1478. C67_MV(C67_compare_reg, r); // MV v,r v -> r
  1479. } else if (v == VT_JMP || v == VT_JMPI) {
  1480. t = v & 1;
  1481. C67_B_DISP(4); // Branch with constant displacement, skip over this branch, load, nop, load
  1482. C67_MVKL(r, t); // r=reg to load, 0 or 1 (do this while branching)
  1483. C67_NOP(4); // NOP 4
  1484. gsym(fc); // modifies other branches to branch here
  1485. C67_MVKL(r, t ^ 1); // r=reg to load, 0 or 1
  1486. } else if (v != r) {
  1487. C67_MV(v, r); // MV v,r v -> r
  1488. if ((ft & VT_BTYPE) == VT_DOUBLE)
  1489. C67_MV(v + 1, r + 1); // MV v,r v -> r
  1490. }
  1491. }
  1492. }
  1493. /* store register 'r' in lvalue 'v' */
  1494. void store(int r, SValue * v)
  1495. {
  1496. int fr, bt, ft, fc, size, t, element;
  1497. ft = v->type.t;
  1498. fc = v->c.ul;
  1499. fr = v->r & VT_VALMASK;
  1500. bt = ft & VT_BTYPE;
  1501. /* XXX: incorrect if float reg to reg */
  1502. if (bt == VT_LDOUBLE) {
  1503. error("long double not supported");
  1504. } else {
  1505. if (bt == VT_SHORT)
  1506. size = 2;
  1507. else if (bt == VT_BYTE)
  1508. size = 1;
  1509. else if (bt == VT_DOUBLE)
  1510. size = 8;
  1511. else
  1512. size = 4;
  1513. if ((v->r & VT_VALMASK) == VT_CONST) {
  1514. /* constant memory reference */
  1515. if (v->r & VT_SYM) {
  1516. greloc(cur_text_section, v->sym, ind, R_C60LO16); // rem the inst need to be patched
  1517. greloc(cur_text_section, v->sym, ind + 4, R_C60HI16);
  1518. }
  1519. C67_MVKL(C67_A0, fc); //r=reg to load, constant
  1520. C67_MVKH(C67_A0, fc); //r=reg to load, constant
  1521. if (size == 1)
  1522. C67_STB_PTR(r, C67_A0); // STB r, *A0
  1523. else if (size == 2)
  1524. C67_STH_PTR(r, C67_A0); // STH r, *A0
  1525. else if (size == 4 || size == 8)
  1526. C67_STW_PTR(r, C67_A0); // STW r, *A0
  1527. if (size == 8)
  1528. C67_STW_PTR_PRE_INC(r + 1, C67_A0, 1); // STW r, *+A0[1]
  1529. } else if ((v->r & VT_VALMASK) == VT_LOCAL) {
  1530. // check case of storing to passed argument that
  1531. // tcc thinks is on the stack but for C67 is
  1532. // passed as a reg. However it may have been
  1533. // saved to the stack, if that reg was required
  1534. // for a call to a child function
  1535. if (fc > 0) // argument ??
  1536. {
  1537. // walk through sizes and figure which param
  1538. int stack_pos = 8;
  1539. for (t = 0; t < NoCallArgsPassedOnStack; t++) {
  1540. if (fc == stack_pos)
  1541. break;
  1542. stack_pos += TranslateStackToReg[t];
  1543. }
  1544. // param has been pushed on stack, get it like a local var
  1545. fc = ParamLocOnStack[t] - 8;
  1546. }
  1547. if (size == 8)
  1548. element = 4;
  1549. else
  1550. element = size;
  1551. // divide offset in bytes to create word index
  1552. C67_MVKL(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant
  1553. C67_MVKH(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant
  1554. if (size == 1)
  1555. C67_STB_SP_A0(r); // STB r, SP[A0]
  1556. else if (size == 2)
  1557. C67_STH_SP_A0(r); // STH r, SP[A0]
  1558. else if (size == 4 || size == 8)
  1559. C67_STW_SP_A0(r); // STW r, SP[A0]
  1560. if (size == 8) {
  1561. C67_ADDK(1, C67_A0); // ADDK 1,A0
  1562. C67_STW_SP_A0(r + 1); // STW r, SP[A0]
  1563. }
  1564. } else {
  1565. if (size == 1)
  1566. C67_STB_PTR(r, fr); // STB r, *fr
  1567. else if (size == 2)
  1568. C67_STH_PTR(r, fr); // STH r, *fr
  1569. else if (size == 4 || size == 8)
  1570. C67_STW_PTR(r, fr); // STW r, *fr
  1571. if (size == 8) {
  1572. C67_STW_PTR_PRE_INC(r + 1, fr, 1); // STW r, *+fr[1]
  1573. }
  1574. }
  1575. }
  1576. }
  1577. /* 'is_jmp' is '1' if it is a jump */
  1578. static void gcall_or_jmp(int is_jmp)
  1579. {
  1580. int r;
  1581. Sym *sym;
  1582. if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
  1583. /* constant case */
  1584. if (vtop->r & VT_SYM) {
  1585. /* relocation case */
  1586. // get add into A0, then start the jump B3
  1587. greloc(cur_text_section, vtop->sym, ind, R_C60LO16); // rem the inst need to be patched
  1588. greloc(cur_text_section, vtop->sym, ind + 4, R_C60HI16);
  1589. C67_MVKL(C67_A0, 0); //r=reg to load, constant
  1590. C67_MVKH(C67_A0, 0); //r=reg to load, constant
  1591. C67_IREG_B_REG(0, C67_CREG_ZERO, C67_A0); // B.S2x A0
  1592. if (is_jmp) {
  1593. C67_NOP(5); // simple jump, just put NOP
  1594. } else {
  1595. // Call, must load return address into B3 during delay slots
  1596. sym = get_sym_ref(&char_pointer_type, cur_text_section, ind + 12, 0); // symbol for return address
  1597. greloc(cur_text_section, sym, ind, R_C60LO16); // rem the inst need to be patched
  1598. greloc(cur_text_section, sym, ind + 4, R_C60HI16);
  1599. C67_MVKL(C67_B3, 0); //r=reg to load, constant
  1600. C67_MVKH(C67_B3, 0); //r=reg to load, constant
  1601. C67_NOP(3); // put remaining NOPs
  1602. }
  1603. } else {
  1604. /* put an empty PC32 relocation */
  1605. ALWAYS_ASSERT(FALSE);
  1606. }
  1607. } else {
  1608. /* otherwise, indirect call */
  1609. r = gv(RC_INT);
  1610. C67_IREG_B_REG(0, C67_CREG_ZERO, r); // B.S2x r
  1611. if (is_jmp) {
  1612. C67_NOP(5); // simple jump, just put NOP
  1613. } else {
  1614. // Call, must load return address into B3 during delay slots
  1615. sym = get_sym_ref(&char_pointer_type, cur_text_section, ind + 12, 0); // symbol for return address
  1616. greloc(cur_text_section, sym, ind, R_C60LO16); // rem the inst need to be patched
  1617. greloc(cur_text_section, sym, ind + 4, R_C60HI16);
  1618. C67_MVKL(C67_B3, 0); //r=reg to load, constant
  1619. C67_MVKH(C67_B3, 0); //r=reg to load, constant
  1620. C67_NOP(3); // put remaining NOPs
  1621. }
  1622. }
  1623. }
  1624. /* generate function call with address in (vtop->t, vtop->c) and free function
  1625. context. Stack entry is popped */
  1626. void gfunc_call(int nb_args)
  1627. {
  1628. int i, r, size = 0;
  1629. int args_sizes[NoCallArgsPassedOnStack];
  1630. if (nb_args > NoCallArgsPassedOnStack) {
  1631. error("more than 10 function params not currently supported");
  1632. // handle more than 10, put some on the stack
  1633. }
  1634. for (i = 0; i < nb_args; i++) {
  1635. if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
  1636. ALWAYS_ASSERT(FALSE);
  1637. } else if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
  1638. ALWAYS_ASSERT(FALSE);
  1639. } else {
  1640. /* simple type (currently always same size) */
  1641. /* XXX: implicit cast ? */
  1642. if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
  1643. error("long long not supported");
  1644. } else if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
  1645. error("long double not supported");
  1646. } else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) {
  1647. size = 8;
  1648. } else {
  1649. size = 4;
  1650. }
  1651. // put the parameter into the corresponding reg (pair)
  1652. r = gv(RC_C67_A4 << (2 * i));
  1653. // must put on stack because with 1 pass compiler , no way to tell
  1654. // if an up coming nested call might overwrite these regs
  1655. C67_PUSH(r);
  1656. if (size == 8) {
  1657. C67_STW_PTR_PRE_INC(r + 1, C67_SP, 3); // STW r, *+SP[3] (go back and put the other)
  1658. }
  1659. args_sizes[i] = size;
  1660. }
  1661. vtop--;
  1662. }
  1663. // POP all the params on the stack into registers for the
  1664. // immediate call (in reverse order)
  1665. for (i = nb_args - 1; i >= 0; i--) {
  1666. if (args_sizes[i] == 8)
  1667. C67_POP_DW(TREG_C67_A4 + i * 2);
  1668. else
  1669. C67_POP(TREG_C67_A4 + i * 2);
  1670. }
  1671. gcall_or_jmp(0);
  1672. vtop--;
  1673. }
  1674. // to be compatible with Code Composer for the C67
  1675. // the first 10 parameters must be passed in registers
  1676. // (pairs for 64 bits) starting wit; A4:A5, then B4:B5 and
  1677. // ending with B12:B13.
  1678. //
  1679. // When a call is made, if the caller has its parameters
  1680. // in regs A4-B13 these must be saved before/as the call
  1681. // parameters are loaded and restored upon return (or if/when needed).
  1682. /* generate function prolog of type 't' */
  1683. void gfunc_prolog(CType * func_type)
  1684. {
  1685. int addr, align, size, func_call, i;
  1686. Sym *sym;
  1687. CType *type;
  1688. sym = func_type->ref;
  1689. func_call = sym->r;
  1690. addr = 8;
  1691. /* if the function returns a structure, then add an
  1692. implicit pointer parameter */
  1693. func_vt = sym->type;
  1694. if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
  1695. func_vc = addr;
  1696. addr += 4;
  1697. }
  1698. NoOfCurFuncArgs = 0;
  1699. /* define parameters */
  1700. while ((sym = sym->next) != NULL) {
  1701. type = &sym->type;
  1702. sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | lvalue_type(type->t), addr);
  1703. size = type_size(type, &align);
  1704. size = (size + 3) & ~3;
  1705. // keep track of size of arguments so
  1706. // we can translate where tcc thinks they
  1707. // are on the stack into the appropriate reg
  1708. TranslateStackToReg[NoOfCurFuncArgs] = size;
  1709. NoOfCurFuncArgs++;
  1710. #ifdef FUNC_STRUCT_PARAM_AS_PTR
  1711. /* structs are passed as pointer */
  1712. if ((type->t & VT_BTYPE) == VT_STRUCT) {
  1713. size = 4;
  1714. }
  1715. #endif
  1716. addr += size;
  1717. }
  1718. func_ret_sub = 0;
  1719. /* pascal type call ? */
  1720. if (func_call == FUNC_STDCALL)
  1721. func_ret_sub = addr - 8;
  1722. C67_MV(C67_FP, C67_A0); // move FP -> A0
  1723. C67_MV(C67_SP, C67_FP); // move SP -> FP
  1724. // place all the args passed in regs onto the stack
  1725. loc = 0;
  1726. for (i = 0; i < NoOfCurFuncArgs; i++) {
  1727. ParamLocOnStack[i] = loc; // remember where the param is
  1728. loc += -8;
  1729. C67_PUSH(TREG_C67_A4 + i * 2);
  1730. if (TranslateStackToReg[i] == 8) {
  1731. C67_STW_PTR_PRE_INC(TREG_C67_A4 + i * 2 + 1, C67_SP, 3); // STW r, *+SP[1] (go back and put the other)
  1732. }
  1733. }
  1734. TotalBytesPushedOnStack = -loc;
  1735. func_sub_sp_offset = ind; // remember where we put the stack instruction
  1736. C67_ADDK(0, C67_SP); // ADDK.L2 loc,SP (just put zero temporarily)
  1737. C67_PUSH(C67_A0);
  1738. C67_PUSH(C67_B3);
  1739. }
  1740. /* generate function epilog */
  1741. void gfunc_epilog(void)
  1742. {
  1743. {
  1744. int local = (-loc + 7) & -8; // stack must stay aligned to 8 bytes for LDDW instr
  1745. C67_POP(C67_B3);
  1746. C67_NOP(4); // NOP wait for load
  1747. C67_IREG_B_REG(0, C67_CREG_ZERO, C67_B3); // B.S2 B3
  1748. C67_POP(C67_FP);
  1749. C67_ADDK(local, C67_SP); // ADDK.L2 loc,SP
  1750. C67_Adjust_ADDK((int *) (cur_text_section->data +
  1751. func_sub_sp_offset),
  1752. -local + TotalBytesPushedOnStack);
  1753. C67_NOP(3); // NOP
  1754. }
  1755. }
  1756. /* generate a jump to a label */
  1757. int gjmp(int t)
  1758. {
  1759. int ind1 = ind;
  1760. C67_MVKL(C67_A0, t); //r=reg to load, constant
  1761. C67_MVKH(C67_A0, t); //r=reg to load, constant
  1762. C67_IREG_B_REG(0, C67_CREG_ZERO, C67_A0); // [!R] B.S2x A0
  1763. C67_NOP(5);
  1764. return ind1;
  1765. }
  1766. /* generate a jump to a fixed address */
  1767. void gjmp_addr(int a)
  1768. {
  1769. Sym *sym;
  1770. // I guess this routine is used for relative short
  1771. // local jumps, for now just handle it as the general
  1772. // case
  1773. // define a label that will be relocated
  1774. sym = get_sym_ref(&char_pointer_type, cur_text_section, a, 0);
  1775. greloc(cur_text_section, sym, ind, R_C60LO16);
  1776. greloc(cur_text_section, sym, ind + 4, R_C60HI16);
  1777. gjmp(0); // place a zero there later the symbol will be added to it
  1778. }
  1779. /* generate a test. set 'inv' to invert test. Stack entry is popped */
  1780. int gtst(int inv, int t)
  1781. {
  1782. int ind1, n;
  1783. int v, *p;
  1784. v = vtop->r & VT_VALMASK;
  1785. if (v == VT_CMP) {
  1786. /* fast case : can jump directly since flags are set */
  1787. // C67 uses B2 sort of as flags register
  1788. ind1 = ind;
  1789. C67_MVKL(C67_A0, t); //r=reg to load, constant
  1790. C67_MVKH(C67_A0, t); //r=reg to load, constant
  1791. if (C67_compare_reg != TREG_EAX && // check if not already in a conditional test reg
  1792. C67_compare_reg != TREG_EDX &&
  1793. C67_compare_reg != TREG_ST0 && C67_compare_reg != C67_B2) {
  1794. C67_MV(C67_compare_reg, C67_B2);
  1795. C67_compare_reg = C67_B2;
  1796. }
  1797. C67_IREG_B_REG(C67_invert_test ^ inv, C67_compare_reg, C67_A0); // [!R] B.S2x A0
  1798. C67_NOP(5);
  1799. t = ind1; //return where we need to patch
  1800. } else if (v == VT_JMP || v == VT_JMPI) {
  1801. /* && or || optimization */
  1802. if ((v & 1) == inv) {
  1803. /* insert vtop->c jump list in t */
  1804. p = &vtop->c.i;
  1805. // I guess the idea is to traverse to the
  1806. // null at the end of the list and store t
  1807. // there
  1808. n = *p;
  1809. while (n != 0) {
  1810. p = (int *) (cur_text_section->data + n);
  1811. // extract 32 bit address from MVKH/MVKL
  1812. n = ((*p >> 7) & 0xffff);
  1813. n |= ((*(p + 1) >> 7) & 0xffff) << 16;
  1814. }
  1815. *p |= (t & 0xffff) << 7;
  1816. *(p + 1) |= ((t >> 16) & 0xffff) << 7;
  1817. t = vtop->c.i;
  1818. } else {
  1819. t = gjmp(t);
  1820. gsym(vtop->c.i);
  1821. }
  1822. } else {
  1823. if (is_float(vtop->type.t)) {
  1824. vpushi(0);
  1825. gen_op(TOK_NE);
  1826. }
  1827. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  1828. /* constant jmp optimization */
  1829. if ((vtop->c.i != 0) != inv)
  1830. t = gjmp(t);
  1831. } else {
  1832. // I think we need to get the value on the stack
  1833. // into a register, test it, and generate a branch
  1834. // return the address of the branch, so it can be
  1835. // later patched
  1836. v = gv(RC_INT); // get value into a reg
  1837. ind1 = ind;
  1838. C67_MVKL(C67_A0, t); //r=reg to load, constant
  1839. C67_MVKH(C67_A0, t); //r=reg to load, constant
  1840. if (v != TREG_EAX && // check if not already in a conditional test reg
  1841. v != TREG_EDX && v != TREG_ST0 && v != C67_B2) {
  1842. C67_MV(v, C67_B2);
  1843. v = C67_B2;
  1844. }
  1845. C67_IREG_B_REG(inv, v, C67_A0); // [!R] B.S2x A0
  1846. C67_NOP(5);
  1847. t = ind1; //return where we need to patch
  1848. ind1 = ind;
  1849. }
  1850. }
  1851. vtop--;
  1852. return t;
  1853. }
  1854. /* generate an integer binary operation */
  1855. void gen_opi(int op)
  1856. {
  1857. int r, fr, opc, t;
  1858. switch (op) {
  1859. case '+':
  1860. case TOK_ADDC1: /* add with carry generation */
  1861. opc = 0;
  1862. gen_op8:
  1863. // C67 can't do const compares, must load into a reg
  1864. // so just go to gv2 directly - tktk
  1865. if (op >= TOK_ULT && op <= TOK_GT)
  1866. gv2(RC_INT_BSIDE, RC_INT); // make sure r (src1) is on the B Side of CPU
  1867. else
  1868. gv2(RC_INT, RC_INT);
  1869. r = vtop[-1].r;
  1870. fr = vtop[0].r;
  1871. C67_compare_reg = C67_B2;
  1872. if (op == TOK_LT) {
  1873. C67_CMPLT(r, fr, C67_B2);
  1874. C67_invert_test = false;
  1875. } else if (op == TOK_GE) {
  1876. C67_CMPLT(r, fr, C67_B2);
  1877. C67_invert_test = true;
  1878. } else if (op == TOK_GT) {
  1879. C67_CMPGT(r, fr, C67_B2);
  1880. C67_invert_test = false;
  1881. } else if (op == TOK_LE) {
  1882. C67_CMPGT(r, fr, C67_B2);
  1883. C67_invert_test = true;
  1884. } else if (op == TOK_EQ) {
  1885. C67_CMPEQ(r, fr, C67_B2);
  1886. C67_invert_test = false;
  1887. } else if (op == TOK_NE) {
  1888. C67_CMPEQ(r, fr, C67_B2);
  1889. C67_invert_test = true;
  1890. } else if (op == TOK_ULT) {
  1891. C67_CMPLTU(r, fr, C67_B2);
  1892. C67_invert_test = false;
  1893. } else if (op == TOK_UGE) {
  1894. C67_CMPLTU(r, fr, C67_B2);
  1895. C67_invert_test = true;
  1896. } else if (op == TOK_UGT) {
  1897. C67_CMPGTU(r, fr, C67_B2);
  1898. C67_invert_test = false;
  1899. } else if (op == TOK_ULE) {
  1900. C67_CMPGTU(r, fr, C67_B2);
  1901. C67_invert_test = true;
  1902. } else if (op == '+')
  1903. C67_ADD(fr, r); // ADD r,fr,r
  1904. else if (op == '-')
  1905. C67_SUB(fr, r); // SUB r,fr,r
  1906. else if (op == '&')
  1907. C67_AND(fr, r); // AND r,fr,r
  1908. else if (op == '|')
  1909. C67_OR(fr, r); // OR r,fr,r
  1910. else if (op == '^')
  1911. C67_XOR(fr, r); // XOR r,fr,r
  1912. else
  1913. ALWAYS_ASSERT(FALSE);
  1914. vtop--;
  1915. if (op >= TOK_ULT && op <= TOK_GT) {
  1916. vtop->r = VT_CMP;
  1917. vtop->c.i = op;
  1918. }
  1919. break;
  1920. case '-':
  1921. case TOK_SUBC1: /* sub with carry generation */
  1922. opc = 5;
  1923. goto gen_op8;
  1924. case TOK_ADDC2: /* add with carry use */
  1925. opc = 2;
  1926. goto gen_op8;
  1927. case TOK_SUBC2: /* sub with carry use */
  1928. opc = 3;
  1929. goto gen_op8;
  1930. case '&':
  1931. opc = 4;
  1932. goto gen_op8;
  1933. case '^':
  1934. opc = 6;
  1935. goto gen_op8;
  1936. case '|':
  1937. opc = 1;
  1938. goto gen_op8;
  1939. case '*':
  1940. case TOK_UMULL:
  1941. gv2(RC_INT, RC_INT);
  1942. r = vtop[-1].r;
  1943. fr = vtop[0].r;
  1944. vtop--;
  1945. C67_MPYI(fr, r); // 32 bit bultiply fr,r,fr
  1946. C67_NOP(8); // NOP 8 for worst case
  1947. break;
  1948. case TOK_SHL:
  1949. gv2(RC_INT_BSIDE, RC_INT_BSIDE); // shift amount must be on same side as dst
  1950. r = vtop[-1].r;
  1951. fr = vtop[0].r;
  1952. vtop--;
  1953. C67_SHL(fr, r); // arithmetic/logical shift
  1954. break;
  1955. case TOK_SHR:
  1956. gv2(RC_INT_BSIDE, RC_INT_BSIDE); // shift amount must be on same side as dst
  1957. r = vtop[-1].r;
  1958. fr = vtop[0].r;
  1959. vtop--;
  1960. C67_SHRU(fr, r); // logical shift
  1961. break;
  1962. case TOK_SAR:
  1963. gv2(RC_INT_BSIDE, RC_INT_BSIDE); // shift amount must be on same side as dst
  1964. r = vtop[-1].r;
  1965. fr = vtop[0].r;
  1966. vtop--;
  1967. C67_SHR(fr, r); // arithmetic shift
  1968. break;
  1969. case '/':
  1970. t = TOK__divi;
  1971. call_func:
  1972. vswap();
  1973. /* call generic idiv function */
  1974. vpush_global_sym(&func_old_type, t);
  1975. vrott(3);
  1976. gfunc_call(2);
  1977. vpushi(0);
  1978. vtop->r = REG_IRET;
  1979. vtop->r2 = VT_CONST;
  1980. break;
  1981. case TOK_UDIV:
  1982. case TOK_PDIV:
  1983. t = TOK__divu;
  1984. goto call_func;
  1985. case '%':
  1986. t = TOK__remi;
  1987. goto call_func;
  1988. case TOK_UMOD:
  1989. t = TOK__remu;
  1990. goto call_func;
  1991. default:
  1992. opc = 7;
  1993. goto gen_op8;
  1994. }
  1995. }
  1996. /* generate a floating point operation 'v = t1 op t2' instruction. The
  1997. two operands are guaranted to have the same floating point type */
  1998. /* XXX: need to use ST1 too */
  1999. void gen_opf(int op)
  2000. {
  2001. int ft, fc, fr, r;
  2002. if (op >= TOK_ULT && op <= TOK_GT)
  2003. gv2(RC_EDX, RC_EAX); // make sure src2 is on b side
  2004. else
  2005. gv2(RC_FLOAT, RC_FLOAT); // make sure src2 is on b side
  2006. ft = vtop->type.t;
  2007. fc = vtop->c.ul;
  2008. r = vtop->r;
  2009. fr = vtop[-1].r;
  2010. if ((ft & VT_BTYPE) == VT_LDOUBLE)
  2011. error("long doubles not supported");
  2012. if (op >= TOK_ULT && op <= TOK_GT) {
  2013. r = vtop[-1].r;
  2014. fr = vtop[0].r;
  2015. C67_compare_reg = C67_B2;
  2016. if (op == TOK_LT) {
  2017. if ((ft & VT_BTYPE) == VT_DOUBLE)
  2018. C67_CMPLTDP(r, fr, C67_B2);
  2019. else
  2020. C67_CMPLTSP(r, fr, C67_B2);
  2021. C67_invert_test = false;
  2022. } else if (op == TOK_GE) {
  2023. if ((ft & VT_BTYPE) == VT_DOUBLE)
  2024. C67_CMPLTDP(r, fr, C67_B2);
  2025. else
  2026. C67_CMPLTSP(r, fr, C67_B2);
  2027. C67_invert_test = true;
  2028. } else if (op == TOK_GT) {
  2029. if ((ft & VT_BTYPE) == VT_DOUBLE)
  2030. C67_CMPGTDP(r, fr, C67_B2);
  2031. else
  2032. C67_CMPGTSP(r, fr, C67_B2);
  2033. C67_invert_test = false;
  2034. } else if (op == TOK_LE) {
  2035. if ((ft & VT_BTYPE) == VT_DOUBLE)
  2036. C67_CMPGTDP(r, fr, C67_B2);
  2037. else
  2038. C67_CMPGTSP(r, fr, C67_B2);
  2039. C67_invert_test = true;
  2040. } else if (op == TOK_EQ) {
  2041. if ((ft & VT_BTYPE) == VT_DOUBLE)
  2042. C67_CMPEQDP(r, fr, C67_B2);
  2043. else
  2044. C67_CMPEQSP(r, fr, C67_B2);
  2045. C67_invert_test = false;
  2046. } else if (op == TOK_NE) {
  2047. if ((ft & VT_BTYPE) == VT_DOUBLE)
  2048. C67_CMPEQDP(r, fr, C67_B2);
  2049. else
  2050. C67_CMPEQSP(r, fr, C67_B2);
  2051. C67_invert_test = true;
  2052. } else {
  2053. ALWAYS_ASSERT(FALSE);
  2054. }
  2055. vtop->r = VT_CMP; // tell TCC that result is in "flags" actually B2
  2056. } else {
  2057. if (op == '+') {
  2058. if ((ft & VT_BTYPE) == VT_DOUBLE) {
  2059. C67_ADDDP(r, fr); // ADD fr,r,fr
  2060. C67_NOP(6);
  2061. } else {
  2062. C67_ADDSP(r, fr); // ADD fr,r,fr
  2063. C67_NOP(3);
  2064. }
  2065. vtop--;
  2066. } else if (op == '-') {
  2067. if ((ft & VT_BTYPE) == VT_DOUBLE) {
  2068. C67_SUBDP(r, fr); // SUB fr,r,fr
  2069. C67_NOP(6);
  2070. } else {
  2071. C67_SUBSP(r, fr); // SUB fr,r,fr
  2072. C67_NOP(3);
  2073. }
  2074. vtop--;
  2075. } else if (op == '*') {
  2076. if ((ft & VT_BTYPE) == VT_DOUBLE) {
  2077. C67_MPYDP(r, fr); // MPY fr,r,fr
  2078. C67_NOP(9);
  2079. } else {
  2080. C67_MPYSP(r, fr); // MPY fr,r,fr
  2081. C67_NOP(3);
  2082. }
  2083. vtop--;
  2084. } else if (op == '/') {
  2085. if ((ft & VT_BTYPE) == VT_DOUBLE) {
  2086. // must call intrinsic DP floating point divide
  2087. vswap();
  2088. /* call generic idiv function */
  2089. vpush_global_sym(&func_old_type, TOK__divd);
  2090. vrott(3);
  2091. gfunc_call(2);
  2092. vpushi(0);
  2093. vtop->r = REG_FRET;
  2094. vtop->r2 = REG_LRET;
  2095. } else {
  2096. // must call intrinsic SP floating point divide
  2097. vswap();
  2098. /* call generic idiv function */
  2099. vpush_global_sym(&func_old_type, TOK__divf);
  2100. vrott(3);
  2101. gfunc_call(2);
  2102. vpushi(0);
  2103. vtop->r = REG_FRET;
  2104. vtop->r2 = VT_CONST;
  2105. }
  2106. } else
  2107. ALWAYS_ASSERT(FALSE);
  2108. }
  2109. }
  2110. /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
  2111. and 'long long' cases. */
  2112. void gen_cvt_itof(int t)
  2113. {
  2114. int r;
  2115. gv(RC_INT);
  2116. r = vtop->r;
  2117. if ((t & VT_BTYPE) == VT_DOUBLE) {
  2118. if (t & VT_UNSIGNED)
  2119. C67_INTDPU(r, r);
  2120. else
  2121. C67_INTDP(r, r);
  2122. C67_NOP(4);
  2123. vtop->type.t = VT_DOUBLE;
  2124. } else {
  2125. if (t & VT_UNSIGNED)
  2126. C67_INTSPU(r, r);
  2127. else
  2128. C67_INTSP(r, r);
  2129. C67_NOP(3);
  2130. vtop->type.t = VT_FLOAT;
  2131. }
  2132. }
  2133. /* convert fp to int 't' type */
  2134. /* XXX: handle long long case */
  2135. void gen_cvt_ftoi(int t)
  2136. {
  2137. int r;
  2138. gv(RC_FLOAT);
  2139. r = vtop->r;
  2140. if (t != VT_INT)
  2141. error("long long not supported");
  2142. else {
  2143. if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) {
  2144. C67_DPTRUNC(r, r);
  2145. C67_NOP(3);
  2146. } else {
  2147. C67_SPTRUNC(r, r);
  2148. C67_NOP(3);
  2149. }
  2150. vtop->type.t = VT_INT;
  2151. }
  2152. }
  2153. /* convert from one floating point type to another */
  2154. void gen_cvt_ftof(int t)
  2155. {
  2156. int r, r2;
  2157. if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE &&
  2158. (t & VT_BTYPE) == VT_FLOAT) {
  2159. // convert double to float
  2160. gv(RC_FLOAT); // get it in a register pair
  2161. r = vtop->r;
  2162. C67_DPSP(r, r); // convert it to SP same register
  2163. C67_NOP(3);
  2164. vtop->type.t = VT_FLOAT;
  2165. vtop->r2 = VT_CONST; // set this as unused
  2166. } else if ((vtop->type.t & VT_BTYPE) == VT_FLOAT &&
  2167. (t & VT_BTYPE) == VT_DOUBLE) {
  2168. // convert float to double
  2169. gv(RC_FLOAT); // get it in a register
  2170. r = vtop->r;
  2171. if (r == TREG_EAX) { // make sure the paired reg is avail
  2172. r2 = get_reg(RC_ECX);
  2173. } else if (r == TREG_EDX) {
  2174. r2 = get_reg(RC_ST0);
  2175. } else {
  2176. ALWAYS_ASSERT(FALSE);
  2177. r2 = 0; /* avoid warning */
  2178. }
  2179. C67_SPDP(r, r); // convert it to DP same register
  2180. C67_NOP(1);
  2181. vtop->type.t = VT_DOUBLE;
  2182. vtop->r2 = r2; // set this as unused
  2183. } else {
  2184. ALWAYS_ASSERT(FALSE);
  2185. }
  2186. }
  2187. /* computed goto support */
  2188. void ggoto(void)
  2189. {
  2190. gcall_or_jmp(1);
  2191. vtop--;
  2192. }
  2193. /* end of X86 code generator */
  2194. /*************************************************************/