github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/dist/buildgc.c (about)

     1  // Copyright 2012 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  #include "a.h"
     6  
     7  /*
     8   * Helpers for building cmd/gc.
     9   */
    10  
    11  // gcopnames creates opnames.h from go.h.
    12  // It finds the OXXX enum, pulls out all the constants
    13  // from OXXX to OEND, and writes a table mapping
    14  // op to string.
    15  void
    16  gcopnames(char *dir, char *file)
    17  {
    18  	char *p, *q;
    19  	int i, j, end;
    20  	Buf in, b, out;
    21  	Vec lines, fields;
    22  	
    23  	binit(&in);
    24  	binit(&b);
    25  	binit(&out);
    26  	vinit(&lines);
    27  	vinit(&fields);
    28  	
    29  	bwritestr(&out, bprintf(&b, "// auto generated by go tool dist\n"));
    30  	bwritestr(&out, bprintf(&b, "static char *opnames[] = {\n"));
    31  
    32  	readfile(&in, bprintf(&b, "%s/go.h", dir));
    33  	splitlines(&lines, bstr(&in));
    34  	i = 0;
    35  	while(i<lines.len && !contains(lines.p[i], "OXXX"))
    36  		i++;
    37  	end = 0;
    38  	for(; i<lines.len && !end; i++) {
    39  		p = xstrstr(lines.p[i], "//");
    40  		if(p != nil)
    41  			*p = '\0';
    42  		end = contains(lines.p[i], "OEND");
    43  		splitfields(&fields, lines.p[i]);
    44  		for(j=0; j<fields.len; j++) {
    45  			q = fields.p[j];
    46  			if(*q == 'O')
    47  				q++;
    48  			p = q+xstrlen(q)-1;
    49  			if(*p == ',')
    50  				*p = '\0';
    51  			bwritestr(&out, bprintf(&b, "	[O%s] = \"%s\",\n", q, q));
    52  		}
    53  	}
    54  	
    55  	bwritestr(&out, bprintf(&b, "};\n"));
    56  
    57  	writefile(&out, file, 0);
    58  
    59  	bfree(&in);
    60  	bfree(&b);
    61  	bfree(&out);
    62  	vfree(&lines);
    63  	vfree(&fields);
    64  }
    65  
    66  // mkenam reads [568].out.h and writes enam.c
    67  // The format is much the same as the Go opcodes above.
    68  void
    69  mkenam(char *dir, char *file)
    70  {
    71  	int i, ch;
    72  	Buf in, b, out;
    73  	Vec lines;
    74  	char *p;
    75  
    76  	binit(&b);
    77  	binit(&in);
    78  	binit(&out);
    79  	vinit(&lines);
    80  
    81  	ch = dir[xstrlen(dir)-2];
    82  	bprintf(&b, "%s/../%cl/%c.out.h", dir, ch, ch);
    83  	readfile(&in, bstr(&b));
    84  	splitlines(&lines, bstr(&in));
    85  	bwritestr(&out, "char*	anames[] = {\n");
    86  	for(i=0; i<lines.len; i++) {
    87  		if(hasprefix(lines.p[i], "\tA")) {
    88  			p = xstrstr(lines.p[i], ",");
    89  			if(p)
    90  				*p = '\0';
    91  			p = xstrstr(lines.p[i], "\n");
    92  			if(p)
    93  				*p = '\0';
    94  			p = lines.p[i] + 2;
    95  			bwritestr(&out, bprintf(&b, "\t\"%s\",\n", p));
    96  		}
    97  	}
    98  	bwritestr(&out, "};\n");
    99  	writefile(&out, file, 0);
   100  
   101  	bfree(&b);
   102  	bfree(&in);
   103  	bfree(&out);
   104  	vfree(&lines);
   105  }