github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/cmd/9g/gobj.c (about)

     1  // Derived from Inferno utils/6c/swt.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6c/swt.c
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  #include <u.h>
    32  #include <libc.h>
    33  #include "gg.h"
    34  
    35  int
    36  dsname(Sym *s, int off, char *t, int n)
    37  {
    38  	Prog *p;
    39  
    40  	p = gins(ADATA, N, N);
    41  	p->from.type = D_OREG;
    42  	p->from.name = D_EXTERN;
    43  	p->from.offset = off;
    44  	p->from.reg = NREG;
    45  	p->from.sym = linksym(s);
    46  
    47  	p->reg = n;
    48  	
    49  	p->to.type = D_SCONST;
    50  	p->to.name = D_NONE;
    51  	p->to.reg = NREG;
    52  	p->to.offset = 0;
    53  	memmove(p->to.u.sval, t, n);
    54  	return off + n;
    55  }
    56  
    57  /*
    58   * make a refer to the data s, s+len
    59   * emitting DATA if needed.
    60   */
    61  void
    62  datastring(char *s, int len, Addr *a)
    63  {
    64  	Sym *sym;
    65  	
    66  	sym = stringsym(s, len);
    67  	a->type = D_OREG;
    68  	a->name = D_EXTERN;
    69  	a->etype = simtype[TINT];
    70  	a->offset = widthptr+widthint;  // skip header
    71  	a->reg = NREG;
    72  	a->sym = linksym(sym);
    73  	a->node = sym->def;
    74  }
    75  
    76  /*
    77   * make a refer to the string sval,
    78   * emitting DATA if needed.
    79   */
    80  void
    81  datagostring(Strlit *sval, Addr *a)
    82  {
    83  	Sym *sym;
    84  
    85  	sym = stringsym(sval->s, sval->len);
    86  	a->type = D_OREG;
    87  	a->name = D_EXTERN;
    88  	a->sym = linksym(sym);
    89  	a->reg = NREG;
    90  	a->node = sym->def;
    91  	a->offset = 0;  // header
    92  	a->etype = TSTRING;
    93  }
    94  
    95  void
    96  gdata(Node *nam, Node *nr, int wid)
    97  {
    98  	Prog *p;
    99  
   100  	if(nr->op == OLITERAL) {
   101  		switch(nr->val.ctype) {
   102  		case CTCPLX:
   103  			gdatacomplex(nam, nr->val.u.cval);
   104  			return;
   105  		case CTSTR:
   106  			gdatastring(nam, nr->val.u.sval);
   107  			return;
   108  		}
   109  	}
   110  	p = gins(ADATA, nam, nr);
   111  	p->reg = wid;
   112  }
   113  
   114  void
   115  gdatacomplex(Node *nam, Mpcplx *cval)
   116  {
   117  	Prog *p;
   118  	int w;
   119  
   120  	w = cplxsubtype(nam->type->etype);
   121  	w = types[w]->width;
   122  
   123  	p = gins(ADATA, nam, N);
   124  	p->reg = w;
   125  	p->to.type = D_FCONST;
   126  	p->to.u.dval = mpgetflt(&cval->real);
   127  
   128  	p = gins(ADATA, nam, N);
   129  	p->reg = w;
   130  	p->from.offset += w;
   131  	p->to.type = D_FCONST;
   132  	p->to.u.dval = mpgetflt(&cval->imag);
   133  }
   134  
   135  void
   136  gdatastring(Node *nam, Strlit *sval)
   137  {
   138  	Prog *p;
   139  	Node nod1;
   140  
   141  	p = gins(ADATA, nam, N);
   142  	datastring(sval->s, sval->len, &p->to);
   143  	p->reg = types[tptr]->width;
   144  	p->to.type = D_CONST;
   145  	p->to.etype = simtype[tptr];
   146  
   147  	nodconst(&nod1, types[TINT], sval->len);
   148  	p = gins(ADATA, nam, &nod1);
   149  	p->reg = widthint;
   150  	p->from.offset += widthptr;
   151  }
   152  
   153  int
   154  dstringptr(Sym *s, int off, char *str)
   155  {
   156  	Prog *p;
   157  
   158  	off = rnd(off, widthptr);
   159  	p = gins(ADATA, N, N);
   160  	p->from.type = D_OREG;
   161  	p->from.name = D_EXTERN;
   162  	p->from.sym = linksym(s);
   163  	p->from.offset = off;
   164  	p->reg = widthptr;
   165  
   166  	datastring(str, strlen(str)+1, &p->to);
   167  	p->to.type = D_CONST;
   168  	p->to.etype = simtype[TINT];
   169  	off += widthptr;
   170  
   171  	return off;
   172  }
   173  
   174  int
   175  dgostrlitptr(Sym *s, int off, Strlit *lit)
   176  {
   177  	Prog *p;
   178  
   179  	if(lit == nil)
   180  		return duintptr(s, off, 0);
   181  
   182  	off = rnd(off, widthptr);
   183  	p = gins(ADATA, N, N);
   184  	p->from.type = D_OREG;
   185  	p->from.name = D_EXTERN;
   186  	p->from.sym = linksym(s);
   187  	p->from.offset = off;
   188  	p->reg = widthptr;
   189  	datagostring(lit, &p->to);
   190  	p->to.type = D_CONST;
   191  	p->to.etype = simtype[TINT];
   192  	off += widthptr;
   193  
   194  	return off;
   195  }
   196  
   197  int
   198  dgostringptr(Sym *s, int off, char *str)
   199  {
   200  	int n;
   201  	Strlit *lit;
   202  
   203  	if(str == nil)
   204  		return duintptr(s, off, 0);
   205  
   206  	n = strlen(str);
   207  	lit = mal(sizeof *lit + n);
   208  	strcpy(lit->s, str);
   209  	lit->len = n;
   210  	return dgostrlitptr(s, off, lit);
   211  }
   212  
   213  int
   214  dsymptr(Sym *s, int off, Sym *x, int xoff)
   215  {
   216  	Prog *p;
   217  
   218  	off = rnd(off, widthptr);
   219  
   220  	p = gins(ADATA, N, N);
   221  	p->from.type = D_OREG;
   222  	p->from.name = D_EXTERN;
   223  	p->from.sym = linksym(s);
   224  	p->from.offset = off;
   225  	p->reg = widthptr;
   226  	p->to.type = D_CONST;
   227  	p->to.name = D_EXTERN;
   228  	p->to.sym = linksym(x);
   229  	p->to.offset = xoff;
   230  	off += widthptr;
   231  
   232  	return off;
   233  }
   234  
   235  void
   236  nopout(Prog *p)
   237  {
   238  	p->as = ANOP;
   239  }
   240