1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
/* 
 * $Id: sparc.c,v 1.7 2001/07/24 12:19:31 grubba Exp $ 
 * 
 * Machine code generator for sparc. 
 * 
 * Henrik Grubbström 20010720 
 */ 
 
#include "operators.h" 
 
#define ADD_CALL(X, DELAY_OK) do {                                    \ 
    INT32 delta_;                                                       \ 
    struct program *p_ = Pike_compiler->new_program;                    \ 
    INT32 off_ = p_->num_program;                                       \ 
    /* noop             */                                              \ 
    INT32 delay_ = 0x01000000;                                          \ 
                                                                        \ 
    if (DELAY_OK) {                                                     \ 
      /* Move the previous opcode to the delay-slot. */                 \ 
      delay_ = p_->program[--off_];                                     \ 
    } else {                                                            \ 
      add_to_program(0); /* Placeholder... */                           \ 
    }                                                                   \ 
    /* call X   */                                                      \ 
    delta_ = ((PIKE_OPCODE_T *)(X)) - (p_->program + off_);             \ 
    p_->program[off_] = 0x40000000 | (delta_ & 0x3fffffff);             \ 
    add_to_relocations(off_);                                           \ 
    add_to_program(delay_);                                             \ 
  } while(0) 
 
static void low_ins_f_byte(unsigned int b, int delay_ok) 
{ 
  void *addr = instrs[b].address; 
 
#ifdef PIKE_DEBUG 
  if(store_linenumbers && b<F_MAX_OPCODE) 
    ADD_COMPILED(b); 
#endif /* PIKE_DEBUG */ 
 
  b-=F_OFFSET; 
#ifdef PIKE_DEBUG 
  if(b>255) 
    Pike_error("Instruction too big %d\n",b); 
#endif 
     
  { 
    static int last_prog_id=-1; 
    static int last_num_linenumbers=-1; 
    if(last_prog_id != Pike_compiler->new_program->id || 
       last_num_linenumbers != Pike_compiler->new_program->num_linenumbers) 
    { 
      last_prog_id=Pike_compiler->new_program->id; 
      last_num_linenumbers = Pike_compiler->new_program->num_linenumbers; 
      UPDATE_PC(); 
      delay_ok = 1; 
    } 
  } 
 
#ifndef PIKE_DEBUG 
  /* This is not very pretty */ 
  switch(b) 
  { 
  case F_MAKE_ITERATOR - F_OFFSET: 
    { 
      extern void f_Iterator(INT32); 
      SET_REG(SPARC_REG_O0, 1); 
      delay_ok = 1; 
      addr = (void *)f_Iterator; 
    } 
    break; 
  case F_COMPL - F_OFFSET: addr=(void *)o_compl; break; 
  case F_LSH - F_OFFSET: addr=(void *)o_lsh; break; 
  case F_RSH - F_OFFSET: addr=(void *)o_rsh; break; 
  case F_ADD - F_OFFSET: 
    SET_REG(SPARC_REG_O0, 2); 
    delay_ok = 1; 
    addr = (void *)f_add; 
    break; 
  case F_SUBTRACT - F_OFFSET: addr = (void *)o_subtract; break; 
  case F_AND - F_OFFSET: addr=(void *)o_and; break; 
  case F_OR - F_OFFSET: addr=(void *)o_or; break; 
  case F_XOR - F_OFFSET: addr=(void *)o_xor; break; 
  case F_MULTIPLY - F_OFFSET: addr=(void *)o_multiply; break; 
  case F_DIVIDE - F_OFFSET: addr=(void *)o_divide; break; 
  case F_MOD - F_OFFSET: addr=(void *)o_mod; break; 
  case F_CAST - F_OFFSET: addr = (void *)f_cast; break; 
  case F_CAST_TO_INT - F_OFFSET: addr = (void *)o_cast_to_int; break; 
  case F_CAST_TO_STRING - F_OFFSET: addr = (void *)o_cast_to_string; break; 
  case F_RANGE - F_OFFSET: addr = (void *)o_range; break; 
  case F_SSCANF - F_OFFSET: addr=(void *)o_sscanf; break; 
  } 
#endif 
   
  ADD_CALL(instrs[b].address, delay_ok); 
} 
 
void ins_f_byte(unsigned int opcode) 
{ 
  low_ins_f_byte(opcode, 0); 
} 
 
void ins_f_byte_with_arg(unsigned int a,unsigned INT32 b) 
{ 
  SET_REG(SPARC_REG_O0, b); 
  low_ins_f_byte(a, 1); 
  return; 
} 
 
void ins_f_byte_with_2_args(unsigned int a, 
                            unsigned INT32 c, 
                            unsigned INT32 b) 
{ 
  SET_REG(SPARC_REG_O0, c); 
  SET_REG(SPARC_REG_O1, b); 
  low_ins_f_byte(a, 1); 
  return; 
} 
 
#define addstr(s, l) low_my_binary_strcat((s), (l), buf) 
#define adddata2(s,l) addstr((char *)(s),(l) * sizeof((s)[0])); 
 
void sparc_encode_program(struct program *p, struct dynamic_buffer_s *buf) 
{ 
  size_t prev = 0, rel; 
  /* De-relocate the program... */ 
  for (rel = 0; rel < p->num_relocations; rel++) { 
    size_t off = p->relocations[rel]; 
    INT32 opcode; 
#ifdef PIKE_DEBUG 
    if (off < prev) { 
      fatal("Relocations in bad order!\n"); 
    } 
#endif /* PIKE_DEBUG */ 
    adddata2(p->program + prev, off - prev); 
 
#ifdef PIKE_DEBUG 
    if ((p->program[off] & 0xc0000000) != 0x40000000) { 
      fatal("Bad relocation!\n"); 
    } 
#endif /* PIKE_DEBUG */ 
    /* Relocate to being relative to NULL */ 
    opcode = 0x40000000 | 
      ((p->program[off] + (((INT32)(p->program)>>2))) & 0x3fffffff); 
    adddata2(&opcode, 1); 
    prev = off+1; 
  } 
  adddata2(p->program + prev, p->num_program - prev); 
} 
 
void sparc_decode_program(struct program *p) 
{ 
  /* Relocate the program... */ 
  PIKE_OPCODE_T *prog = p->program; 
  INT32 delta = ((INT32)p->program)>>2; 
  size_t rel = p->num_relocations; 
  while (rel--) { 
#ifdef PIKE_DEBUG 
    if ((prog[p->relocations[rel]] & 0xc0000000) != 0x40000000) { 
      Pike_error("Bad relocation: %d, off:%d, opcode: 0x%08x\n", 
                 rel, p->relocations[rel], 
                 prog[p->relocations[rel]]); 
    } 
#endif /* PIKE_DEBUG */ 
    prog[p->relocations[rel]] = 0x40000000 | 
      (((prog[p->relocations[rel]] & 0x3fffffff) - delta) & 
       0x3fffffff); 
  } 
} 
 
/* 
 * Inline machine-code... 
 */ 
const unsigned INT32 sparc_flush_instruction_cache[] = { 
  /* 
   * On entry: 
   *    %o0 : void *ptr 
   *    %o1 : size_t nbytes 
   * 
   *    cmp     %o1, #1 
   * .l0: 
   *    flush   %o0+%o1 
   *    bge,a   .l0 
   *    subcc   %o1, 8, %o1 
   *    retl 
   *    or      %g0,%g0,%o0 
   */ 
  /* 1000 0000 1010 0000 0010 0000 0000 0000 */ 
  0x80a02000|(SPARC_REG_O1<<14)|1, 
  /* 1000 0001 1101 1000 0000 0000 0000 0000 */ 
  0x81d80000|(SPARC_REG_O0<<14)|(SPARC_REG_O1), 
  /* 0011 0110 1000 0000 0000 0000 0000 0000 */ 
  0x36800000|((-1)&0x3fffff), 
  /* 1000 0000 1010 0000 0010 0000 0000 0000 */ 
  0x80a02000|(SPARC_REG_O1<<25)|(SPARC_REG_O1<<14)|8, 
  /* 1000 0001 1100 0000 0010 0000 0000 0000 */ 
  0x81c02000|(SPARC_REG_O7<<14)|8, 
  /* 1000 0000 0001 0000 0000 0000 0000 0000 */ 
  0x80100000|(SPARC_REG_O0<<25), 
};