genoutput.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /* Generate code from to output assembler insns as recognized from rtl.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This program reads the machine description for the compiler target machine
  16. and produces a file containing these things:
  17. 1. An array of `struct insn_data_d', which is indexed by insn code number,
  18. which contains:
  19. a. `name' is the name for that pattern. Nameless patterns are
  20. given a name.
  21. b. `output' hold either the output template, an array of output
  22. templates, or an output function.
  23. c. `genfun' is the function to generate a body for that pattern,
  24. given operands as arguments.
  25. d. `n_operands' is the number of distinct operands in the pattern
  26. for that insn,
  27. e. `n_dups' is the number of match_dup's that appear in the insn's
  28. pattern. This says how many elements of `recog_data.dup_loc' are
  29. significant after an insn has been recognized.
  30. f. `n_alternatives' is the number of alternatives in the constraints
  31. of each pattern.
  32. g. `output_format' tells what type of thing `output' is.
  33. h. `operand' is the base of an array of operand data for the insn.
  34. 2. An array of `struct insn_operand data', used by `operand' above.
  35. a. `predicate', an int-valued function, is the match_operand predicate
  36. for this operand.
  37. b. `constraint' is the constraint for this operand.
  38. c. `address_p' indicates that the operand appears within ADDRESS
  39. rtx's.
  40. d. `mode' is the machine mode that that operand is supposed to have.
  41. e. `strict_low', is nonzero for operands contained in a STRICT_LOW_PART.
  42. f. `eliminable', is nonzero for operands that are matched normally by
  43. MATCH_OPERAND; it is zero for operands that should not be changed during
  44. register elimination such as MATCH_OPERATORs.
  45. g. `allows_mem', is true for operands that accept MEM rtxes.
  46. The code number of an insn is simply its position in the machine
  47. description; code numbers are assigned sequentially to entries in
  48. the description, starting with code number 0.
  49. Thus, the following entry in the machine description
  50. (define_insn "clrdf"
  51. [(set (match_operand:DF 0 "general_operand" "")
  52. (const_int 0))]
  53. ""
  54. "clrd %0")
  55. assuming it is the 25th entry present, would cause
  56. insn_data[24].template to be "clrd %0", and
  57. insn_data[24].n_operands to be 1. */
  58. #include "bconfig.h"
  59. #include "system.h"
  60. #include "coretypes.h"
  61. #include "tm.h"
  62. #include "rtl.h"
  63. #include "errors.h"
  64. #include "read-md.h"
  65. #include "gensupport.h"
  66. /* No instruction can have more operands than this. Sorry for this
  67. arbitrary limit, but what machine will have an instruction with
  68. this many operands? */
  69. #define MAX_MAX_OPERANDS 40
  70. static char general_mem[] = { TARGET_MEM_CONSTRAINT, 0 };
  71. static int n_occurrences (int, const char *);
  72. static const char *strip_whitespace (const char *);
  73. /* insns in the machine description are assigned sequential code numbers
  74. that are used by insn-recog.c (produced by genrecog) to communicate
  75. to insn-output.c (produced by this program). */
  76. static int next_code_number;
  77. /* This counts all definitions in the md file,
  78. for the sake of error messages. */
  79. static int next_index_number;
  80. /* This counts all operands used in the md file. The first is null. */
  81. static int next_operand_number = 1;
  82. /* Record in this chain all information about the operands we will output. */
  83. struct operand_data
  84. {
  85. struct operand_data *next;
  86. int index;
  87. const char *predicate;
  88. const char *constraint;
  89. machine_mode mode;
  90. unsigned char n_alternatives;
  91. char address_p;
  92. char strict_low;
  93. char eliminable;
  94. char seen;
  95. };
  96. /* Begin with a null operand at index 0. */
  97. static struct operand_data null_operand =
  98. {
  99. 0, 0, "", "", VOIDmode, 0, 0, 0, 0, 0
  100. };
  101. static struct operand_data *odata = &null_operand;
  102. static struct operand_data **odata_end = &null_operand.next;
  103. /* Must match the constants in recog.h. */
  104. #define INSN_OUTPUT_FORMAT_NONE 0 /* abort */
  105. #define INSN_OUTPUT_FORMAT_SINGLE 1 /* const char * */
  106. #define INSN_OUTPUT_FORMAT_MULTI 2 /* const char * const * */
  107. #define INSN_OUTPUT_FORMAT_FUNCTION 3 /* const char * (*)(...) */
  108. /* Record in this chain all information that we will output,
  109. associated with the code number of the insn. */
  110. struct data
  111. {
  112. struct data *next;
  113. const char *name;
  114. const char *template_code;
  115. int code_number;
  116. int index_number;
  117. const char *filename;
  118. int lineno;
  119. int n_generator_args; /* Number of arguments passed to generator */
  120. int n_operands; /* Number of operands this insn recognizes */
  121. int n_dups; /* Number times match_dup appears in pattern */
  122. int n_alternatives; /* Number of alternatives in each constraint */
  123. int operand_number; /* Operand index in the big array. */
  124. int output_format; /* INSN_OUTPUT_FORMAT_*. */
  125. struct operand_data operand[MAX_MAX_OPERANDS];
  126. };
  127. /* A dummy insn, for CODE_FOR_nothing. */
  128. static struct data nothing;
  129. /* This variable points to the first link in the insn chain. */
  130. static struct data *idata = &nothing;
  131. /* This variable points to the end of the insn chain. This is where
  132. everything relevant from the machien description is appended to. */
  133. static struct data **idata_end = &nothing.next;
  134. static void output_prologue (void);
  135. static void output_operand_data (void);
  136. static void output_insn_data (void);
  137. static void output_get_insn_name (void);
  138. static void scan_operands (struct data *, rtx, int, int);
  139. static int compare_operands (struct operand_data *,
  140. struct operand_data *);
  141. static void place_operands (struct data *);
  142. static void process_template (struct data *, const char *);
  143. static void validate_insn_alternatives (struct data *);
  144. static void validate_insn_operands (struct data *);
  145. static void gen_insn (rtx, int);
  146. static void gen_peephole (rtx, int);
  147. static void gen_expand (rtx, int);
  148. static void gen_split (rtx, int);
  149. struct constraint_data
  150. {
  151. struct constraint_data *next_this_letter;
  152. int lineno;
  153. unsigned int namelen;
  154. const char name[1];
  155. };
  156. /* All machine-independent constraint characters (except digits) that
  157. are handled outside the define*_constraint mechanism. */
  158. static const char indep_constraints[] = ",=+%*?!^$#&g";
  159. static struct constraint_data *
  160. constraints_by_letter_table[1 << CHAR_BIT];
  161. static int mdep_constraint_len (const char *, int, int);
  162. static void note_constraint (rtx, int);
  163. static void
  164. output_prologue (void)
  165. {
  166. printf ("/* Generated automatically by the program `genoutput'\n\
  167. from the machine description file `md'. */\n\n");
  168. printf ("#include \"config.h\"\n");
  169. printf ("#include \"system.h\"\n");
  170. printf ("#include \"coretypes.h\"\n");
  171. printf ("#include \"tm.h\"\n");
  172. printf ("#include \"flags.h\"\n");
  173. printf ("#include \"ggc.h\"\n");
  174. printf ("#include \"hash-set.h\"\n");
  175. printf ("#include \"machmode.h\"\n");
  176. printf ("#include \"vec.h\"\n");
  177. printf ("#include \"double-int.h\"\n");
  178. printf ("#include \"input.h\"\n");
  179. printf ("#include \"alias.h\"\n");
  180. printf ("#include \"symtab.h\"\n");
  181. printf ("#include \"wide-int.h\"\n");
  182. printf ("#include \"inchash.h\"\n");
  183. printf ("#include \"tree.h\"\n");
  184. printf ("#include \"varasm.h\"\n");
  185. printf ("#include \"stor-layout.h\"\n");
  186. printf ("#include \"calls.h\"\n");
  187. printf ("#include \"rtl.h\"\n");
  188. printf ("#include \"hashtab.h\"\n");
  189. printf ("#include \"hard-reg-set.h\"\n");
  190. printf ("#include \"function.h\"\n");
  191. printf ("#include \"statistics.h\"\n");
  192. printf ("#include \"real.h\"\n");
  193. printf ("#include \"fixed-value.h\"\n");
  194. printf ("#include \"insn-config.h\"\n");
  195. printf ("#include \"expmed.h\"\n");
  196. printf ("#include \"dojump.h\"\n");
  197. printf ("#include \"explow.h\"\n");
  198. printf ("#include \"emit-rtl.h\"\n");
  199. printf ("#include \"stmt.h\"\n");
  200. printf ("#include \"expr.h\"\n");
  201. printf ("#include \"insn-codes.h\"\n");
  202. printf ("#include \"tm_p.h\"\n");
  203. printf ("#include \"regs.h\"\n");
  204. printf ("#include \"conditions.h\"\n");
  205. printf ("#include \"insn-attr.h\"\n\n");
  206. printf ("#include \"recog.h\"\n\n");
  207. printf ("#include \"diagnostic-core.h\"\n");
  208. printf ("#include \"output.h\"\n");
  209. printf ("#include \"target.h\"\n");
  210. printf ("#include \"tm-constrs.h\"\n");
  211. printf ("#include \"predict.h\"\n");
  212. }
  213. static void
  214. output_operand_data (void)
  215. {
  216. struct operand_data *d;
  217. printf ("\nstatic const struct insn_operand_data operand_data[] = \n{\n");
  218. for (d = odata; d; d = d->next)
  219. {
  220. struct pred_data *pred;
  221. printf (" {\n");
  222. printf (" %s,\n",
  223. d->predicate && d->predicate[0] ? d->predicate : "0");
  224. printf (" \"%s\",\n", d->constraint ? d->constraint : "");
  225. printf (" %smode,\n", GET_MODE_NAME (d->mode));
  226. printf (" %d,\n", d->strict_low);
  227. printf (" %d,\n", d->constraint == NULL ? 1 : 0);
  228. printf (" %d,\n", d->eliminable);
  229. pred = NULL;
  230. if (d->predicate)
  231. pred = lookup_predicate (d->predicate);
  232. printf (" %d\n", pred && pred->codes[MEM]);
  233. printf (" },\n");
  234. }
  235. printf ("};\n\n\n");
  236. }
  237. static void
  238. output_insn_data (void)
  239. {
  240. struct data *d;
  241. int name_offset = 0;
  242. int next_name_offset;
  243. const char * last_name = 0;
  244. const char * next_name = 0;
  245. struct data *n;
  246. for (n = idata, next_name_offset = 1; n; n = n->next, next_name_offset++)
  247. if (n->name)
  248. {
  249. next_name = n->name;
  250. break;
  251. }
  252. printf ("#if GCC_VERSION >= 2007\n__extension__\n#endif\n");
  253. printf ("\nconst struct insn_data_d insn_data[] = \n{\n");
  254. for (d = idata; d; d = d->next)
  255. {
  256. printf (" /* %s:%d */\n", d->filename, d->lineno);
  257. printf (" {\n");
  258. if (d->name)
  259. {
  260. printf (" \"%s\",\n", d->name);
  261. name_offset = 0;
  262. last_name = d->name;
  263. next_name = 0;
  264. for (n = d->next, next_name_offset = 1; n;
  265. n = n->next, next_name_offset++)
  266. {
  267. if (n->name)
  268. {
  269. next_name = n->name;
  270. break;
  271. }
  272. }
  273. }
  274. else
  275. {
  276. name_offset++;
  277. if (next_name && (last_name == 0
  278. || name_offset > next_name_offset / 2))
  279. printf (" \"%s-%d\",\n", next_name,
  280. next_name_offset - name_offset);
  281. else
  282. printf (" \"%s+%d\",\n", last_name, name_offset);
  283. }
  284. switch (d->output_format)
  285. {
  286. case INSN_OUTPUT_FORMAT_NONE:
  287. printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
  288. printf (" { 0 },\n");
  289. printf ("#else\n");
  290. printf (" { 0, 0, 0 },\n");
  291. printf ("#endif\n");
  292. break;
  293. case INSN_OUTPUT_FORMAT_SINGLE:
  294. {
  295. const char *p = d->template_code;
  296. char prev = 0;
  297. printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
  298. printf (" { .single =\n");
  299. printf ("#else\n");
  300. printf (" {\n");
  301. printf ("#endif\n");
  302. printf (" \"");
  303. while (*p)
  304. {
  305. if (IS_VSPACE (*p) && prev != '\\')
  306. {
  307. /* Preserve two consecutive \n's or \r's, but treat \r\n
  308. as a single newline. */
  309. if (*p == '\n' && prev != '\r')
  310. printf ("\\n\\\n");
  311. }
  312. else
  313. putchar (*p);
  314. prev = *p;
  315. ++p;
  316. }
  317. printf ("\",\n");
  318. printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
  319. printf (" },\n");
  320. printf ("#else\n");
  321. printf (" 0, 0 },\n");
  322. printf ("#endif\n");
  323. }
  324. break;
  325. case INSN_OUTPUT_FORMAT_MULTI:
  326. printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
  327. printf (" { .multi = output_%d },\n", d->code_number);
  328. printf ("#else\n");
  329. printf (" { 0, output_%d, 0 },\n", d->code_number);
  330. printf ("#endif\n");
  331. break;
  332. case INSN_OUTPUT_FORMAT_FUNCTION:
  333. printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
  334. printf (" { .function = output_%d },\n", d->code_number);
  335. printf ("#else\n");
  336. printf (" { 0, 0, output_%d },\n", d->code_number);
  337. printf ("#endif\n");
  338. break;
  339. default:
  340. gcc_unreachable ();
  341. }
  342. if (d->name && d->name[0] != '*')
  343. printf (" { (insn_gen_fn::stored_funcptr) gen_%s },\n", d->name);
  344. else
  345. printf (" { 0 },\n");
  346. printf (" &operand_data[%d],\n", d->operand_number);
  347. printf (" %d,\n", d->n_generator_args);
  348. printf (" %d,\n", d->n_operands);
  349. printf (" %d,\n", d->n_dups);
  350. printf (" %d,\n", d->n_alternatives);
  351. printf (" %d\n", d->output_format);
  352. printf (" },\n");
  353. }
  354. printf ("};\n\n\n");
  355. }
  356. static void
  357. output_get_insn_name (void)
  358. {
  359. printf ("const char *\n");
  360. printf ("get_insn_name (int code)\n");
  361. printf ("{\n");
  362. printf (" if (code == NOOP_MOVE_INSN_CODE)\n");
  363. printf (" return \"NOOP_MOVE\";\n");
  364. printf (" else\n");
  365. printf (" return insn_data[code].name;\n");
  366. printf ("}\n");
  367. }
  368. /* Stores the operand data into `d->operand[i]'.
  369. THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
  370. THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART. */
  371. static void
  372. scan_operands (struct data *d, rtx part, int this_address_p,
  373. int this_strict_low)
  374. {
  375. int i, j;
  376. const char *format_ptr;
  377. int opno;
  378. if (part == 0)
  379. return;
  380. switch (GET_CODE (part))
  381. {
  382. case MATCH_OPERAND:
  383. opno = XINT (part, 0);
  384. if (opno >= MAX_MAX_OPERANDS)
  385. {
  386. error_with_line (d->lineno, "maximum number of operands exceeded");
  387. return;
  388. }
  389. if (d->operand[opno].seen)
  390. error_with_line (d->lineno, "repeated operand number %d\n", opno);
  391. d->operand[opno].seen = 1;
  392. d->operand[opno].mode = GET_MODE (part);
  393. d->operand[opno].strict_low = this_strict_low;
  394. d->operand[opno].predicate = XSTR (part, 1);
  395. d->operand[opno].constraint = strip_whitespace (XSTR (part, 2));
  396. d->operand[opno].n_alternatives
  397. = n_occurrences (',', d->operand[opno].constraint) + 1;
  398. d->operand[opno].address_p = this_address_p;
  399. d->operand[opno].eliminable = 1;
  400. return;
  401. case MATCH_SCRATCH:
  402. opno = XINT (part, 0);
  403. if (opno >= MAX_MAX_OPERANDS)
  404. {
  405. error_with_line (d->lineno, "maximum number of operands exceeded");
  406. return;
  407. }
  408. if (d->operand[opno].seen)
  409. error_with_line (d->lineno, "repeated operand number %d\n", opno);
  410. d->operand[opno].seen = 1;
  411. d->operand[opno].mode = GET_MODE (part);
  412. d->operand[opno].strict_low = 0;
  413. d->operand[opno].predicate = "scratch_operand";
  414. d->operand[opno].constraint = strip_whitespace (XSTR (part, 1));
  415. d->operand[opno].n_alternatives
  416. = n_occurrences (',', d->operand[opno].constraint) + 1;
  417. d->operand[opno].address_p = 0;
  418. d->operand[opno].eliminable = 0;
  419. return;
  420. case MATCH_OPERATOR:
  421. case MATCH_PARALLEL:
  422. opno = XINT (part, 0);
  423. if (opno >= MAX_MAX_OPERANDS)
  424. {
  425. error_with_line (d->lineno, "maximum number of operands exceeded");
  426. return;
  427. }
  428. if (d->operand[opno].seen)
  429. error_with_line (d->lineno, "repeated operand number %d\n", opno);
  430. d->operand[opno].seen = 1;
  431. d->operand[opno].mode = GET_MODE (part);
  432. d->operand[opno].strict_low = 0;
  433. d->operand[opno].predicate = XSTR (part, 1);
  434. d->operand[opno].constraint = 0;
  435. d->operand[opno].address_p = 0;
  436. d->operand[opno].eliminable = 0;
  437. for (i = 0; i < XVECLEN (part, 2); i++)
  438. scan_operands (d, XVECEXP (part, 2, i), 0, 0);
  439. return;
  440. case STRICT_LOW_PART:
  441. scan_operands (d, XEXP (part, 0), 0, 1);
  442. return;
  443. default:
  444. break;
  445. }
  446. format_ptr = GET_RTX_FORMAT (GET_CODE (part));
  447. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
  448. switch (*format_ptr++)
  449. {
  450. case 'e':
  451. case 'u':
  452. scan_operands (d, XEXP (part, i), 0, 0);
  453. break;
  454. case 'E':
  455. if (XVEC (part, i) != NULL)
  456. for (j = 0; j < XVECLEN (part, i); j++)
  457. scan_operands (d, XVECEXP (part, i, j), 0, 0);
  458. break;
  459. }
  460. }
  461. /* Compare two operands for content equality. */
  462. static int
  463. compare_operands (struct operand_data *d0, struct operand_data *d1)
  464. {
  465. const char *p0, *p1;
  466. p0 = d0->predicate;
  467. if (!p0)
  468. p0 = "";
  469. p1 = d1->predicate;
  470. if (!p1)
  471. p1 = "";
  472. if (strcmp (p0, p1) != 0)
  473. return 0;
  474. p0 = d0->constraint;
  475. if (!p0)
  476. p0 = "";
  477. p1 = d1->constraint;
  478. if (!p1)
  479. p1 = "";
  480. if (strcmp (p0, p1) != 0)
  481. return 0;
  482. if (d0->mode != d1->mode)
  483. return 0;
  484. if (d0->strict_low != d1->strict_low)
  485. return 0;
  486. if (d0->eliminable != d1->eliminable)
  487. return 0;
  488. return 1;
  489. }
  490. /* Scan the list of operands we've already committed to output and either
  491. find a subsequence that is the same, or allocate a new one at the end. */
  492. static void
  493. place_operands (struct data *d)
  494. {
  495. struct operand_data *od, *od2;
  496. int i;
  497. if (d->n_operands == 0)
  498. {
  499. d->operand_number = 0;
  500. return;
  501. }
  502. /* Brute force substring search. */
  503. for (od = odata, i = 0; od; od = od->next, i = 0)
  504. if (compare_operands (od, &d->operand[0]))
  505. {
  506. od2 = od->next;
  507. i = 1;
  508. while (1)
  509. {
  510. if (i == d->n_operands)
  511. goto full_match;
  512. if (od2 == NULL)
  513. goto partial_match;
  514. if (! compare_operands (od2, &d->operand[i]))
  515. break;
  516. ++i, od2 = od2->next;
  517. }
  518. }
  519. /* Either partial match at the end of the list, or no match. In either
  520. case, we tack on what operands are remaining to the end of the list. */
  521. partial_match:
  522. d->operand_number = next_operand_number - i;
  523. for (; i < d->n_operands; ++i)
  524. {
  525. od2 = &d->operand[i];
  526. *odata_end = od2;
  527. odata_end = &od2->next;
  528. od2->index = next_operand_number++;
  529. }
  530. *odata_end = NULL;
  531. return;
  532. full_match:
  533. d->operand_number = od->index;
  534. return;
  535. }
  536. /* Process an assembler template from a define_insn or a define_peephole.
  537. It is either the assembler code template, a list of assembler code
  538. templates, or C code to generate the assembler code template. */
  539. static void
  540. process_template (struct data *d, const char *template_code)
  541. {
  542. const char *cp;
  543. int i;
  544. /* Templates starting with * contain straight code to be run. */
  545. if (template_code[0] == '*')
  546. {
  547. d->template_code = 0;
  548. d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
  549. puts ("\nstatic const char *");
  550. printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED)\n",
  551. d->code_number);
  552. puts ("{");
  553. print_md_ptr_loc (template_code);
  554. puts (template_code + 1);
  555. puts ("}");
  556. }
  557. /* If the assembler code template starts with a @ it is a newline-separated
  558. list of assembler code templates, one for each alternative. */
  559. else if (template_code[0] == '@')
  560. {
  561. int found_star = 0;
  562. for (cp = &template_code[1]; *cp; )
  563. {
  564. while (ISSPACE (*cp))
  565. cp++;
  566. if (*cp == '*')
  567. found_star = 1;
  568. while (!IS_VSPACE (*cp) && *cp != '\0')
  569. ++cp;
  570. }
  571. d->template_code = 0;
  572. if (found_star)
  573. {
  574. d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
  575. puts ("\nstatic const char *");
  576. printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, "
  577. "rtx_insn *insn ATTRIBUTE_UNUSED)\n", d->code_number);
  578. puts ("{");
  579. puts (" switch (which_alternative)\n {");
  580. }
  581. else
  582. {
  583. d->output_format = INSN_OUTPUT_FORMAT_MULTI;
  584. printf ("\nstatic const char * const output_%d[] = {\n",
  585. d->code_number);
  586. }
  587. for (i = 0, cp = &template_code[1]; *cp; )
  588. {
  589. const char *ep, *sp, *bp;
  590. while (ISSPACE (*cp))
  591. cp++;
  592. bp = cp;
  593. if (found_star)
  594. {
  595. printf (" case %d:", i);
  596. if (*cp == '*')
  597. {
  598. printf ("\n ");
  599. cp++;
  600. }
  601. else
  602. printf (" return \"");
  603. }
  604. else
  605. printf (" \"");
  606. for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
  607. if (!ISSPACE (*ep))
  608. sp = ep + 1;
  609. if (sp != ep)
  610. message_with_line (d->lineno,
  611. "trailing whitespace in output template");
  612. while (cp < sp)
  613. {
  614. putchar (*cp);
  615. cp++;
  616. }
  617. if (!found_star)
  618. puts ("\",");
  619. else if (*bp != '*')
  620. puts ("\";");
  621. else
  622. {
  623. /* The usual action will end with a return.
  624. If there is neither break or return at the end, this is
  625. assumed to be intentional; this allows to have multiple
  626. consecutive alternatives share some code. */
  627. puts ("");
  628. }
  629. i++;
  630. }
  631. if (i == 1)
  632. message_with_line (d->lineno,
  633. "'@' is redundant for output template with single alternative");
  634. if (i != d->n_alternatives)
  635. error_with_line (d->lineno,
  636. "wrong number of alternatives in the output template");
  637. if (found_star)
  638. puts (" default: gcc_unreachable ();\n }\n}");
  639. else
  640. printf ("};\n");
  641. }
  642. else
  643. {
  644. d->template_code = template_code;
  645. d->output_format = INSN_OUTPUT_FORMAT_SINGLE;
  646. }
  647. }
  648. /* Check insn D for consistency in number of constraint alternatives. */
  649. static void
  650. validate_insn_alternatives (struct data *d)
  651. {
  652. int n = 0, start;
  653. /* Make sure all the operands have the same number of alternatives
  654. in their constraints. Let N be that number. */
  655. for (start = 0; start < d->n_operands; start++)
  656. if (d->operand[start].n_alternatives > 0)
  657. {
  658. int len, i;
  659. const char *p;
  660. char c;
  661. int which_alternative = 0;
  662. int alternative_count_unsure = 0;
  663. bool seen_write = false;
  664. for (p = d->operand[start].constraint; (c = *p); p += len)
  665. {
  666. if ((c == '%' || c == '=' || c == '+')
  667. && p != d->operand[start].constraint)
  668. error_with_line (d->lineno,
  669. "character '%c' can only be used at the"
  670. " beginning of a constraint string", c);
  671. if (c == '=' || c == '+')
  672. seen_write = true;
  673. /* Earlyclobber operands must always be marked write-only
  674. or read/write. */
  675. if (!seen_write && c == '&')
  676. error_with_line (d->lineno,
  677. "earlyclobber operands may not be"
  678. " read-only in alternative %d",
  679. which_alternative);
  680. if (ISSPACE (c) || strchr (indep_constraints, c))
  681. len = 1;
  682. else if (ISDIGIT (c))
  683. {
  684. const char *q = p;
  685. do
  686. q++;
  687. while (ISDIGIT (*q));
  688. len = q - p;
  689. }
  690. else
  691. len = mdep_constraint_len (p, d->lineno, start);
  692. if (c == ',')
  693. {
  694. which_alternative++;
  695. continue;
  696. }
  697. for (i = 1; i < len; i++)
  698. if (p[i] == '\0')
  699. {
  700. error_with_line (d->lineno,
  701. "NUL in alternative %d of operand %d",
  702. which_alternative, start);
  703. alternative_count_unsure = 1;
  704. break;
  705. }
  706. else if (strchr (",#*", p[i]))
  707. {
  708. error_with_line (d->lineno,
  709. "'%c' in alternative %d of operand %d",
  710. p[i], which_alternative, start);
  711. alternative_count_unsure = 1;
  712. }
  713. }
  714. if (!alternative_count_unsure)
  715. {
  716. if (n == 0)
  717. n = d->operand[start].n_alternatives;
  718. else if (n != d->operand[start].n_alternatives)
  719. error_with_line (d->lineno,
  720. "wrong number of alternatives in operand %d",
  721. start);
  722. }
  723. }
  724. /* Record the insn's overall number of alternatives. */
  725. d->n_alternatives = n;
  726. }
  727. /* Verify that there are no gaps in operand numbers for INSNs. */
  728. static void
  729. validate_insn_operands (struct data *d)
  730. {
  731. int i;
  732. for (i = 0; i < d->n_operands; ++i)
  733. if (d->operand[i].seen == 0)
  734. error_with_line (d->lineno, "missing operand %d", i);
  735. }
  736. static void
  737. validate_optab_operands (struct data *d)
  738. {
  739. if (!d->name || d->name[0] == '\0' || d->name[0] == '*')
  740. return;
  741. /* Miscellaneous tests. */
  742. if (strncmp (d->name, "cstore", 6) == 0
  743. && d->name[strlen (d->name) - 1] == '4'
  744. && d->operand[0].mode == VOIDmode)
  745. {
  746. message_with_line (d->lineno, "missing mode for operand 0 of cstore");
  747. have_error = 1;
  748. }
  749. }
  750. /* Look at a define_insn just read. Assign its code number. Record
  751. on idata the template and the number of arguments. If the insn has
  752. a hairy output action, output a function for now. */
  753. static void
  754. gen_insn (rtx insn, int lineno)
  755. {
  756. struct pattern_stats stats;
  757. struct data *d = XNEW (struct data);
  758. int i;
  759. d->code_number = next_code_number;
  760. d->index_number = next_index_number;
  761. d->filename = read_md_filename;
  762. d->lineno = lineno;
  763. if (XSTR (insn, 0)[0])
  764. d->name = XSTR (insn, 0);
  765. else
  766. d->name = 0;
  767. /* Build up the list in the same order as the insns are seen
  768. in the machine description. */
  769. d->next = 0;
  770. *idata_end = d;
  771. idata_end = &d->next;
  772. memset (d->operand, 0, sizeof (d->operand));
  773. for (i = 0; i < XVECLEN (insn, 1); i++)
  774. scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
  775. get_pattern_stats (&stats, XVEC (insn, 1));
  776. d->n_generator_args = stats.num_generator_args;
  777. d->n_operands = stats.num_insn_operands;
  778. d->n_dups = stats.num_dups;
  779. validate_insn_operands (d);
  780. validate_insn_alternatives (d);
  781. validate_optab_operands (d);
  782. place_operands (d);
  783. process_template (d, XTMPL (insn, 3));
  784. }
  785. /* Look at a define_peephole just read. Assign its code number.
  786. Record on idata the template and the number of arguments.
  787. If the insn has a hairy output action, output it now. */
  788. static void
  789. gen_peephole (rtx peep, int lineno)
  790. {
  791. struct pattern_stats stats;
  792. struct data *d = XNEW (struct data);
  793. int i;
  794. d->code_number = next_code_number;
  795. d->index_number = next_index_number;
  796. d->filename = read_md_filename;
  797. d->lineno = lineno;
  798. d->name = 0;
  799. /* Build up the list in the same order as the insns are seen
  800. in the machine description. */
  801. d->next = 0;
  802. *idata_end = d;
  803. idata_end = &d->next;
  804. memset (d->operand, 0, sizeof (d->operand));
  805. /* Get the number of operands by scanning all the patterns of the
  806. peephole optimizer. But ignore all the rest of the information
  807. thus obtained. */
  808. for (i = 0; i < XVECLEN (peep, 0); i++)
  809. scan_operands (d, XVECEXP (peep, 0, i), 0, 0);
  810. get_pattern_stats (&stats, XVEC (peep, 0));
  811. d->n_generator_args = 0;
  812. d->n_operands = stats.num_insn_operands;
  813. d->n_dups = 0;
  814. validate_insn_alternatives (d);
  815. place_operands (d);
  816. process_template (d, XTMPL (peep, 2));
  817. }
  818. /* Process a define_expand just read. Assign its code number,
  819. only for the purposes of `insn_gen_function'. */
  820. static void
  821. gen_expand (rtx insn, int lineno)
  822. {
  823. struct pattern_stats stats;
  824. struct data *d = XNEW (struct data);
  825. int i;
  826. d->code_number = next_code_number;
  827. d->index_number = next_index_number;
  828. d->filename = read_md_filename;
  829. d->lineno = lineno;
  830. if (XSTR (insn, 0)[0])
  831. d->name = XSTR (insn, 0);
  832. else
  833. d->name = 0;
  834. /* Build up the list in the same order as the insns are seen
  835. in the machine description. */
  836. d->next = 0;
  837. *idata_end = d;
  838. idata_end = &d->next;
  839. memset (d->operand, 0, sizeof (d->operand));
  840. /* Scan the operands to get the specified predicates and modes,
  841. since expand_binop needs to know them. */
  842. if (XVEC (insn, 1))
  843. for (i = 0; i < XVECLEN (insn, 1); i++)
  844. scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
  845. get_pattern_stats (&stats, XVEC (insn, 1));
  846. d->n_generator_args = stats.num_generator_args;
  847. d->n_operands = stats.num_insn_operands;
  848. d->n_dups = stats.num_dups;
  849. d->template_code = 0;
  850. d->output_format = INSN_OUTPUT_FORMAT_NONE;
  851. validate_insn_alternatives (d);
  852. validate_optab_operands (d);
  853. place_operands (d);
  854. }
  855. /* Process a define_split just read. Assign its code number,
  856. only for reasons of consistency and to simplify genrecog. */
  857. static void
  858. gen_split (rtx split, int lineno)
  859. {
  860. struct pattern_stats stats;
  861. struct data *d = XNEW (struct data);
  862. int i;
  863. d->code_number = next_code_number;
  864. d->index_number = next_index_number;
  865. d->filename = read_md_filename;
  866. d->lineno = lineno;
  867. d->name = 0;
  868. /* Build up the list in the same order as the insns are seen
  869. in the machine description. */
  870. d->next = 0;
  871. *idata_end = d;
  872. idata_end = &d->next;
  873. memset (d->operand, 0, sizeof (d->operand));
  874. /* Get the number of operands by scanning all the patterns of the
  875. split patterns. But ignore all the rest of the information thus
  876. obtained. */
  877. for (i = 0; i < XVECLEN (split, 0); i++)
  878. scan_operands (d, XVECEXP (split, 0, i), 0, 0);
  879. get_pattern_stats (&stats, XVEC (split, 0));
  880. d->n_generator_args = 0;
  881. d->n_operands = stats.num_insn_operands;
  882. d->n_dups = 0;
  883. d->n_alternatives = 0;
  884. d->template_code = 0;
  885. d->output_format = INSN_OUTPUT_FORMAT_NONE;
  886. place_operands (d);
  887. }
  888. static void
  889. init_insn_for_nothing (void)
  890. {
  891. memset (&nothing, 0, sizeof (nothing));
  892. nothing.name = "*placeholder_for_nothing";
  893. nothing.filename = "<internal>";
  894. }
  895. extern int main (int, char **);
  896. int
  897. main (int argc, char **argv)
  898. {
  899. rtx desc;
  900. progname = "genoutput";
  901. init_insn_for_nothing ();
  902. if (!init_rtx_reader_args (argc, argv))
  903. return (FATAL_EXIT_CODE);
  904. output_prologue ();
  905. next_index_number = 0;
  906. /* Read the machine description. */
  907. while (1)
  908. {
  909. int line_no;
  910. desc = read_md_rtx (&line_no, &next_code_number);
  911. if (desc == NULL)
  912. break;
  913. switch (GET_CODE (desc))
  914. {
  915. case DEFINE_INSN:
  916. gen_insn (desc, line_no);
  917. break;
  918. case DEFINE_PEEPHOLE:
  919. gen_peephole (desc, line_no);
  920. break;
  921. case DEFINE_EXPAND:
  922. gen_expand (desc, line_no);
  923. break;
  924. case DEFINE_SPLIT:
  925. case DEFINE_PEEPHOLE2:
  926. gen_split (desc, line_no);
  927. break;
  928. case DEFINE_CONSTRAINT:
  929. case DEFINE_REGISTER_CONSTRAINT:
  930. case DEFINE_ADDRESS_CONSTRAINT:
  931. case DEFINE_MEMORY_CONSTRAINT:
  932. note_constraint (desc, line_no);
  933. break;
  934. default:
  935. break;
  936. }
  937. next_index_number++;
  938. }
  939. printf ("\n\n");
  940. output_operand_data ();
  941. output_insn_data ();
  942. output_get_insn_name ();
  943. fflush (stdout);
  944. return (ferror (stdout) != 0 || have_error
  945. ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  946. }
  947. /* Return the number of occurrences of character C in string S or
  948. -1 if S is the null string. */
  949. static int
  950. n_occurrences (int c, const char *s)
  951. {
  952. int n = 0;
  953. if (s == 0 || *s == '\0')
  954. return -1;
  955. while (*s)
  956. n += (*s++ == c);
  957. return n;
  958. }
  959. /* Remove whitespace in `s' by moving up characters until the end.
  960. Return a new string. */
  961. static const char *
  962. strip_whitespace (const char *s)
  963. {
  964. char *p, *q;
  965. char ch;
  966. if (s == 0)
  967. return 0;
  968. p = q = XNEWVEC (char, strlen (s) + 1);
  969. while ((ch = *s++) != '\0')
  970. if (! ISSPACE (ch))
  971. *p++ = ch;
  972. *p = '\0';
  973. return q;
  974. }
  975. /* Record just enough information about a constraint to allow checking
  976. of operand constraint strings above, in validate_insn_alternatives.
  977. Does not validate most properties of the constraint itself; does
  978. enforce no duplicate names, no overlap with MI constraints, and no
  979. prefixes. EXP is the define_*constraint form, LINENO the line number
  980. reported by the reader. */
  981. static void
  982. note_constraint (rtx exp, int lineno)
  983. {
  984. const char *name = XSTR (exp, 0);
  985. struct constraint_data **iter, **slot, *new_cdata;
  986. if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
  987. name = general_mem;
  988. unsigned int namelen = strlen (name);
  989. if (strchr (indep_constraints, name[0]))
  990. {
  991. if (name[1] == '\0')
  992. error_with_line (lineno, "constraint letter '%s' cannot be "
  993. "redefined by the machine description", name);
  994. else
  995. error_with_line (lineno, "constraint name '%s' cannot be defined by "
  996. "the machine description, as it begins with '%c'",
  997. name, name[0]);
  998. return;
  999. }
  1000. slot = &constraints_by_letter_table[(unsigned int)name[0]];
  1001. for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
  1002. {
  1003. /* This causes slot to end up pointing to the
  1004. next_this_letter field of the last constraint with a name
  1005. of equal or greater length than the new constraint; hence
  1006. the new constraint will be inserted after all previous
  1007. constraints with names of the same length. */
  1008. if ((*iter)->namelen >= namelen)
  1009. slot = iter;
  1010. if (!strcmp ((*iter)->name, name))
  1011. {
  1012. error_with_line (lineno, "redefinition of constraint '%s'", name);
  1013. message_with_line ((*iter)->lineno, "previous definition is here");
  1014. return;
  1015. }
  1016. else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
  1017. {
  1018. error_with_line (lineno, "defining constraint '%s' here", name);
  1019. message_with_line ((*iter)->lineno, "renders constraint '%s' "
  1020. "(defined here) a prefix", (*iter)->name);
  1021. return;
  1022. }
  1023. else if (!strncmp ((*iter)->name, name, namelen))
  1024. {
  1025. error_with_line (lineno, "constraint '%s' is a prefix", name);
  1026. message_with_line ((*iter)->lineno, "of constraint '%s' "
  1027. "(defined here)", (*iter)->name);
  1028. return;
  1029. }
  1030. }
  1031. new_cdata = XNEWVAR (struct constraint_data, sizeof (struct constraint_data) + namelen);
  1032. strcpy (CONST_CAST (char *, new_cdata->name), name);
  1033. new_cdata->namelen = namelen;
  1034. new_cdata->lineno = lineno;
  1035. new_cdata->next_this_letter = *slot;
  1036. *slot = new_cdata;
  1037. }
  1038. /* Return the length of the constraint name beginning at position S
  1039. of an operand constraint string, or issue an error message if there
  1040. is no such constraint. Does not expect to be called for generic
  1041. constraints. */
  1042. static int
  1043. mdep_constraint_len (const char *s, int lineno, int opno)
  1044. {
  1045. struct constraint_data *p;
  1046. p = constraints_by_letter_table[(unsigned int)s[0]];
  1047. if (p)
  1048. for (; p; p = p->next_this_letter)
  1049. if (!strncmp (s, p->name, p->namelen))
  1050. return p->namelen;
  1051. error_with_line (lineno,
  1052. "error: undefined machine-specific constraint "
  1053. "at this point: \"%s\"", s);
  1054. message_with_line (lineno, "note: in operand %d", opno);
  1055. return 1; /* safe */
  1056. }