ia64-gen.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873
  1. /* ia64-gen.c -- Generate a shrunk set of opcode tables
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Written by Bob Manson, Cygnus Solutions, <manson@cygnus.com>
  4. This file is part of the GNU opcodes library.
  5. This library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. It is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this file; see the file COPYING. If not, write to the
  15. Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. /* While the ia64-opc-* set of opcode tables are easy to maintain,
  18. they waste a tremendous amount of space. ia64-gen rearranges the
  19. instructions into a directed acyclic graph (DAG) of instruction opcodes and
  20. their possible completers, as well as compacting the set of strings used.
  21. The disassembler table consists of a state machine that does
  22. branching based on the bits of the opcode being disassembled. The
  23. state encodings have been chosen to minimize the amount of space
  24. required.
  25. The resource table is constructed based on some text dependency tables,
  26. which are also easier to maintain than the final representation. */
  27. #include "sysdep.h"
  28. #include <stdio.h>
  29. #include <stdarg.h>
  30. #include <errno.h>
  31. #include "libiberty.h"
  32. #include "safe-ctype.h"
  33. #include "getopt.h"
  34. #include "ia64-opc.h"
  35. #include "ia64-opc-a.c"
  36. #include "ia64-opc-i.c"
  37. #include "ia64-opc-m.c"
  38. #include "ia64-opc-b.c"
  39. #include "ia64-opc-f.c"
  40. #include "ia64-opc-x.c"
  41. #include "ia64-opc-d.c"
  42. #include <libintl.h>
  43. #define _(String) gettext (String)
  44. /* This is a copy of fprintf_vma from bfd/bfd-in2.h. We have to use this
  45. always, because we might be compiled without BFD64 defined, if configured
  46. for a 32-bit target and --enable-targets=all is used. This will work for
  47. both 32-bit and 64-bit hosts. */
  48. #define _opcode_int64_low(x) ((unsigned long) (((x) & 0xffffffff)))
  49. #define _opcode_int64_high(x) ((unsigned long) (((x) >> 32) & 0xffffffff))
  50. #define opcode_fprintf_vma(s,x) \
  51. fprintf ((s), "%08lx%08lx", _opcode_int64_high (x), _opcode_int64_low (x))
  52. const char * program_name = NULL;
  53. int debug = 0;
  54. #define NELEMS(a) (sizeof (a) / sizeof ((a)[0]))
  55. #define tmalloc(X) (X *) xmalloc (sizeof (X))
  56. typedef unsigned long long ci_t;
  57. /* The main opcode table entry. Each entry is a unique combination of
  58. name and flags (no two entries in the table compare as being equal
  59. via opcodes_eq). */
  60. struct main_entry
  61. {
  62. /* The base name of this opcode. The names of its completers are
  63. appended to it to generate the full instruction name. */
  64. struct string_entry *name;
  65. /* The base opcode entry. Which one to use is a fairly arbitrary choice;
  66. it uses the first one passed to add_opcode_entry. */
  67. struct ia64_opcode *opcode;
  68. /* The list of completers that can be applied to this opcode. */
  69. struct completer_entry *completers;
  70. /* Next entry in the chain. */
  71. struct main_entry *next;
  72. /* Index in the main table. */
  73. int main_index;
  74. } *maintable, **ordered_table;
  75. int otlen = 0;
  76. int ottotlen = 0;
  77. int opcode_count = 0;
  78. /* The set of possible completers for an opcode. */
  79. struct completer_entry
  80. {
  81. /* This entry's index in the ia64_completer_table[] array. */
  82. int num;
  83. /* The name of the completer. */
  84. struct string_entry *name;
  85. /* This entry's parent. */
  86. struct completer_entry *parent;
  87. /* Set if this is a terminal completer (occurs at the end of an
  88. opcode). */
  89. int is_terminal;
  90. /* An alternative completer. */
  91. struct completer_entry *alternative;
  92. /* Additional completers that can be appended to this one. */
  93. struct completer_entry *addl_entries;
  94. /* Before compute_completer_bits () is invoked, this contains the actual
  95. instruction opcode for this combination of opcode and completers.
  96. Afterwards, it contains those bits that are different from its
  97. parent opcode. */
  98. ia64_insn bits;
  99. /* Bits set to 1 correspond to those bits in this completer's opcode
  100. that are different from its parent completer's opcode (or from
  101. the base opcode if the entry is the root of the opcode's completer
  102. list). This field is filled in by compute_completer_bits (). */
  103. ia64_insn mask;
  104. /* Index into the opcode dependency list, or -1 if none. */
  105. int dependencies;
  106. /* Remember the order encountered in the opcode tables. */
  107. int order;
  108. };
  109. /* One entry in the disassembler name table. */
  110. struct disent
  111. {
  112. /* The index into the ia64_name_dis array for this entry. */
  113. int ournum;
  114. /* The index into the main_table[] array. */
  115. int insn;
  116. /* The disassmbly priority of this entry. */
  117. int priority;
  118. /* The completer_index value for this entry. */
  119. ci_t completer_index;
  120. /* How many other entries share this decode. */
  121. int nextcnt;
  122. /* The next entry sharing the same decode. */
  123. struct disent *nexte;
  124. /* The next entry in the name list. */
  125. struct disent *next_ent;
  126. } *disinsntable = NULL;
  127. /* A state machine that will eventually be used to generate the
  128. disassembler table. */
  129. struct bittree
  130. {
  131. struct disent *disent;
  132. struct bittree *bits[3]; /* 0, 1, and X (don't care). */
  133. int bits_to_skip;
  134. int skip_flag;
  135. } *bittree;
  136. /* The string table contains all opcodes and completers sorted in
  137. alphabetical order. */
  138. /* One entry in the string table. */
  139. struct string_entry
  140. {
  141. /* The index in the ia64_strings[] array for this entry. */
  142. int num;
  143. /* And the string. */
  144. char *s;
  145. } **string_table = NULL;
  146. int strtablen = 0;
  147. int strtabtotlen = 0;
  148. /* Resource dependency entries. */
  149. struct rdep
  150. {
  151. char *name; /* Resource name. */
  152. unsigned
  153. mode:2, /* RAW, WAW, or WAR. */
  154. semantics:3; /* Dependency semantics. */
  155. char *extra; /* Additional semantics info. */
  156. int nchks;
  157. int total_chks; /* Total #of terminal insns. */
  158. int *chks; /* Insn classes which read (RAW), write
  159. (WAW), or write (WAR) this rsrc. */
  160. int *chknotes; /* Dependency notes for each class. */
  161. int nregs;
  162. int total_regs; /* Total #of terminal insns. */
  163. int *regs; /* Insn class which write (RAW), write2
  164. (WAW), or read (WAR) this rsrc. */
  165. int *regnotes; /* Dependency notes for each class. */
  166. int waw_special; /* Special WAW dependency note. */
  167. } **rdeps = NULL;
  168. static int rdepslen = 0;
  169. static int rdepstotlen = 0;
  170. /* Array of all instruction classes. */
  171. struct iclass
  172. {
  173. char *name; /* Instruction class name. */
  174. int is_class; /* Is a class, not a terminal. */
  175. int nsubs;
  176. int *subs; /* Other classes within this class. */
  177. int nxsubs;
  178. int xsubs[4]; /* Exclusions. */
  179. char *comment; /* Optional comment. */
  180. int note; /* Optional note. */
  181. int terminal_resolved; /* Did we match this with anything? */
  182. int orphan; /* Detect class orphans. */
  183. } **ics = NULL;
  184. static int iclen = 0;
  185. static int ictotlen = 0;
  186. /* An opcode dependency (chk/reg pair of dependency lists). */
  187. struct opdep
  188. {
  189. int chk; /* index into dlists */
  190. int reg; /* index into dlists */
  191. } **opdeps;
  192. static int opdeplen = 0;
  193. static int opdeptotlen = 0;
  194. /* A generic list of dependencies w/notes encoded. These may be shared. */
  195. struct deplist
  196. {
  197. int len;
  198. unsigned short *deps;
  199. } **dlists;
  200. static int dlistlen = 0;
  201. static int dlisttotlen = 0;
  202. static void fail (const char *, ...) ATTRIBUTE_PRINTF_1;
  203. static void warn (const char *, ...) ATTRIBUTE_PRINTF_1;
  204. static struct rdep * insert_resource (const char *, enum ia64_dependency_mode);
  205. static int deplist_equals (struct deplist *, struct deplist *);
  206. static short insert_deplist (int, unsigned short *);
  207. static short insert_dependencies (int, unsigned short *, int, unsigned short *);
  208. static void mark_used (struct iclass *, int);
  209. static int fetch_insn_class (const char *, int);
  210. static int sub_compare (const void *, const void *);
  211. static void load_insn_classes (void);
  212. static void parse_resource_users (const char *, int **, int *, int **);
  213. static int parse_semantics (char *);
  214. static void add_dep (const char *, const char *, const char *, int, int, char *, int);
  215. static void load_depfile (const char *, enum ia64_dependency_mode);
  216. static void load_dependencies (void);
  217. static int irf_operand (int, const char *);
  218. static int in_iclass_mov_x (struct ia64_opcode *, struct iclass *, const char *, const char *);
  219. static int in_iclass (struct ia64_opcode *, struct iclass *, const char *, const char *, int *);
  220. static int lookup_regindex (const char *, int);
  221. static int lookup_specifier (const char *);
  222. static void print_dependency_table (void);
  223. static struct string_entry * insert_string (char *);
  224. static void gen_dis_table (struct bittree *);
  225. static void print_dis_table (void);
  226. static void generate_disassembler (void);
  227. static void print_string_table (void);
  228. static int completer_entries_eq (struct completer_entry *, struct completer_entry *);
  229. static struct completer_entry * insert_gclist (struct completer_entry *);
  230. static int get_prefix_len (const char *);
  231. static void compute_completer_bits (struct main_entry *, struct completer_entry *);
  232. static void collapse_redundant_completers (void);
  233. static int insert_opcode_dependencies (struct ia64_opcode *, struct completer_entry *);
  234. static void insert_completer_entry (struct ia64_opcode *, struct main_entry *, int);
  235. static void print_completer_entry (struct completer_entry *);
  236. static void print_completer_table (void);
  237. static int opcodes_eq (struct ia64_opcode *, struct ia64_opcode *);
  238. static void add_opcode_entry (struct ia64_opcode *);
  239. static void print_main_table (void);
  240. static void shrink (struct ia64_opcode *);
  241. static void print_version (void);
  242. static void usage (FILE *, int);
  243. static void finish_distable (void);
  244. static void insert_bit_table_ent (struct bittree *, int, ia64_insn, ia64_insn, int, int, ci_t);
  245. static void add_dis_entry (struct bittree *, ia64_insn, ia64_insn, int, struct completer_entry *, ci_t);
  246. static void compact_distree (struct bittree *);
  247. static struct bittree * make_bittree_entry (void);
  248. static struct disent * add_dis_table_ent (struct disent *, int, int, ci_t);
  249. static void
  250. fail (const char *message, ...)
  251. {
  252. va_list args;
  253. va_start (args, message);
  254. fprintf (stderr, _("%s: Error: "), program_name);
  255. vfprintf (stderr, message, args);
  256. va_end (args);
  257. xexit (1);
  258. }
  259. static void
  260. warn (const char *message, ...)
  261. {
  262. va_list args;
  263. va_start (args, message);
  264. fprintf (stderr, _("%s: Warning: "), program_name);
  265. vfprintf (stderr, message, args);
  266. va_end (args);
  267. }
  268. /* Add NAME to the resource table, where TYPE is RAW or WAW. */
  269. static struct rdep *
  270. insert_resource (const char *name, enum ia64_dependency_mode type)
  271. {
  272. if (rdepslen == rdepstotlen)
  273. {
  274. rdepstotlen += 20;
  275. rdeps = (struct rdep **)
  276. xrealloc (rdeps, sizeof(struct rdep **) * rdepstotlen);
  277. }
  278. rdeps[rdepslen] = tmalloc(struct rdep);
  279. memset((void *)rdeps[rdepslen], 0, sizeof(struct rdep));
  280. rdeps[rdepslen]->name = xstrdup (name);
  281. rdeps[rdepslen]->mode = type;
  282. rdeps[rdepslen]->waw_special = 0;
  283. return rdeps[rdepslen++];
  284. }
  285. /* Are the lists of dependency indexes equivalent? */
  286. static int
  287. deplist_equals (struct deplist *d1, struct deplist *d2)
  288. {
  289. int i;
  290. if (d1->len != d2->len)
  291. return 0;
  292. for (i = 0; i < d1->len; i++)
  293. if (d1->deps[i] != d2->deps[i])
  294. return 0;
  295. return 1;
  296. }
  297. /* Add the list of dependencies to the list of dependency lists. */
  298. static short
  299. insert_deplist (int count, unsigned short *deps)
  300. {
  301. /* Sort the list, then see if an equivalent list exists already.
  302. this results in a much smaller set of dependency lists. */
  303. struct deplist *list;
  304. char set[0x10000];
  305. int i;
  306. memset ((void *)set, 0, sizeof (set));
  307. for (i = 0; i < count; i++)
  308. set[deps[i]] = 1;
  309. count = 0;
  310. for (i = 0; i < (int) sizeof (set); i++)
  311. if (set[i])
  312. ++count;
  313. list = tmalloc (struct deplist);
  314. list->len = count;
  315. list->deps = (unsigned short *) malloc (sizeof (unsigned short) * count);
  316. for (i = 0, count = 0; i < (int) sizeof (set); i++)
  317. if (set[i])
  318. list->deps[count++] = i;
  319. /* Does this list exist already? */
  320. for (i = 0; i < dlistlen; i++)
  321. if (deplist_equals (list, dlists[i]))
  322. {
  323. free (list->deps);
  324. free (list);
  325. return i;
  326. }
  327. if (dlistlen == dlisttotlen)
  328. {
  329. dlisttotlen += 20;
  330. dlists = (struct deplist **)
  331. xrealloc (dlists, sizeof(struct deplist **) * dlisttotlen);
  332. }
  333. dlists[dlistlen] = list;
  334. return dlistlen++;
  335. }
  336. /* Add the given pair of dependency lists to the opcode dependency list. */
  337. static short
  338. insert_dependencies (int nchks, unsigned short *chks,
  339. int nregs, unsigned short *regs)
  340. {
  341. struct opdep *pair;
  342. int i;
  343. int regind = -1;
  344. int chkind = -1;
  345. if (nregs > 0)
  346. regind = insert_deplist (nregs, regs);
  347. if (nchks > 0)
  348. chkind = insert_deplist (nchks, chks);
  349. for (i = 0; i < opdeplen; i++)
  350. if (opdeps[i]->chk == chkind
  351. && opdeps[i]->reg == regind)
  352. return i;
  353. pair = tmalloc (struct opdep);
  354. pair->chk = chkind;
  355. pair->reg = regind;
  356. if (opdeplen == opdeptotlen)
  357. {
  358. opdeptotlen += 20;
  359. opdeps = (struct opdep **)
  360. xrealloc (opdeps, sizeof(struct opdep **) * opdeptotlen);
  361. }
  362. opdeps[opdeplen] = pair;
  363. return opdeplen++;
  364. }
  365. static void
  366. mark_used (struct iclass *ic, int clear_terminals)
  367. {
  368. int i;
  369. ic->orphan = 0;
  370. if (clear_terminals)
  371. ic->terminal_resolved = 1;
  372. for (i = 0; i < ic->nsubs; i++)
  373. mark_used (ics[ic->subs[i]], clear_terminals);
  374. for (i = 0; i < ic->nxsubs; i++)
  375. mark_used (ics[ic->xsubs[i]], clear_terminals);
  376. }
  377. /* Look up an instruction class; if CREATE make a new one if none found;
  378. returns the index into the insn class array. */
  379. static int
  380. fetch_insn_class (const char *full_name, int create)
  381. {
  382. char *name;
  383. char *notestr;
  384. char *xsect;
  385. char *comment;
  386. int i, note = 0;
  387. int ind;
  388. int is_class = 0;
  389. if (startswith (full_name, "IC:"))
  390. {
  391. name = xstrdup (full_name + 3);
  392. is_class = 1;
  393. }
  394. else
  395. name = xstrdup (full_name);
  396. if ((xsect = strchr(name, '\\')) != NULL)
  397. is_class = 1;
  398. if ((comment = strchr(name, '[')) != NULL)
  399. is_class = 1;
  400. if ((notestr = strchr(name, '+')) != NULL)
  401. is_class = 1;
  402. /* If it is a composite class, then ignore comments and notes that come after
  403. the '\\', since they don't apply to the part we are decoding now. */
  404. if (xsect)
  405. {
  406. if (comment > xsect)
  407. comment = 0;
  408. if (notestr > xsect)
  409. notestr = 0;
  410. }
  411. if (notestr)
  412. {
  413. char *nextnotestr;
  414. note = atoi (notestr + 1);
  415. if ((nextnotestr = strchr (notestr + 1, '+')) != NULL)
  416. {
  417. if (strcmp (notestr, "+1+13") == 0)
  418. note = 13;
  419. else if (!xsect || nextnotestr < xsect)
  420. warn (_("multiple note %s not handled\n"), notestr);
  421. }
  422. }
  423. /* If it's a composite class, leave the notes and comments in place so that
  424. we have a unique name for the composite class. Otherwise, we remove
  425. them. */
  426. if (!xsect)
  427. {
  428. if (notestr)
  429. *notestr = 0;
  430. if (comment)
  431. *comment = 0;
  432. }
  433. for (i = 0; i < iclen; i++)
  434. if (strcmp (name, ics[i]->name) == 0
  435. && ((comment == NULL && ics[i]->comment == NULL)
  436. || (comment != NULL && ics[i]->comment != NULL
  437. && strncmp (ics[i]->comment, comment,
  438. strlen (ics[i]->comment)) == 0))
  439. && note == ics[i]->note)
  440. return i;
  441. if (!create)
  442. return -1;
  443. /* Doesn't exist, so make a new one. */
  444. if (iclen == ictotlen)
  445. {
  446. ictotlen += 20;
  447. ics = (struct iclass **)
  448. xrealloc (ics, (ictotlen) * sizeof (struct iclass *));
  449. }
  450. ind = iclen++;
  451. ics[ind] = tmalloc (struct iclass);
  452. memset ((void *)ics[ind], 0, sizeof (struct iclass));
  453. ics[ind]->name = xstrdup (name);
  454. ics[ind]->is_class = is_class;
  455. ics[ind]->orphan = 1;
  456. if (comment)
  457. {
  458. ics[ind]->comment = xstrdup (comment + 1);
  459. ics[ind]->comment[strlen (ics[ind]->comment)-1] = 0;
  460. }
  461. if (notestr)
  462. ics[ind]->note = note;
  463. /* If it's a composite class, there's a comment or note, look for an
  464. existing class or terminal with the same name. */
  465. if ((xsect || comment || notestr) && is_class)
  466. {
  467. /* First, populate with the class we're based on. */
  468. char *subname = name;
  469. if (xsect)
  470. *xsect = 0;
  471. else if (comment)
  472. *comment = 0;
  473. else if (notestr)
  474. *notestr = 0;
  475. ics[ind]->nsubs = 1;
  476. ics[ind]->subs = tmalloc(int);
  477. ics[ind]->subs[0] = fetch_insn_class (subname, 1);
  478. }
  479. while (xsect)
  480. {
  481. char *subname = xsect + 1;
  482. xsect = strchr (subname, '\\');
  483. if (xsect)
  484. *xsect = 0;
  485. ics[ind]->xsubs[ics[ind]->nxsubs] = fetch_insn_class (subname,1);
  486. ics[ind]->nxsubs++;
  487. }
  488. free (name);
  489. return ind;
  490. }
  491. /* For sorting a class's sub-class list only; make sure classes appear before
  492. terminals. */
  493. static int
  494. sub_compare (const void *e1, const void *e2)
  495. {
  496. struct iclass *ic1 = ics[*(int *)e1];
  497. struct iclass *ic2 = ics[*(int *)e2];
  498. if (ic1->is_class)
  499. {
  500. if (!ic2->is_class)
  501. return -1;
  502. }
  503. else if (ic2->is_class)
  504. return 1;
  505. return strcmp (ic1->name, ic2->name);
  506. }
  507. static void
  508. load_insn_classes (void)
  509. {
  510. FILE *fp = fopen ("ia64-ic.tbl", "r");
  511. char buf[2048];
  512. if (fp == NULL)
  513. fail (_("can't find ia64-ic.tbl for reading\n"));
  514. /* Discard first line. */
  515. fgets (buf, sizeof(buf), fp);
  516. while (!feof (fp))
  517. {
  518. int iclass;
  519. char *name;
  520. char *tmp;
  521. if (fgets (buf, sizeof (buf), fp) == NULL)
  522. break;
  523. while (ISSPACE (buf[strlen (buf) - 1]))
  524. buf[strlen (buf) - 1] = '\0';
  525. name = tmp = buf;
  526. while (*tmp != ';')
  527. {
  528. ++tmp;
  529. if (tmp == buf + sizeof (buf))
  530. abort ();
  531. }
  532. *tmp++ = '\0';
  533. iclass = fetch_insn_class (name, 1);
  534. ics[iclass]->is_class = 1;
  535. if (strcmp (name, "none") == 0)
  536. {
  537. ics[iclass]->is_class = 0;
  538. ics[iclass]->terminal_resolved = 1;
  539. continue;
  540. }
  541. /* For this class, record all sub-classes. */
  542. while (*tmp)
  543. {
  544. char *subname;
  545. int sub;
  546. while (*tmp && ISSPACE (*tmp))
  547. {
  548. ++tmp;
  549. if (tmp == buf + sizeof (buf))
  550. abort ();
  551. }
  552. subname = tmp;
  553. while (*tmp && *tmp != ',')
  554. {
  555. ++tmp;
  556. if (tmp == buf + sizeof (buf))
  557. abort ();
  558. }
  559. if (*tmp == ',')
  560. *tmp++ = '\0';
  561. ics[iclass]->subs = (int *)
  562. xrealloc ((void *)ics[iclass]->subs,
  563. (ics[iclass]->nsubs + 1) * sizeof (int));
  564. sub = fetch_insn_class (subname, 1);
  565. ics[iclass]->subs = (int *)
  566. xrealloc (ics[iclass]->subs, (ics[iclass]->nsubs + 1) * sizeof (int));
  567. ics[iclass]->subs[ics[iclass]->nsubs++] = sub;
  568. }
  569. /* Make sure classes come before terminals. */
  570. qsort ((void *)ics[iclass]->subs,
  571. ics[iclass]->nsubs, sizeof(int), sub_compare);
  572. }
  573. fclose (fp);
  574. if (debug)
  575. printf ("%d classes\n", iclen);
  576. }
  577. /* Extract the insn classes from the given line. */
  578. static void
  579. parse_resource_users (const char *ref, int **usersp, int *nusersp,
  580. int **notesp)
  581. {
  582. int c;
  583. char *line = xstrdup (ref);
  584. char *tmp = line;
  585. int *users = *usersp;
  586. int count = *nusersp;
  587. int *notes = *notesp;
  588. c = *tmp;
  589. while (c != 0)
  590. {
  591. char *notestr;
  592. int note;
  593. char *xsect;
  594. int iclass;
  595. int create = 0;
  596. char *name;
  597. while (ISSPACE (*tmp))
  598. ++tmp;
  599. name = tmp;
  600. while (*tmp && *tmp != ',')
  601. ++tmp;
  602. c = *tmp;
  603. *tmp++ = '\0';
  604. xsect = strchr (name, '\\');
  605. if ((notestr = strstr (name, "+")) != NULL)
  606. {
  607. char *nextnotestr;
  608. note = atoi (notestr + 1);
  609. if ((nextnotestr = strchr (notestr + 1, '+')) != NULL)
  610. {
  611. /* Note 13 always implies note 1. */
  612. if (strcmp (notestr, "+1+13") == 0)
  613. note = 13;
  614. else if (!xsect || nextnotestr < xsect)
  615. warn (_("multiple note %s not handled\n"), notestr);
  616. }
  617. if (!xsect)
  618. *notestr = '\0';
  619. }
  620. else
  621. note = 0;
  622. /* All classes are created when the insn class table is parsed;
  623. Individual instructions might not appear until the dependency tables
  624. are read. Only create new classes if it's *not* an insn class,
  625. or if it's a composite class (which wouldn't necessarily be in the IC
  626. table). */
  627. if (! startswith (name, "IC:") || xsect != NULL)
  628. create = 1;
  629. iclass = fetch_insn_class (name, create);
  630. if (iclass != -1)
  631. {
  632. users = (int *)
  633. xrealloc ((void *) users,(count + 1) * sizeof (int));
  634. notes = (int *)
  635. xrealloc ((void *) notes,(count + 1) * sizeof (int));
  636. notes[count] = note;
  637. users[count++] = iclass;
  638. mark_used (ics[iclass], 0);
  639. }
  640. else if (debug)
  641. printf("Class %s not found\n", name);
  642. }
  643. /* Update the return values. */
  644. *usersp = users;
  645. *nusersp = count;
  646. *notesp = notes;
  647. free (line);
  648. }
  649. static int
  650. parse_semantics (char *sem)
  651. {
  652. if (strcmp (sem, "none") == 0)
  653. return IA64_DVS_NONE;
  654. else if (strcmp (sem, "implied") == 0)
  655. return IA64_DVS_IMPLIED;
  656. else if (strcmp (sem, "impliedF") == 0)
  657. return IA64_DVS_IMPLIEDF;
  658. else if (strcmp (sem, "data") == 0)
  659. return IA64_DVS_DATA;
  660. else if (strcmp (sem, "instr") == 0)
  661. return IA64_DVS_INSTR;
  662. else if (strcmp (sem, "specific") == 0)
  663. return IA64_DVS_SPECIFIC;
  664. else if (strcmp (sem, "stop") == 0)
  665. return IA64_DVS_STOP;
  666. else
  667. return IA64_DVS_OTHER;
  668. }
  669. static void
  670. add_dep (const char *name, const char *chk, const char *reg,
  671. int semantics, int mode, char *extra, int flag)
  672. {
  673. struct rdep *rs;
  674. rs = insert_resource (name, mode);
  675. parse_resource_users (chk, &rs->chks, &rs->nchks, &rs->chknotes);
  676. parse_resource_users (reg, &rs->regs, &rs->nregs, &rs->regnotes);
  677. rs->semantics = semantics;
  678. rs->extra = extra;
  679. rs->waw_special = flag;
  680. }
  681. static void
  682. load_depfile (const char *filename, enum ia64_dependency_mode mode)
  683. {
  684. FILE *fp = fopen (filename, "r");
  685. char buf[1024];
  686. if (fp == NULL)
  687. fail (_("can't find %s for reading\n"), filename);
  688. fgets (buf, sizeof(buf), fp);
  689. while (!feof (fp))
  690. {
  691. char *name, *tmp;
  692. int semantics;
  693. char *extra;
  694. char *regp, *chkp;
  695. if (fgets (buf, sizeof(buf), fp) == NULL)
  696. break;
  697. while (ISSPACE (buf[strlen (buf) - 1]))
  698. buf[strlen (buf) - 1] = '\0';
  699. name = tmp = buf;
  700. while (*tmp != ';')
  701. ++tmp;
  702. *tmp++ = '\0';
  703. while (ISSPACE (*tmp))
  704. ++tmp;
  705. regp = tmp;
  706. tmp = strchr (tmp, ';');
  707. if (!tmp)
  708. abort ();
  709. *tmp++ = 0;
  710. while (ISSPACE (*tmp))
  711. ++tmp;
  712. chkp = tmp;
  713. tmp = strchr (tmp, ';');
  714. if (!tmp)
  715. abort ();
  716. *tmp++ = 0;
  717. while (ISSPACE (*tmp))
  718. ++tmp;
  719. semantics = parse_semantics (tmp);
  720. extra = semantics == IA64_DVS_OTHER ? xstrdup (tmp) : NULL;
  721. /* For WAW entries, if the chks and regs differ, we need to enter the
  722. entries in both positions so that the tables will be parsed properly,
  723. without a lot of extra work. */
  724. if (mode == IA64_DV_WAW && strcmp (regp, chkp) != 0)
  725. {
  726. add_dep (name, chkp, regp, semantics, mode, extra, 0);
  727. add_dep (name, regp, chkp, semantics, mode, extra, 1);
  728. }
  729. else
  730. {
  731. add_dep (name, chkp, regp, semantics, mode, extra, 0);
  732. }
  733. }
  734. fclose (fp);
  735. }
  736. static void
  737. load_dependencies (void)
  738. {
  739. load_depfile ("ia64-raw.tbl", IA64_DV_RAW);
  740. load_depfile ("ia64-waw.tbl", IA64_DV_WAW);
  741. load_depfile ("ia64-war.tbl", IA64_DV_WAR);
  742. if (debug)
  743. printf ("%d RAW/WAW/WAR dependencies\n", rdepslen);
  744. }
  745. /* Is the given operand an indirect register file operand? */
  746. static int
  747. irf_operand (int op, const char *field)
  748. {
  749. if (!field)
  750. {
  751. return op == IA64_OPND_RR_R3 || op == IA64_OPND_DBR_R3
  752. || op == IA64_OPND_IBR_R3 || op == IA64_OPND_PKR_R3
  753. || op == IA64_OPND_PMC_R3 || op == IA64_OPND_PMD_R3
  754. || op == IA64_OPND_MSR_R3 || op == IA64_OPND_CPUID_R3;
  755. }
  756. else
  757. {
  758. return ((op == IA64_OPND_RR_R3 && strstr (field, "rr"))
  759. || (op == IA64_OPND_DBR_R3 && strstr (field, "dbr"))
  760. || (op == IA64_OPND_IBR_R3 && strstr (field, "ibr"))
  761. || (op == IA64_OPND_PKR_R3 && strstr (field, "pkr"))
  762. || (op == IA64_OPND_PMC_R3 && strstr (field, "pmc"))
  763. || (op == IA64_OPND_PMD_R3 && strstr (field, "pmd"))
  764. || (op == IA64_OPND_MSR_R3 && strstr (field, "msr"))
  765. || (op == IA64_OPND_CPUID_R3 && strstr (field, "cpuid"))
  766. || (op == IA64_OPND_DAHR_R3 && strstr (field, "dahr")));
  767. }
  768. }
  769. /* Handle mov_ar, mov_br, mov_cr, move_dahr, mov_indirect, mov_ip, mov_pr,
  770. * mov_psr, and mov_um insn classes. */
  771. static int
  772. in_iclass_mov_x (struct ia64_opcode *idesc, struct iclass *ic,
  773. const char *format, const char *field)
  774. {
  775. int plain_mov = strcmp (idesc->name, "mov") == 0;
  776. if (!format)
  777. return 0;
  778. switch (ic->name[4])
  779. {
  780. default:
  781. abort ();
  782. case 'a':
  783. {
  784. int i = strcmp (idesc->name, "mov.i") == 0;
  785. int m = strcmp (idesc->name, "mov.m") == 0;
  786. int i2627 = i && idesc->operands[0] == IA64_OPND_AR3;
  787. int i28 = i && idesc->operands[1] == IA64_OPND_AR3;
  788. int m2930 = m && idesc->operands[0] == IA64_OPND_AR3;
  789. int m31 = m && idesc->operands[1] == IA64_OPND_AR3;
  790. int pseudo0 = plain_mov && idesc->operands[1] == IA64_OPND_AR3;
  791. int pseudo1 = plain_mov && idesc->operands[0] == IA64_OPND_AR3;
  792. /* IC:mov ar */
  793. if (i2627)
  794. return strstr (format, "I26") || strstr (format, "I27");
  795. if (i28)
  796. return strstr (format, "I28") != NULL;
  797. if (m2930)
  798. return strstr (format, "M29") || strstr (format, "M30");
  799. if (m31)
  800. return strstr (format, "M31") != NULL;
  801. if (pseudo0 || pseudo1)
  802. return 1;
  803. }
  804. break;
  805. case 'b':
  806. {
  807. int i21 = idesc->operands[0] == IA64_OPND_B1;
  808. int i22 = plain_mov && idesc->operands[1] == IA64_OPND_B2;
  809. if (i22)
  810. return strstr (format, "I22") != NULL;
  811. if (i21)
  812. return strstr (format, "I21") != NULL;
  813. }
  814. break;
  815. case 'c':
  816. {
  817. int m32 = plain_mov && idesc->operands[0] == IA64_OPND_CR3;
  818. int m33 = plain_mov && idesc->operands[1] == IA64_OPND_CR3;
  819. if (m32)
  820. return strstr (format, "M32") != NULL;
  821. if (m33)
  822. return strstr (format, "M33") != NULL;
  823. }
  824. break;
  825. case 'd':
  826. {
  827. int m50 = plain_mov && idesc->operands[0] == IA64_OPND_DAHR3;
  828. if (m50)
  829. return strstr (format, "M50") != NULL;
  830. }
  831. break;
  832. case 'i':
  833. if (ic->name[5] == 'n')
  834. {
  835. int m42 = plain_mov && irf_operand (idesc->operands[0], field);
  836. int m43 = plain_mov && irf_operand (idesc->operands[1], field);
  837. if (m42)
  838. return strstr (format, "M42") != NULL;
  839. if (m43)
  840. return strstr (format, "M43") != NULL;
  841. }
  842. else if (ic->name[5] == 'p')
  843. {
  844. return idesc->operands[1] == IA64_OPND_IP;
  845. }
  846. else
  847. abort ();
  848. break;
  849. case 'p':
  850. if (ic->name[5] == 'r')
  851. {
  852. int i25 = plain_mov && idesc->operands[1] == IA64_OPND_PR;
  853. int i23 = plain_mov && idesc->operands[0] == IA64_OPND_PR;
  854. int i24 = plain_mov && idesc->operands[0] == IA64_OPND_PR_ROT;
  855. if (i23)
  856. return strstr (format, "I23") != NULL;
  857. if (i24)
  858. return strstr (format, "I24") != NULL;
  859. if (i25)
  860. return strstr (format, "I25") != NULL;
  861. }
  862. else if (ic->name[5] == 's')
  863. {
  864. int m35 = plain_mov && idesc->operands[0] == IA64_OPND_PSR_L;
  865. int m36 = plain_mov && idesc->operands[1] == IA64_OPND_PSR;
  866. if (m35)
  867. return strstr (format, "M35") != NULL;
  868. if (m36)
  869. return strstr (format, "M36") != NULL;
  870. }
  871. else
  872. abort ();
  873. break;
  874. case 'u':
  875. {
  876. int m35 = plain_mov && idesc->operands[0] == IA64_OPND_PSR_UM;
  877. int m36 = plain_mov && idesc->operands[1] == IA64_OPND_PSR_UM;
  878. if (m35)
  879. return strstr (format, "M35") != NULL;
  880. if (m36)
  881. return strstr (format, "M36") != NULL;
  882. }
  883. break;
  884. }
  885. return 0;
  886. }
  887. /* Is the given opcode in the given insn class? */
  888. static int
  889. in_iclass (struct ia64_opcode *idesc, struct iclass *ic,
  890. const char *format, const char *field, int *notep)
  891. {
  892. int i;
  893. int resolved = 0;
  894. if (ic->comment)
  895. {
  896. if (startswith (ic->comment, "Format"))
  897. {
  898. /* Assume that the first format seen is the most restrictive, and
  899. only keep a later one if it looks like it's more restrictive. */
  900. if (format)
  901. {
  902. if (strlen (ic->comment) < strlen (format))
  903. {
  904. warn (_("most recent format '%s'\nappears more restrictive than '%s'\n"),
  905. ic->comment, format);
  906. format = ic->comment;
  907. }
  908. }
  909. else
  910. format = ic->comment;
  911. }
  912. else if (startswith (ic->comment, "Field"))
  913. {
  914. if (field)
  915. warn (_("overlapping field %s->%s\n"),
  916. ic->comment, field);
  917. field = ic->comment;
  918. }
  919. }
  920. /* An insn class matches anything that is the same followed by completers,
  921. except when the absence and presence of completers constitutes different
  922. instructions. */
  923. if (ic->nsubs == 0 && ic->nxsubs == 0)
  924. {
  925. int is_mov = startswith (idesc->name, "mov");
  926. int plain_mov = strcmp (idesc->name, "mov") == 0;
  927. int len = strlen(ic->name);
  928. resolved = ((strncmp (ic->name, idesc->name, len) == 0)
  929. && (idesc->name[len] == '\0'
  930. || idesc->name[len] == '.'));
  931. /* All break, nop, and hint variations must match exactly. */
  932. if (resolved &&
  933. (strcmp (ic->name, "break") == 0
  934. || strcmp (ic->name, "nop") == 0
  935. || strcmp (ic->name, "hint") == 0))
  936. resolved = strcmp (ic->name, idesc->name) == 0;
  937. /* Assume restrictions in the FORMAT/FIELD negate resolution,
  938. unless specifically allowed by clauses in this block. */
  939. if (resolved && field)
  940. {
  941. /* Check Field(sf)==sN against opcode sN. */
  942. if (strstr(field, "(sf)==") != NULL)
  943. {
  944. char *sf;
  945. if ((sf = strstr (idesc->name, ".s")) != 0)
  946. resolved = strcmp (sf + 1, strstr (field, "==") + 2) == 0;
  947. }
  948. /* Check Field(lftype)==XXX. */
  949. else if (strstr (field, "(lftype)") != NULL)
  950. {
  951. if (strstr (idesc->name, "fault") != NULL)
  952. resolved = strstr (field, "fault") != NULL;
  953. else
  954. resolved = strstr (field, "fault") == NULL;
  955. }
  956. /* Handle Field(ctype)==XXX. */
  957. else if (strstr (field, "(ctype)") != NULL)
  958. {
  959. if (strstr (idesc->name, "or.andcm"))
  960. resolved = strstr (field, "or.andcm") != NULL;
  961. else if (strstr (idesc->name, "and.orcm"))
  962. resolved = strstr (field, "and.orcm") != NULL;
  963. else if (strstr (idesc->name, "orcm"))
  964. resolved = strstr (field, "or orcm") != NULL;
  965. else if (strstr (idesc->name, "or"))
  966. resolved = strstr (field, "or orcm") != NULL;
  967. else if (strstr (idesc->name, "andcm"))
  968. resolved = strstr (field, "and andcm") != NULL;
  969. else if (strstr (idesc->name, "and"))
  970. resolved = strstr (field, "and andcm") != NULL;
  971. else if (strstr (idesc->name, "unc"))
  972. resolved = strstr (field, "unc") != NULL;
  973. else
  974. resolved = strcmp (field, "Field(ctype)==") == 0;
  975. }
  976. }
  977. if (resolved && format)
  978. {
  979. if (startswith (idesc->name, "dep")
  980. && strstr (format, "I13") != NULL)
  981. resolved = idesc->operands[1] == IA64_OPND_IMM8;
  982. else if (startswith (idesc->name, "chk")
  983. && strstr (format, "M21") != NULL)
  984. resolved = idesc->operands[0] == IA64_OPND_F2;
  985. else if (startswith (idesc->name, "lfetch"))
  986. resolved = (strstr (format, "M14 M15") != NULL
  987. && (idesc->operands[1] == IA64_OPND_R2
  988. || idesc->operands[1] == IA64_OPND_IMM9b));
  989. else if (startswith (idesc->name, "br.call")
  990. && strstr (format, "B5") != NULL)
  991. resolved = idesc->operands[1] == IA64_OPND_B2;
  992. else if (startswith (idesc->name, "br.call")
  993. && strstr (format, "B3") != NULL)
  994. resolved = idesc->operands[1] == IA64_OPND_TGT25c;
  995. else if (startswith (idesc->name, "brp")
  996. && strstr (format, "B7") != NULL)
  997. resolved = idesc->operands[0] == IA64_OPND_B2;
  998. else if (strcmp (ic->name, "invala") == 0)
  999. resolved = strcmp (idesc->name, ic->name) == 0;
  1000. else if (startswith (idesc->name, "st")
  1001. && (strstr (format, "M5") != NULL
  1002. || strstr (format, "M10") != NULL))
  1003. resolved = idesc->flags & IA64_OPCODE_POSTINC;
  1004. else if (startswith (idesc->name, "ld")
  1005. && (strstr (format, "M2 M3") != NULL
  1006. || strstr (format, "M12") != NULL
  1007. || strstr (format, "M7 M8") != NULL))
  1008. resolved = idesc->flags & IA64_OPCODE_POSTINC;
  1009. else
  1010. resolved = 0;
  1011. }
  1012. /* Misc brl variations ('.cond' is optional);
  1013. plain brl matches brl.cond. */
  1014. if (!resolved
  1015. && (strcmp (idesc->name, "brl") == 0
  1016. || startswith (idesc->name, "brl."))
  1017. && strcmp (ic->name, "brl.cond") == 0)
  1018. {
  1019. resolved = 1;
  1020. }
  1021. /* Misc br variations ('.cond' is optional). */
  1022. if (!resolved
  1023. && (strcmp (idesc->name, "br") == 0
  1024. || startswith (idesc->name, "br."))
  1025. && strcmp (ic->name, "br.cond") == 0)
  1026. {
  1027. if (format)
  1028. resolved = (strstr (format, "B4") != NULL
  1029. && idesc->operands[0] == IA64_OPND_B2)
  1030. || (strstr (format, "B1") != NULL
  1031. && idesc->operands[0] == IA64_OPND_TGT25c);
  1032. else
  1033. resolved = 1;
  1034. }
  1035. /* probe variations. */
  1036. if (!resolved && startswith (idesc->name, "probe"))
  1037. {
  1038. resolved = strcmp (ic->name, "probe") == 0
  1039. && !((strstr (idesc->name, "fault") != NULL)
  1040. ^ (format && strstr (format, "M40") != NULL));
  1041. }
  1042. /* mov variations. */
  1043. if (!resolved && is_mov)
  1044. {
  1045. if (plain_mov)
  1046. {
  1047. /* mov alias for fmerge. */
  1048. if (strcmp (ic->name, "fmerge") == 0)
  1049. {
  1050. resolved = idesc->operands[0] == IA64_OPND_F1
  1051. && idesc->operands[1] == IA64_OPND_F3;
  1052. }
  1053. /* mov alias for adds (r3 or imm14). */
  1054. else if (strcmp (ic->name, "adds") == 0)
  1055. {
  1056. resolved = (idesc->operands[0] == IA64_OPND_R1
  1057. && (idesc->operands[1] == IA64_OPND_R3
  1058. || (idesc->operands[1] == IA64_OPND_IMM14)));
  1059. }
  1060. /* mov alias for addl. */
  1061. else if (strcmp (ic->name, "addl") == 0)
  1062. {
  1063. resolved = idesc->operands[0] == IA64_OPND_R1
  1064. && idesc->operands[1] == IA64_OPND_IMM22;
  1065. }
  1066. }
  1067. /* Some variants of mov and mov.[im]. */
  1068. if (!resolved && startswith (ic->name, "mov_"))
  1069. resolved = in_iclass_mov_x (idesc, ic, format, field);
  1070. }
  1071. /* Keep track of this so we can flag any insn classes which aren't
  1072. mapped onto at least one real insn. */
  1073. if (resolved)
  1074. ic->terminal_resolved = 1;
  1075. }
  1076. else for (i = 0; i < ic->nsubs; i++)
  1077. {
  1078. if (in_iclass (idesc, ics[ic->subs[i]], format, field, notep))
  1079. {
  1080. int j;
  1081. for (j = 0; j < ic->nxsubs; j++)
  1082. if (in_iclass (idesc, ics[ic->xsubs[j]], NULL, NULL, NULL))
  1083. return 0;
  1084. if (debug > 1)
  1085. printf ("%s is in IC %s\n", idesc->name, ic->name);
  1086. resolved = 1;
  1087. break;
  1088. }
  1089. }
  1090. /* If it's in this IC, add the IC note (if any) to the insn. */
  1091. if (resolved)
  1092. {
  1093. if (ic->note && notep)
  1094. {
  1095. if (*notep && *notep != ic->note)
  1096. warn (_("overwriting note %d with note %d (IC:%s)\n"),
  1097. *notep, ic->note, ic->name);
  1098. *notep = ic->note;
  1099. }
  1100. }
  1101. return resolved;
  1102. }
  1103. static int
  1104. lookup_regindex (const char *name, int specifier)
  1105. {
  1106. switch (specifier)
  1107. {
  1108. case IA64_RS_ARX:
  1109. if (strstr (name, "[RSC]"))
  1110. return 16;
  1111. if (strstr (name, "[BSP]"))
  1112. return 17;
  1113. else if (strstr (name, "[BSPSTORE]"))
  1114. return 18;
  1115. else if (strstr (name, "[RNAT]"))
  1116. return 19;
  1117. else if (strstr (name, "[FCR]"))
  1118. return 21;
  1119. else if (strstr (name, "[EFLAG]"))
  1120. return 24;
  1121. else if (strstr (name, "[CSD]"))
  1122. return 25;
  1123. else if (strstr (name, "[SSD]"))
  1124. return 26;
  1125. else if (strstr (name, "[CFLG]"))
  1126. return 27;
  1127. else if (strstr (name, "[FSR]"))
  1128. return 28;
  1129. else if (strstr (name, "[FIR]"))
  1130. return 29;
  1131. else if (strstr (name, "[FDR]"))
  1132. return 30;
  1133. else if (strstr (name, "[CCV]"))
  1134. return 32;
  1135. else if (strstr (name, "[ITC]"))
  1136. return 44;
  1137. else if (strstr (name, "[RUC]"))
  1138. return 45;
  1139. else if (strstr (name, "[PFS]"))
  1140. return 64;
  1141. else if (strstr (name, "[LC]"))
  1142. return 65;
  1143. else if (strstr (name, "[EC]"))
  1144. return 66;
  1145. abort ();
  1146. case IA64_RS_CRX:
  1147. if (strstr (name, "[DCR]"))
  1148. return 0;
  1149. else if (strstr (name, "[ITM]"))
  1150. return 1;
  1151. else if (strstr (name, "[IVA]"))
  1152. return 2;
  1153. else if (strstr (name, "[PTA]"))
  1154. return 8;
  1155. else if (strstr (name, "[GPTA]"))
  1156. return 9;
  1157. else if (strstr (name, "[IPSR]"))
  1158. return 16;
  1159. else if (strstr (name, "[ISR]"))
  1160. return 17;
  1161. else if (strstr (name, "[IIP]"))
  1162. return 19;
  1163. else if (strstr (name, "[IFA]"))
  1164. return 20;
  1165. else if (strstr (name, "[ITIR]"))
  1166. return 21;
  1167. else if (strstr (name, "[IIPA]"))
  1168. return 22;
  1169. else if (strstr (name, "[IFS]"))
  1170. return 23;
  1171. else if (strstr (name, "[IIM]"))
  1172. return 24;
  1173. else if (strstr (name, "[IHA]"))
  1174. return 25;
  1175. else if (strstr (name, "[LID]"))
  1176. return 64;
  1177. else if (strstr (name, "[IVR]"))
  1178. return 65;
  1179. else if (strstr (name, "[TPR]"))
  1180. return 66;
  1181. else if (strstr (name, "[EOI]"))
  1182. return 67;
  1183. else if (strstr (name, "[ITV]"))
  1184. return 72;
  1185. else if (strstr (name, "[PMV]"))
  1186. return 73;
  1187. else if (strstr (name, "[CMCV]"))
  1188. return 74;
  1189. abort ();
  1190. case IA64_RS_PSR:
  1191. if (strstr (name, ".be"))
  1192. return 1;
  1193. else if (strstr (name, ".up"))
  1194. return 2;
  1195. else if (strstr (name, ".ac"))
  1196. return 3;
  1197. else if (strstr (name, ".mfl"))
  1198. return 4;
  1199. else if (strstr (name, ".mfh"))
  1200. return 5;
  1201. else if (strstr (name, ".ic"))
  1202. return 13;
  1203. else if (strstr (name, ".i"))
  1204. return 14;
  1205. else if (strstr (name, ".pk"))
  1206. return 15;
  1207. else if (strstr (name, ".dt"))
  1208. return 17;
  1209. else if (strstr (name, ".dfl"))
  1210. return 18;
  1211. else if (strstr (name, ".dfh"))
  1212. return 19;
  1213. else if (strstr (name, ".sp"))
  1214. return 20;
  1215. else if (strstr (name, ".pp"))
  1216. return 21;
  1217. else if (strstr (name, ".di"))
  1218. return 22;
  1219. else if (strstr (name, ".si"))
  1220. return 23;
  1221. else if (strstr (name, ".db"))
  1222. return 24;
  1223. else if (strstr (name, ".lp"))
  1224. return 25;
  1225. else if (strstr (name, ".tb"))
  1226. return 26;
  1227. else if (strstr (name, ".rt"))
  1228. return 27;
  1229. else if (strstr (name, ".cpl"))
  1230. return 32;
  1231. else if (strstr (name, ".rs"))
  1232. return 34;
  1233. else if (strstr (name, ".mc"))
  1234. return 35;
  1235. else if (strstr (name, ".it"))
  1236. return 36;
  1237. else if (strstr (name, ".id"))
  1238. return 37;
  1239. else if (strstr (name, ".da"))
  1240. return 38;
  1241. else if (strstr (name, ".dd"))
  1242. return 39;
  1243. else if (strstr (name, ".ss"))
  1244. return 40;
  1245. else if (strstr (name, ".ri"))
  1246. return 41;
  1247. else if (strstr (name, ".ed"))
  1248. return 43;
  1249. else if (strstr (name, ".bn"))
  1250. return 44;
  1251. else if (strstr (name, ".ia"))
  1252. return 45;
  1253. else if (strstr (name, ".vm"))
  1254. return 46;
  1255. else
  1256. abort ();
  1257. default:
  1258. break;
  1259. }
  1260. return REG_NONE;
  1261. }
  1262. static int
  1263. lookup_specifier (const char *name)
  1264. {
  1265. if (strchr (name, '%'))
  1266. {
  1267. if (strstr (name, "AR[K%]") != NULL)
  1268. return IA64_RS_AR_K;
  1269. if (strstr (name, "AR[UNAT]") != NULL)
  1270. return IA64_RS_AR_UNAT;
  1271. if (strstr (name, "AR%, % in 8") != NULL)
  1272. return IA64_RS_AR;
  1273. if (strstr (name, "AR%, % in 48") != NULL)
  1274. return IA64_RS_ARb;
  1275. if (strstr (name, "BR%") != NULL)
  1276. return IA64_RS_BR;
  1277. if (strstr (name, "CR[IIB%]") != NULL)
  1278. return IA64_RS_CR_IIB;
  1279. if (strstr (name, "CR[IRR%]") != NULL)
  1280. return IA64_RS_CR_IRR;
  1281. if (strstr (name, "CR[LRR%]") != NULL)
  1282. return IA64_RS_CR_LRR;
  1283. if (strstr (name, "CR%") != NULL)
  1284. return IA64_RS_CR;
  1285. if (strstr (name, "DAHR%, % in 0") != NULL)
  1286. return IA64_RS_DAHR;
  1287. if (strstr (name, "FR%, % in 0") != NULL)
  1288. return IA64_RS_FR;
  1289. if (strstr (name, "FR%, % in 2") != NULL)
  1290. return IA64_RS_FRb;
  1291. if (strstr (name, "GR%") != NULL)
  1292. return IA64_RS_GR;
  1293. if (strstr (name, "PR%, % in 1 ") != NULL)
  1294. return IA64_RS_PR;
  1295. if (strstr (name, "PR%, % in 16 ") != NULL)
  1296. return IA64_RS_PRr;
  1297. warn (_("don't know how to specify %% dependency %s\n"),
  1298. name);
  1299. }
  1300. else if (strchr (name, '#'))
  1301. {
  1302. if (strstr (name, "CPUID#") != NULL)
  1303. return IA64_RS_CPUID;
  1304. if (strstr (name, "DBR#") != NULL)
  1305. return IA64_RS_DBR;
  1306. if (strstr (name, "IBR#") != NULL)
  1307. return IA64_RS_IBR;
  1308. if (strstr (name, "MSR#") != NULL)
  1309. return IA64_RS_MSR;
  1310. if (strstr (name, "PKR#") != NULL)
  1311. return IA64_RS_PKR;
  1312. if (strstr (name, "PMC#") != NULL)
  1313. return IA64_RS_PMC;
  1314. if (strstr (name, "PMD#") != NULL)
  1315. return IA64_RS_PMD;
  1316. if (strstr (name, "RR#") != NULL)
  1317. return IA64_RS_RR;
  1318. warn (_("Don't know how to specify # dependency %s\n"),
  1319. name);
  1320. }
  1321. else if (startswith (name, "AR[FPSR]"))
  1322. return IA64_RS_AR_FPSR;
  1323. else if (startswith (name, "AR["))
  1324. return IA64_RS_ARX;
  1325. else if (startswith (name, "CR["))
  1326. return IA64_RS_CRX;
  1327. else if (startswith (name, "PSR."))
  1328. return IA64_RS_PSR;
  1329. else if (strcmp (name, "InService*") == 0)
  1330. return IA64_RS_INSERVICE;
  1331. else if (strcmp (name, "GR0") == 0)
  1332. return IA64_RS_GR0;
  1333. else if (strcmp (name, "CFM") == 0)
  1334. return IA64_RS_CFM;
  1335. else if (strcmp (name, "PR63") == 0)
  1336. return IA64_RS_PR63;
  1337. else if (strcmp (name, "RSE") == 0)
  1338. return IA64_RS_RSE;
  1339. return IA64_RS_ANY;
  1340. }
  1341. static void
  1342. print_dependency_table (void)
  1343. {
  1344. int i, j;
  1345. if (debug)
  1346. {
  1347. for (i=0;i < iclen;i++)
  1348. {
  1349. if (ics[i]->is_class)
  1350. {
  1351. if (!ics[i]->nsubs)
  1352. {
  1353. if (ics[i]->comment)
  1354. warn (_("IC:%s [%s] has no terminals or sub-classes\n"),
  1355. ics[i]->name, ics[i]->comment);
  1356. else
  1357. warn (_("IC:%s has no terminals or sub-classes\n"),
  1358. ics[i]->name);
  1359. }
  1360. }
  1361. else
  1362. {
  1363. if (!ics[i]->terminal_resolved && !ics[i]->orphan)
  1364. {
  1365. if (ics[i]->comment)
  1366. warn (_("no insns mapped directly to terminal IC %s [%s]"),
  1367. ics[i]->name, ics[i]->comment);
  1368. else
  1369. warn (_("no insns mapped directly to terminal IC %s\n"),
  1370. ics[i]->name);
  1371. }
  1372. }
  1373. }
  1374. for (i = 0; i < iclen; i++)
  1375. {
  1376. if (ics[i]->orphan)
  1377. {
  1378. mark_used (ics[i], 1);
  1379. warn (_("class %s is defined but not used\n"),
  1380. ics[i]->name);
  1381. }
  1382. }
  1383. if (debug > 1)
  1384. for (i = 0; i < rdepslen; i++)
  1385. {
  1386. static const char *mode_str[] = { "RAW", "WAW", "WAR" };
  1387. if (rdeps[i]->total_chks == 0)
  1388. {
  1389. if (rdeps[i]->total_regs)
  1390. warn (_("Warning: rsrc %s (%s) has no chks\n"),
  1391. rdeps[i]->name, mode_str[rdeps[i]->mode]);
  1392. else
  1393. warn (_("Warning: rsrc %s (%s) has no chks or regs\n"),
  1394. rdeps[i]->name, mode_str[rdeps[i]->mode]);
  1395. }
  1396. else if (rdeps[i]->total_regs == 0)
  1397. warn (_("rsrc %s (%s) has no regs\n"),
  1398. rdeps[i]->name, mode_str[rdeps[i]->mode]);
  1399. }
  1400. }
  1401. /* The dependencies themselves. */
  1402. printf ("static const struct ia64_dependency\ndependencies[] = {\n");
  1403. for (i = 0; i < rdepslen; i++)
  1404. {
  1405. /* '%', '#', AR[], CR[], or PSR. indicates we need to specify the actual
  1406. resource used. */
  1407. int specifier = lookup_specifier (rdeps[i]->name);
  1408. int regindex = lookup_regindex (rdeps[i]->name, specifier);
  1409. printf (" { \"%s\", %d, %d, %d, %d, ",
  1410. rdeps[i]->name, specifier,
  1411. (int)rdeps[i]->mode, (int)rdeps[i]->semantics, regindex);
  1412. if (rdeps[i]->semantics == IA64_DVS_OTHER)
  1413. {
  1414. const char *quote, *rest;
  1415. putchar ('\"');
  1416. rest = rdeps[i]->extra;
  1417. quote = strchr (rest, '\"');
  1418. while (quote != NULL)
  1419. {
  1420. printf ("%.*s\\\"", (int) (quote - rest), rest);
  1421. rest = quote + 1;
  1422. quote = strchr (rest, '\"');
  1423. }
  1424. printf ("%s\", ", rest);
  1425. }
  1426. else
  1427. printf ("NULL, ");
  1428. printf("},\n");
  1429. }
  1430. printf ("};\n\n");
  1431. /* And dependency lists. */
  1432. for (i=0;i < dlistlen;i++)
  1433. {
  1434. unsigned int len = (unsigned) -1;
  1435. printf ("static const unsigned short dep%d[] = {", i);
  1436. for (j=0;j < dlists[i]->len; j++)
  1437. {
  1438. if (len > 74)
  1439. {
  1440. printf("\n ");
  1441. len = 1;
  1442. }
  1443. len += printf (" %d,", dlists[i]->deps[j]);
  1444. }
  1445. printf ("\n};\n\n");
  1446. }
  1447. /* And opcode dependency list. */
  1448. printf ("#define NELS(X) (sizeof(X)/sizeof(X[0]))\n");
  1449. printf ("static const struct ia64_opcode_dependency\n");
  1450. printf ("op_dependencies[] = {\n");
  1451. for (i = 0; i < opdeplen; i++)
  1452. {
  1453. printf (" { ");
  1454. if (opdeps[i]->chk == -1)
  1455. printf ("0, NULL, ");
  1456. else
  1457. printf ("NELS(dep%d), dep%d, ", opdeps[i]->chk, opdeps[i]->chk);
  1458. if (opdeps[i]->reg == -1)
  1459. printf ("0, NULL, ");
  1460. else
  1461. printf ("NELS(dep%d), dep%d, ", opdeps[i]->reg, opdeps[i]->reg);
  1462. printf ("},\n");
  1463. }
  1464. printf ("};\n\n");
  1465. }
  1466. /* Add STR to the string table. */
  1467. static struct string_entry *
  1468. insert_string (char *str)
  1469. {
  1470. int start = 0, end = strtablen;
  1471. int i, x;
  1472. if (strtablen == strtabtotlen)
  1473. {
  1474. strtabtotlen += 20;
  1475. string_table = (struct string_entry **)
  1476. xrealloc (string_table,
  1477. sizeof (struct string_entry **) * strtabtotlen);
  1478. }
  1479. if (strtablen == 0)
  1480. {
  1481. strtablen = 1;
  1482. string_table[0] = tmalloc (struct string_entry);
  1483. string_table[0]->s = xstrdup (str);
  1484. string_table[0]->num = 0;
  1485. return string_table[0];
  1486. }
  1487. if (strcmp (str, string_table[strtablen - 1]->s) > 0)
  1488. i = end;
  1489. else if (strcmp (str, string_table[0]->s) < 0)
  1490. i = 0;
  1491. else
  1492. {
  1493. while (1)
  1494. {
  1495. int c;
  1496. i = (start + end) / 2;
  1497. c = strcmp (str, string_table[i]->s);
  1498. if (c < 0)
  1499. end = i - 1;
  1500. else if (c == 0)
  1501. return string_table[i];
  1502. else
  1503. start = i + 1;
  1504. if (start > end)
  1505. break;
  1506. }
  1507. }
  1508. for (; i > 0 && i < strtablen; i--)
  1509. if (strcmp (str, string_table[i - 1]->s) > 0)
  1510. break;
  1511. for (; i < strtablen; i++)
  1512. if (strcmp (str, string_table[i]->s) < 0)
  1513. break;
  1514. for (x = strtablen - 1; x >= i; x--)
  1515. {
  1516. string_table[x + 1] = string_table[x];
  1517. string_table[x + 1]->num = x + 1;
  1518. }
  1519. string_table[i] = tmalloc (struct string_entry);
  1520. string_table[i]->s = xstrdup (str);
  1521. string_table[i]->num = i;
  1522. strtablen++;
  1523. return string_table[i];
  1524. }
  1525. static struct bittree *
  1526. make_bittree_entry (void)
  1527. {
  1528. struct bittree *res = tmalloc (struct bittree);
  1529. res->disent = NULL;
  1530. res->bits[0] = NULL;
  1531. res->bits[1] = NULL;
  1532. res->bits[2] = NULL;
  1533. res->skip_flag = 0;
  1534. res->bits_to_skip = 0;
  1535. return res;
  1536. }
  1537. static struct disent *
  1538. add_dis_table_ent (struct disent *which, int insn, int order,
  1539. ci_t completer_index)
  1540. {
  1541. int ci = 0;
  1542. struct disent *ent;
  1543. if (which != NULL)
  1544. {
  1545. ent = which;
  1546. ent->nextcnt++;
  1547. while (ent->nexte != NULL)
  1548. ent = ent->nexte;
  1549. ent = (ent->nexte = tmalloc (struct disent));
  1550. }
  1551. else
  1552. {
  1553. ent = tmalloc (struct disent);
  1554. ent->next_ent = disinsntable;
  1555. disinsntable = ent;
  1556. which = ent;
  1557. }
  1558. ent->nextcnt = 0;
  1559. ent->nexte = NULL;
  1560. ent->insn = insn;
  1561. ent->priority = order;
  1562. while (completer_index != 1)
  1563. {
  1564. ci = (ci << 1) | (completer_index & 1);
  1565. completer_index >>= 1;
  1566. }
  1567. ent->completer_index = ci;
  1568. return which;
  1569. }
  1570. static void
  1571. finish_distable (void)
  1572. {
  1573. struct disent *ent = disinsntable;
  1574. struct disent *prev = ent;
  1575. ent->ournum = 32768;
  1576. while ((ent = ent->next_ent) != NULL)
  1577. {
  1578. ent->ournum = prev->ournum + prev->nextcnt + 1;
  1579. prev = ent;
  1580. }
  1581. }
  1582. static void
  1583. insert_bit_table_ent (struct bittree *curr_ent, int bit, ia64_insn opcode,
  1584. ia64_insn mask, int opcodenum, int order,
  1585. ci_t completer_index)
  1586. {
  1587. ia64_insn m;
  1588. int b;
  1589. struct bittree *next;
  1590. if (bit == -1)
  1591. {
  1592. struct disent *nent = add_dis_table_ent (curr_ent->disent,
  1593. opcodenum, order,
  1594. completer_index);
  1595. curr_ent->disent = nent;
  1596. return;
  1597. }
  1598. m = ((ia64_insn) 1) << bit;
  1599. if (mask & m)
  1600. b = (opcode & m) ? 1 : 0;
  1601. else
  1602. b = 2;
  1603. next = curr_ent->bits[b];
  1604. if (next == NULL)
  1605. {
  1606. next = make_bittree_entry ();
  1607. curr_ent->bits[b] = next;
  1608. }
  1609. insert_bit_table_ent (next, bit - 1, opcode, mask, opcodenum, order,
  1610. completer_index);
  1611. }
  1612. static void
  1613. add_dis_entry (struct bittree *first, ia64_insn opcode, ia64_insn mask,
  1614. int opcodenum, struct completer_entry *ent, ci_t completer_index)
  1615. {
  1616. if (completer_index & ((ci_t)1 << 32) )
  1617. abort ();
  1618. while (ent != NULL)
  1619. {
  1620. ia64_insn newopcode = (opcode & (~ ent->mask)) | ent->bits;
  1621. add_dis_entry (first, newopcode, mask, opcodenum, ent->addl_entries,
  1622. (completer_index << 1) | 1);
  1623. if (ent->is_terminal)
  1624. {
  1625. insert_bit_table_ent (bittree, 40, newopcode, mask,
  1626. opcodenum, opcode_count - ent->order - 1,
  1627. (completer_index << 1) | 1);
  1628. }
  1629. completer_index <<= 1;
  1630. ent = ent->alternative;
  1631. }
  1632. }
  1633. /* This optimization pass combines multiple "don't care" nodes. */
  1634. static void
  1635. compact_distree (struct bittree *ent)
  1636. {
  1637. #define IS_SKIP(ent) \
  1638. ((ent->bits[2] !=NULL) \
  1639. && (ent->bits[0] == NULL && ent->bits[1] == NULL && ent->skip_flag == 0))
  1640. int bitcnt = 0;
  1641. struct bittree *nent = ent;
  1642. int x;
  1643. while (IS_SKIP (nent))
  1644. {
  1645. bitcnt++;
  1646. nent = nent->bits[2];
  1647. }
  1648. if (bitcnt)
  1649. {
  1650. struct bittree *next = ent->bits[2];
  1651. ent->bits[0] = nent->bits[0];
  1652. ent->bits[1] = nent->bits[1];
  1653. ent->bits[2] = nent->bits[2];
  1654. ent->disent = nent->disent;
  1655. ent->skip_flag = 1;
  1656. ent->bits_to_skip = bitcnt;
  1657. while (next != nent)
  1658. {
  1659. struct bittree *b = next;
  1660. next = next->bits[2];
  1661. free (b);
  1662. }
  1663. free (nent);
  1664. }
  1665. for (x = 0; x < 3; x++)
  1666. {
  1667. struct bittree *i = ent->bits[x];
  1668. if (i != NULL)
  1669. compact_distree (i);
  1670. }
  1671. }
  1672. static unsigned char *insn_list;
  1673. static int insn_list_len = 0;
  1674. static int tot_insn_list_len = 0;
  1675. /* Generate the disassembler state machine corresponding to the tree
  1676. in ENT. */
  1677. static void
  1678. gen_dis_table (struct bittree *ent)
  1679. {
  1680. int x;
  1681. int our_offset = insn_list_len;
  1682. int bitsused = 5;
  1683. int totbits = bitsused;
  1684. int needed_bytes;
  1685. int zero_count = 0;
  1686. int zero_dest = 0; /* Initialize this with 0 to keep gcc quiet... */
  1687. /* If this is a terminal entry, there's no point in skipping any
  1688. bits. */
  1689. if (ent->skip_flag && ent->bits[0] == NULL && ent->bits[1] == NULL &&
  1690. ent->bits[2] == NULL)
  1691. {
  1692. if (ent->disent == NULL)
  1693. abort ();
  1694. else
  1695. ent->skip_flag = 0;
  1696. }
  1697. /* Calculate the amount of space needed for this entry, or at least
  1698. a conservatively large approximation. */
  1699. if (ent->skip_flag)
  1700. totbits += 5;
  1701. for (x = 1; x < 3; x++)
  1702. if (ent->bits[x] != NULL)
  1703. totbits += 16;
  1704. if (ent->disent != NULL)
  1705. {
  1706. if (ent->bits[2] != NULL)
  1707. abort ();
  1708. totbits += 16;
  1709. }
  1710. /* Now allocate the space. */
  1711. needed_bytes = (totbits + 7) / 8;
  1712. if ((needed_bytes + insn_list_len) > tot_insn_list_len)
  1713. {
  1714. tot_insn_list_len += 256;
  1715. insn_list = (unsigned char *) xrealloc (insn_list, tot_insn_list_len);
  1716. }
  1717. our_offset = insn_list_len;
  1718. insn_list_len += needed_bytes;
  1719. memset (insn_list + our_offset, 0, needed_bytes);
  1720. /* Encode the skip entry by setting bit 6 set in the state op field,
  1721. and store the # of bits to skip immediately after. */
  1722. if (ent->skip_flag)
  1723. {
  1724. bitsused += 5;
  1725. insn_list[our_offset + 0] |= 0x40 | ((ent->bits_to_skip >> 2) & 0xf);
  1726. insn_list[our_offset + 1] |= ((ent->bits_to_skip & 3) << 6);
  1727. }
  1728. #define IS_ONLY_IFZERO(ENT) \
  1729. ((ENT)->bits[0] != NULL && (ENT)->bits[1] == NULL && (ENT)->bits[2] == NULL \
  1730. && (ENT)->disent == NULL && (ENT)->skip_flag == 0)
  1731. /* Store an "if (bit is zero)" instruction by setting bit 7 in the
  1732. state op field. */
  1733. if (ent->bits[0] != NULL)
  1734. {
  1735. struct bittree *nent = ent->bits[0];
  1736. zero_count = 0;
  1737. insn_list[our_offset] |= 0x80;
  1738. /* We can encode sequences of multiple "if (bit is zero)" tests
  1739. by storing the # of zero bits to check in the lower 3 bits of
  1740. the instruction. However, this only applies if the state
  1741. solely tests for a zero bit. */
  1742. if (IS_ONLY_IFZERO (ent))
  1743. {
  1744. while (IS_ONLY_IFZERO (nent) && zero_count < 7)
  1745. {
  1746. nent = nent->bits[0];
  1747. zero_count++;
  1748. }
  1749. insn_list[our_offset + 0] |= zero_count;
  1750. }
  1751. zero_dest = insn_list_len;
  1752. gen_dis_table (nent);
  1753. }
  1754. /* Now store the remaining tests. We also handle a sole "termination
  1755. entry" by storing it as an "any bit" test. */
  1756. for (x = 1; x < 3; x++)
  1757. {
  1758. if (ent->bits[x] != NULL || (x == 2 && ent->disent != NULL))
  1759. {
  1760. struct bittree *i = ent->bits[x];
  1761. int idest;
  1762. int currbits = 15;
  1763. if (i != NULL)
  1764. {
  1765. /* If the instruction being branched to only consists of
  1766. a termination entry, use the termination entry as the
  1767. place to branch to instead. */
  1768. if (i->bits[0] == NULL && i->bits[1] == NULL
  1769. && i->bits[2] == NULL && i->disent != NULL)
  1770. {
  1771. idest = i->disent->ournum;
  1772. i = NULL;
  1773. }
  1774. else
  1775. idest = insn_list_len - our_offset;
  1776. }
  1777. else
  1778. idest = ent->disent->ournum;
  1779. /* If the destination offset for the if (bit is 1) test is less
  1780. than 256 bytes away, we can store it as 8-bits instead of 16;
  1781. the instruction has bit 5 set for the 16-bit address, and bit
  1782. 4 for the 8-bit address. Since we've already allocated 16
  1783. bits for the address we need to deallocate the space.
  1784. Note that branchings within the table are relative, and
  1785. there are no branches that branch past our instruction yet
  1786. so we do not need to adjust any other offsets. */
  1787. if (x == 1)
  1788. {
  1789. if (idest <= 256)
  1790. {
  1791. int start = our_offset + bitsused / 8 + 1;
  1792. memmove (insn_list + start,
  1793. insn_list + start + 1,
  1794. insn_list_len - (start + 1));
  1795. currbits = 7;
  1796. totbits -= 8;
  1797. needed_bytes--;
  1798. insn_list_len--;
  1799. insn_list[our_offset] |= 0x10;
  1800. idest--;
  1801. }
  1802. else
  1803. insn_list[our_offset] |= 0x20;
  1804. }
  1805. else
  1806. {
  1807. /* An instruction which solely consists of a termination
  1808. marker and whose disassembly name index is < 4096
  1809. can be stored in 16 bits. The encoding is slightly
  1810. odd; the upper 4 bits of the instruction are 0x3, and
  1811. bit 3 loses its normal meaning. */
  1812. if (ent->bits[0] == NULL && ent->bits[1] == NULL
  1813. && ent->bits[2] == NULL && ent->skip_flag == 0
  1814. && ent->disent != NULL
  1815. && ent->disent->ournum < (32768 + 4096))
  1816. {
  1817. int start = our_offset + bitsused / 8 + 1;
  1818. memmove (insn_list + start,
  1819. insn_list + start + 1,
  1820. insn_list_len - (start + 1));
  1821. currbits = 11;
  1822. totbits -= 5;
  1823. bitsused--;
  1824. needed_bytes--;
  1825. insn_list_len--;
  1826. insn_list[our_offset] |= 0x30;
  1827. idest &= ~32768;
  1828. }
  1829. else
  1830. insn_list[our_offset] |= 0x08;
  1831. }
  1832. if (debug)
  1833. {
  1834. int id = idest;
  1835. if (i == NULL)
  1836. id |= 32768;
  1837. else if (! (id & 32768))
  1838. id += our_offset;
  1839. if (x == 1)
  1840. printf ("%d: if (1) goto %d\n", our_offset, id);
  1841. else
  1842. printf ("%d: try %d\n", our_offset, id);
  1843. }
  1844. /* Store the address of the entry being branched to. */
  1845. while (currbits >= 0)
  1846. {
  1847. unsigned char *byte = insn_list + our_offset + bitsused / 8;
  1848. if (idest & (1 << currbits))
  1849. *byte |= (1 << (7 - (bitsused % 8)));
  1850. bitsused++;
  1851. currbits--;
  1852. }
  1853. /* Now generate the states for the entry being branched to. */
  1854. if (i != NULL)
  1855. gen_dis_table (i);
  1856. }
  1857. }
  1858. if (debug)
  1859. {
  1860. if (ent->skip_flag)
  1861. printf ("%d: skipping %d\n", our_offset, ent->bits_to_skip);
  1862. if (ent->bits[0] != NULL)
  1863. printf ("%d: if (0:%d) goto %d\n", our_offset, zero_count + 1,
  1864. zero_dest);
  1865. }
  1866. if (bitsused != totbits)
  1867. abort ();
  1868. }
  1869. static void
  1870. print_dis_table (void)
  1871. {
  1872. int x;
  1873. struct disent *cent = disinsntable;
  1874. printf ("static const char dis_table[] = {");
  1875. for (x = 0; x < insn_list_len; x++)
  1876. {
  1877. if (x % 12 == 0)
  1878. printf ("\n ");
  1879. printf (" 0x%02x,", insn_list[x]);
  1880. }
  1881. printf ("\n};\n\n");
  1882. printf ("static const struct ia64_dis_names ia64_dis_names[] = {\n");
  1883. while (cent != NULL)
  1884. {
  1885. struct disent *ent = cent;
  1886. while (ent != NULL)
  1887. {
  1888. printf ("{ 0x%lx, %d, %d, %d },\n", ( long ) ent->completer_index,
  1889. ent->insn, (ent->nexte != NULL ? 1 : 0),
  1890. ent->priority);
  1891. ent = ent->nexte;
  1892. }
  1893. cent = cent->next_ent;
  1894. }
  1895. printf ("};\n\n");
  1896. }
  1897. static void
  1898. generate_disassembler (void)
  1899. {
  1900. int i;
  1901. bittree = make_bittree_entry ();
  1902. for (i = 0; i < otlen; i++)
  1903. {
  1904. struct main_entry *ptr = ordered_table[i];
  1905. if (ptr->opcode->type != IA64_TYPE_DYN)
  1906. add_dis_entry (bittree,
  1907. ptr->opcode->opcode, ptr->opcode->mask,
  1908. ptr->main_index,
  1909. ptr->completers, 1);
  1910. }
  1911. compact_distree (bittree);
  1912. finish_distable ();
  1913. gen_dis_table (bittree);
  1914. print_dis_table ();
  1915. }
  1916. static void
  1917. print_string_table (void)
  1918. {
  1919. int x;
  1920. char lbuf[80], buf[80];
  1921. int blen = 0;
  1922. printf ("static const char * const ia64_strings[] = {\n");
  1923. lbuf[0] = '\0';
  1924. for (x = 0; x < strtablen; x++)
  1925. {
  1926. int len;
  1927. if (strlen (string_table[x]->s) > 75)
  1928. abort ();
  1929. sprintf (buf, " \"%s\",", string_table[x]->s);
  1930. len = strlen (buf);
  1931. if ((blen + len) > 75)
  1932. {
  1933. printf (" %s\n", lbuf);
  1934. lbuf[0] = '\0';
  1935. blen = 0;
  1936. }
  1937. strcat (lbuf, buf);
  1938. blen += len;
  1939. }
  1940. if (blen > 0)
  1941. printf (" %s\n", lbuf);
  1942. printf ("};\n\n");
  1943. }
  1944. static struct completer_entry **glist;
  1945. static int glistlen = 0;
  1946. static int glisttotlen = 0;
  1947. /* If the completer trees ENT1 and ENT2 are equal, return 1. */
  1948. static int
  1949. completer_entries_eq (struct completer_entry *ent1,
  1950. struct completer_entry *ent2)
  1951. {
  1952. while (ent1 != NULL && ent2 != NULL)
  1953. {
  1954. if (ent1->name->num != ent2->name->num
  1955. || ent1->bits != ent2->bits
  1956. || ent1->mask != ent2->mask
  1957. || ent1->is_terminal != ent2->is_terminal
  1958. || ent1->dependencies != ent2->dependencies
  1959. || ent1->order != ent2->order)
  1960. return 0;
  1961. if (! completer_entries_eq (ent1->addl_entries, ent2->addl_entries))
  1962. return 0;
  1963. ent1 = ent1->alternative;
  1964. ent2 = ent2->alternative;
  1965. }
  1966. return ent1 == ent2;
  1967. }
  1968. /* Insert ENT into the global list of completers and return it. If an
  1969. equivalent entry (according to completer_entries_eq) already exists,
  1970. it is returned instead. */
  1971. static struct completer_entry *
  1972. insert_gclist (struct completer_entry *ent)
  1973. {
  1974. if (ent != NULL)
  1975. {
  1976. int i;
  1977. int x;
  1978. int start = 0, end;
  1979. ent->addl_entries = insert_gclist (ent->addl_entries);
  1980. ent->alternative = insert_gclist (ent->alternative);
  1981. i = glistlen / 2;
  1982. end = glistlen;
  1983. if (glisttotlen == glistlen)
  1984. {
  1985. glisttotlen += 20;
  1986. glist = (struct completer_entry **)
  1987. xrealloc (glist, sizeof (struct completer_entry *) * glisttotlen);
  1988. }
  1989. if (glistlen == 0)
  1990. {
  1991. glist[0] = ent;
  1992. glistlen = 1;
  1993. return ent;
  1994. }
  1995. if (ent->name->num < glist[0]->name->num)
  1996. i = 0;
  1997. else if (ent->name->num > glist[end - 1]->name->num)
  1998. i = end;
  1999. else
  2000. {
  2001. int c;
  2002. while (1)
  2003. {
  2004. i = (start + end) / 2;
  2005. c = ent->name->num - glist[i]->name->num;
  2006. if (c < 0)
  2007. end = i - 1;
  2008. else if (c == 0)
  2009. {
  2010. while (i > 0
  2011. && ent->name->num == glist[i - 1]->name->num)
  2012. i--;
  2013. break;
  2014. }
  2015. else
  2016. start = i + 1;
  2017. if (start > end)
  2018. break;
  2019. }
  2020. if (c == 0)
  2021. {
  2022. while (i < glistlen)
  2023. {
  2024. if (ent->name->num != glist[i]->name->num)
  2025. break;
  2026. if (completer_entries_eq (ent, glist[i]))
  2027. return glist[i];
  2028. i++;
  2029. }
  2030. }
  2031. }
  2032. for (; i > 0 && i < glistlen; i--)
  2033. if (ent->name->num >= glist[i - 1]->name->num)
  2034. break;
  2035. for (; i < glistlen; i++)
  2036. if (ent->name->num < glist[i]->name->num)
  2037. break;
  2038. for (x = glistlen - 1; x >= i; x--)
  2039. glist[x + 1] = glist[x];
  2040. glist[i] = ent;
  2041. glistlen++;
  2042. }
  2043. return ent;
  2044. }
  2045. static int
  2046. get_prefix_len (const char *name)
  2047. {
  2048. char *c;
  2049. if (name[0] == '\0')
  2050. return 0;
  2051. c = strchr (name, '.');
  2052. if (c != NULL)
  2053. return c - name;
  2054. else
  2055. return strlen (name);
  2056. }
  2057. static void
  2058. compute_completer_bits (struct main_entry *ment, struct completer_entry *ent)
  2059. {
  2060. while (ent != NULL)
  2061. {
  2062. compute_completer_bits (ment, ent->addl_entries);
  2063. if (ent->is_terminal)
  2064. {
  2065. ia64_insn mask = 0;
  2066. ia64_insn our_bits = ent->bits;
  2067. struct completer_entry *p = ent->parent;
  2068. ia64_insn p_bits;
  2069. int x;
  2070. while (p != NULL && ! p->is_terminal)
  2071. p = p->parent;
  2072. if (p != NULL)
  2073. p_bits = p->bits;
  2074. else
  2075. p_bits = ment->opcode->opcode;
  2076. for (x = 0; x < 64; x++)
  2077. {
  2078. ia64_insn m = ((ia64_insn) 1) << x;
  2079. if ((p_bits & m) != (our_bits & m))
  2080. mask |= m;
  2081. else
  2082. our_bits &= ~m;
  2083. }
  2084. ent->bits = our_bits;
  2085. ent->mask = mask;
  2086. }
  2087. else
  2088. {
  2089. ent->bits = 0;
  2090. ent->mask = 0;
  2091. }
  2092. ent = ent->alternative;
  2093. }
  2094. }
  2095. /* Find identical completer trees that are used in different
  2096. instructions and collapse their entries. */
  2097. static void
  2098. collapse_redundant_completers (void)
  2099. {
  2100. struct main_entry *ptr;
  2101. int x;
  2102. for (ptr = maintable; ptr != NULL; ptr = ptr->next)
  2103. {
  2104. if (ptr->completers == NULL)
  2105. abort ();
  2106. compute_completer_bits (ptr, ptr->completers);
  2107. ptr->completers = insert_gclist (ptr->completers);
  2108. }
  2109. /* The table has been finalized, now number the indexes. */
  2110. for (x = 0; x < glistlen; x++)
  2111. glist[x]->num = x;
  2112. }
  2113. /* Attach two lists of dependencies to each opcode.
  2114. 1) all resources which, when already marked in use, conflict with this
  2115. opcode (chks)
  2116. 2) all resources which must be marked in use when this opcode is used
  2117. (regs). */
  2118. static int
  2119. insert_opcode_dependencies (struct ia64_opcode *opc,
  2120. struct completer_entry *cmp ATTRIBUTE_UNUSED)
  2121. {
  2122. /* Note all resources which point to this opcode. rfi has the most chks
  2123. (79) and cmpxchng has the most regs (54) so 100 here should be enough. */
  2124. int i;
  2125. int nregs = 0;
  2126. unsigned short regs[256];
  2127. int nchks = 0;
  2128. unsigned short chks[256];
  2129. /* Flag insns for which no class matched; there should be none. */
  2130. int no_class_found = 1;
  2131. for (i = 0; i < rdepslen; i++)
  2132. {
  2133. struct rdep *rs = rdeps[i];
  2134. int j;
  2135. if (strcmp (opc->name, "cmp.eq.and") == 0
  2136. && startswith (rs->name, "PR%")
  2137. && rs->mode == 1)
  2138. no_class_found = 99;
  2139. for (j=0; j < rs->nregs;j++)
  2140. {
  2141. int ic_note = 0;
  2142. if (in_iclass (opc, ics[rs->regs[j]], NULL, NULL, &ic_note))
  2143. {
  2144. /* We can ignore ic_note 11 for non PR resources. */
  2145. if (ic_note == 11 && ! startswith (rs->name, "PR"))
  2146. ic_note = 0;
  2147. if (ic_note != 0 && rs->regnotes[j] != 0
  2148. && ic_note != rs->regnotes[j]
  2149. && !(ic_note == 11 && rs->regnotes[j] == 1))
  2150. warn (_("IC note %d in opcode %s (IC:%s) conflicts with resource %s note %d\n"),
  2151. ic_note, opc->name, ics[rs->regs[j]]->name,
  2152. rs->name, rs->regnotes[j]);
  2153. /* Instruction class notes override resource notes.
  2154. So far, only note 11 applies to an IC instead of a resource,
  2155. and note 11 implies note 1. */
  2156. if (ic_note)
  2157. regs[nregs++] = RDEP(ic_note, i);
  2158. else
  2159. regs[nregs++] = RDEP(rs->regnotes[j], i);
  2160. no_class_found = 0;
  2161. ++rs->total_regs;
  2162. }
  2163. }
  2164. for (j = 0; j < rs->nchks; j++)
  2165. {
  2166. int ic_note = 0;
  2167. if (in_iclass (opc, ics[rs->chks[j]], NULL, NULL, &ic_note))
  2168. {
  2169. /* We can ignore ic_note 11 for non PR resources. */
  2170. if (ic_note == 11 && ! startswith (rs->name, "PR"))
  2171. ic_note = 0;
  2172. if (ic_note != 0 && rs->chknotes[j] != 0
  2173. && ic_note != rs->chknotes[j]
  2174. && !(ic_note == 11 && rs->chknotes[j] == 1))
  2175. warn (_("IC note %d for opcode %s (IC:%s) conflicts with resource %s note %d\n"),
  2176. ic_note, opc->name, ics[rs->chks[j]]->name,
  2177. rs->name, rs->chknotes[j]);
  2178. if (ic_note)
  2179. chks[nchks++] = RDEP(ic_note, i);
  2180. else
  2181. chks[nchks++] = RDEP(rs->chknotes[j], i);
  2182. no_class_found = 0;
  2183. ++rs->total_chks;
  2184. }
  2185. }
  2186. }
  2187. if (no_class_found)
  2188. warn (_("opcode %s has no class (ops %d %d %d)\n"),
  2189. opc->name,
  2190. opc->operands[0], opc->operands[1], opc->operands[2]);
  2191. return insert_dependencies (nchks, chks, nregs, regs);
  2192. }
  2193. static void
  2194. insert_completer_entry (struct ia64_opcode *opc, struct main_entry *tabent,
  2195. int order)
  2196. {
  2197. struct completer_entry **ptr = &tabent->completers;
  2198. struct completer_entry *parent = NULL;
  2199. char pcopy[129], *prefix;
  2200. int at_end = 0;
  2201. if (strlen (opc->name) > 128)
  2202. abort ();
  2203. strcpy (pcopy, opc->name);
  2204. prefix = pcopy + get_prefix_len (pcopy);
  2205. if (prefix[0] != '\0')
  2206. prefix++;
  2207. while (! at_end)
  2208. {
  2209. int need_new_ent = 1;
  2210. int plen = get_prefix_len (prefix);
  2211. struct string_entry *sent;
  2212. at_end = (prefix[plen] == '\0');
  2213. prefix[plen] = '\0';
  2214. sent = insert_string (prefix);
  2215. while (*ptr != NULL)
  2216. {
  2217. int cmpres = sent->num - (*ptr)->name->num;
  2218. if (cmpres == 0)
  2219. {
  2220. need_new_ent = 0;
  2221. break;
  2222. }
  2223. else
  2224. ptr = &((*ptr)->alternative);
  2225. }
  2226. if (need_new_ent)
  2227. {
  2228. struct completer_entry *nent = tmalloc (struct completer_entry);
  2229. nent->name = sent;
  2230. nent->parent = parent;
  2231. nent->addl_entries = NULL;
  2232. nent->alternative = *ptr;
  2233. *ptr = nent;
  2234. nent->is_terminal = 0;
  2235. nent->dependencies = -1;
  2236. }
  2237. if (! at_end)
  2238. {
  2239. parent = *ptr;
  2240. ptr = &((*ptr)->addl_entries);
  2241. prefix += plen + 1;
  2242. }
  2243. }
  2244. if ((*ptr)->is_terminal)
  2245. abort ();
  2246. (*ptr)->is_terminal = 1;
  2247. (*ptr)->mask = (ia64_insn)-1;
  2248. (*ptr)->bits = opc->opcode;
  2249. (*ptr)->dependencies = insert_opcode_dependencies (opc, *ptr);
  2250. (*ptr)->order = order;
  2251. }
  2252. static void
  2253. print_completer_entry (struct completer_entry *ent)
  2254. {
  2255. int moffset = 0;
  2256. ia64_insn mask = ent->mask, bits = ent->bits;
  2257. if (mask != 0)
  2258. {
  2259. while (! (mask & 1))
  2260. {
  2261. moffset++;
  2262. mask = mask >> 1;
  2263. bits = bits >> 1;
  2264. }
  2265. if (bits & 0xffffffff00000000LL)
  2266. abort ();
  2267. }
  2268. printf (" { 0x%x, 0x%x, %d, %d, %d, %d, %d, %d },\n",
  2269. (int)bits,
  2270. (int)mask,
  2271. ent->name->num,
  2272. ent->alternative != NULL ? ent->alternative->num : -1,
  2273. ent->addl_entries != NULL ? ent->addl_entries->num : -1,
  2274. moffset,
  2275. ent->is_terminal ? 1 : 0,
  2276. ent->dependencies);
  2277. }
  2278. static void
  2279. print_completer_table (void)
  2280. {
  2281. int x;
  2282. printf ("static const struct ia64_completer_table\ncompleter_table[] = {\n");
  2283. for (x = 0; x < glistlen; x++)
  2284. print_completer_entry (glist[x]);
  2285. printf ("};\n\n");
  2286. }
  2287. static int
  2288. opcodes_eq (struct ia64_opcode *opc1, struct ia64_opcode *opc2)
  2289. {
  2290. int x;
  2291. int plen1, plen2;
  2292. if ((opc1->mask != opc2->mask) || (opc1->type != opc2->type)
  2293. || (opc1->num_outputs != opc2->num_outputs)
  2294. || (opc1->flags != opc2->flags))
  2295. return 0;
  2296. for (x = 0; x < 5; x++)
  2297. if (opc1->operands[x] != opc2->operands[x])
  2298. return 0;
  2299. plen1 = get_prefix_len (opc1->name);
  2300. plen2 = get_prefix_len (opc2->name);
  2301. if (plen1 == plen2 && (memcmp (opc1->name, opc2->name, plen1) == 0))
  2302. return 1;
  2303. return 0;
  2304. }
  2305. static void
  2306. add_opcode_entry (struct ia64_opcode *opc)
  2307. {
  2308. struct main_entry **place;
  2309. struct string_entry *name;
  2310. char prefix[129];
  2311. int found_it = 0;
  2312. if (strlen (opc->name) > 128)
  2313. abort ();
  2314. place = &maintable;
  2315. strcpy (prefix, opc->name);
  2316. prefix[get_prefix_len (prefix)] = '\0';
  2317. name = insert_string (prefix);
  2318. /* Walk the list of opcode table entries. If it's a new
  2319. instruction, allocate and fill in a new entry. Note
  2320. the main table is alphabetical by opcode name. */
  2321. while (*place != NULL)
  2322. {
  2323. if ((*place)->name->num == name->num
  2324. && opcodes_eq ((*place)->opcode, opc))
  2325. {
  2326. found_it = 1;
  2327. break;
  2328. }
  2329. if ((*place)->name->num > name->num)
  2330. break;
  2331. place = &((*place)->next);
  2332. }
  2333. if (! found_it)
  2334. {
  2335. struct main_entry *nent = tmalloc (struct main_entry);
  2336. nent->name = name;
  2337. nent->opcode = opc;
  2338. nent->next = *place;
  2339. nent->completers = 0;
  2340. *place = nent;
  2341. if (otlen == ottotlen)
  2342. {
  2343. ottotlen += 20;
  2344. ordered_table = (struct main_entry **)
  2345. xrealloc (ordered_table, sizeof (struct main_entry *) * ottotlen);
  2346. }
  2347. ordered_table[otlen++] = nent;
  2348. }
  2349. insert_completer_entry (opc, *place, opcode_count++);
  2350. }
  2351. static void
  2352. print_main_table (void)
  2353. {
  2354. struct main_entry *ptr = maintable;
  2355. int tindex = 0;
  2356. printf ("static const struct ia64_main_table\nmain_table[] = {\n");
  2357. while (ptr != NULL)
  2358. {
  2359. printf (" { %d, %d, %d, 0x",
  2360. ptr->name->num,
  2361. ptr->opcode->type,
  2362. ptr->opcode->num_outputs);
  2363. opcode_fprintf_vma (stdout, ptr->opcode->opcode);
  2364. printf ("ull, 0x");
  2365. opcode_fprintf_vma (stdout, ptr->opcode->mask);
  2366. printf ("ull, { %d, %d, %d, %d, %d }, 0x%x, %d, },\n",
  2367. ptr->opcode->operands[0],
  2368. ptr->opcode->operands[1],
  2369. ptr->opcode->operands[2],
  2370. ptr->opcode->operands[3],
  2371. ptr->opcode->operands[4],
  2372. ptr->opcode->flags,
  2373. ptr->completers->num);
  2374. ptr->main_index = tindex++;
  2375. ptr = ptr->next;
  2376. }
  2377. printf ("};\n\n");
  2378. }
  2379. static void
  2380. shrink (struct ia64_opcode *table)
  2381. {
  2382. int curr_opcode;
  2383. for (curr_opcode = 0; table[curr_opcode].name != NULL; curr_opcode++)
  2384. {
  2385. add_opcode_entry (table + curr_opcode);
  2386. if (table[curr_opcode].num_outputs == 2
  2387. && ((table[curr_opcode].operands[0] == IA64_OPND_P1
  2388. && table[curr_opcode].operands[1] == IA64_OPND_P2)
  2389. || (table[curr_opcode].operands[0] == IA64_OPND_P2
  2390. && table[curr_opcode].operands[1] == IA64_OPND_P1)))
  2391. {
  2392. struct ia64_opcode *alias = tmalloc(struct ia64_opcode);
  2393. unsigned i;
  2394. *alias = table[curr_opcode];
  2395. for (i = 2; i < NELEMS (alias->operands); ++i)
  2396. alias->operands[i - 1] = alias->operands[i];
  2397. alias->operands[NELEMS (alias->operands) - 1] = IA64_OPND_NIL;
  2398. --alias->num_outputs;
  2399. alias->flags |= PSEUDO;
  2400. add_opcode_entry (alias);
  2401. }
  2402. }
  2403. }
  2404. /* Program options. */
  2405. #define OPTION_SRCDIR 200
  2406. struct option long_options[] =
  2407. {
  2408. {"srcdir", required_argument, NULL, OPTION_SRCDIR},
  2409. {"debug", no_argument, NULL, 'd'},
  2410. {"version", no_argument, NULL, 'V'},
  2411. {"help", no_argument, NULL, 'h'},
  2412. {0, no_argument, NULL, 0}
  2413. };
  2414. static void
  2415. print_version (void)
  2416. {
  2417. printf ("%s: version 1.0\n", program_name);
  2418. xexit (0);
  2419. }
  2420. static void
  2421. usage (FILE * stream, int status)
  2422. {
  2423. fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
  2424. program_name);
  2425. xexit (status);
  2426. }
  2427. int
  2428. main (int argc, char **argv)
  2429. {
  2430. extern int chdir (char *);
  2431. char *srcdir = NULL;
  2432. int c;
  2433. program_name = *argv;
  2434. xmalloc_set_program_name (program_name);
  2435. while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
  2436. switch (c)
  2437. {
  2438. case OPTION_SRCDIR:
  2439. srcdir = optarg;
  2440. break;
  2441. case 'V':
  2442. case 'v':
  2443. print_version ();
  2444. break;
  2445. case 'd':
  2446. debug = 1;
  2447. break;
  2448. case 'h':
  2449. case '?':
  2450. usage (stderr, 0);
  2451. default:
  2452. case 0:
  2453. break;
  2454. }
  2455. if (optind != argc)
  2456. usage (stdout, 1);
  2457. if (srcdir != NULL)
  2458. if (chdir (srcdir) != 0)
  2459. fail (_("unable to change directory to \"%s\", errno = %s\n"),
  2460. srcdir, strerror (errno));
  2461. load_insn_classes ();
  2462. load_dependencies ();
  2463. shrink (ia64_opcodes_a);
  2464. shrink (ia64_opcodes_b);
  2465. shrink (ia64_opcodes_f);
  2466. shrink (ia64_opcodes_i);
  2467. shrink (ia64_opcodes_m);
  2468. shrink (ia64_opcodes_x);
  2469. shrink (ia64_opcodes_d);
  2470. collapse_redundant_completers ();
  2471. printf ("/* This file is automatically generated by ia64-gen. Do not edit! */\n");
  2472. printf ("/* Copyright (C) 2007-2022 Free Software Foundation, Inc.\n\
  2473. \n\
  2474. This file is part of the GNU opcodes library.\n\
  2475. \n\
  2476. This library is free software; you can redistribute it and/or modify\n\
  2477. it under the terms of the GNU General Public License as published by\n\
  2478. the Free Software Foundation; either version 3, or (at your option)\n\
  2479. any later version.\n\
  2480. \n\
  2481. It is distributed in the hope that it will be useful, but WITHOUT\n\
  2482. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
  2483. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
  2484. License for more details.\n\
  2485. \n\
  2486. You should have received a copy of the GNU General Public License\n\
  2487. along with this program; see the file COPYING. If not, write to the\n\
  2488. Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA\n\
  2489. 02110-1301, USA. */\n");
  2490. print_string_table ();
  2491. print_dependency_table ();
  2492. print_completer_table ();
  2493. print_main_table ();
  2494. generate_disassembler ();
  2495. exit (0);
  2496. }