OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [gas-zippatch.patch] - Blame information for rev 206

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 202 dgisselq
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/archures.c binutils-2.27-zip/bfd/archures.c
2
--- binutils-2.27/bfd/archures.c        2016-08-03 03:36:50.000000000 -0400
3
+++ binutils-2.27-zip/bfd/archures.c    2017-01-04 14:22:45.000000000 -0500
4
@@ -525,6 +525,8 @@
5
 .#define bfd_mach_nios2r2      2
6
 .  bfd_arch_visium,    {* Visium *}
7
 .#define bfd_mach_visium       1
8
+.  bfd_arch_zip,       {* ZipCPU *}
9
+.#define bfd_mach_zip          0
10
 .  bfd_arch_last
11
 .  };
12
 */
13
@@ -655,6 +657,7 @@
14
 extern const bfd_arch_info_type bfd_xgate_arch;
15
 extern const bfd_arch_info_type bfd_z80_arch;
16
 extern const bfd_arch_info_type bfd_z8k_arch;
17
+extern const bfd_arch_info_type bfd_zip_arch;
18
 
19
 static const bfd_arch_info_type * const bfd_archures_list[] =
20
   {
21
@@ -744,6 +747,7 @@
22
     &bfd_xgate_arch,
23
     &bfd_z80_arch,
24
     &bfd_z8k_arch,
25
+    &bfd_zip_arch,
26
 #endif
27
 
28
 };
29
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/bfd-in2.h binutils-2.27-zip/bfd/bfd-in2.h
30
--- binutils-2.27/bfd/bfd-in2.h 2016-08-03 03:36:50.000000000 -0400
31
+++ binutils-2.27-zip/bfd/bfd-in2.h     2017-01-04 22:04:11.000000000 -0500
32
@@ -2336,6 +2336,8 @@
33
 #define bfd_mach_nios2r2       2
34
   bfd_arch_visium,     /* Visium */
35
 #define bfd_mach_visium        1
36
+  bfd_arch_zip,        /* ZipCPU */
37
+#define bfd_mach_zip           0
38
   bfd_arch_last
39
   };
40
 
41
@@ -6335,6 +6337,22 @@
42
   BFD_RELOC_VISIUM_HI16_PCREL,
43
   BFD_RELOC_VISIUM_LO16_PCREL,
44
   BFD_RELOC_VISIUM_IM16_PCREL,
45
+
46
+/* ZipCPU - 32 bit absolute value for LJMP instruction  */
47
+  BFD_RELOC_ZIP_VALUE,
48
+
49
+/* ZipCPU - 18-bit PC-relative offset for BRA (ADD #x,PC) instructions  */
50
+  BFD_RELOC_ZIP_BRANCH,
51
+
52
+/* ZipCPU value relocations  */
53
+  BFD_RELOC_ZIP_OPB_IMM,
54
+  BFD_RELOC_ZIP_OPB_OFFSET,
55
+  BFD_RELOC_ZIP_OPB_PCREL,
56
+  BFD_RELOC_ZIP_MOV_OFFSET,
57
+  BFD_RELOC_ZIP_MOV_PCREL,
58
+  BFD_RELOC_ZIP_LDI,
59
+  BFD_RELOC_ZIP_LLO,
60
+  BFD_RELOC_ZIP_BREV,
61
   BFD_RELOC_UNUSED };
62
 
63
 typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
64
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/config.bfd binutils-2.27-zip/bfd/config.bfd
65
--- binutils-2.27/bfd/config.bfd        2016-08-03 03:36:50.000000000 -0400
66
+++ binutils-2.27-zip/bfd/config.bfd    2016-12-31 17:11:00.961307172 -0500
67
@@ -1742,6 +1742,10 @@
68
     targ_underscore=yes
69
     ;;
70
 
71
+  zip*)
72
+    targ_defvec=zip_elf32_vec
73
+    ;;
74
+
75
   *-*-ieee*)
76
     targ_defvec=ieee_vec
77
     ;;
78
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/configure binutils-2.27-zip/bfd/configure
79
--- binutils-2.27/bfd/configure 2016-08-03 04:33:36.000000000 -0400
80
+++ binutils-2.27-zip/bfd/configure     2016-12-31 17:12:22.360697343 -0500
81
@@ -14542,6 +14542,7 @@
82
     xtensa_elf32_le_vec)        tb="$tb xtensa-isa.lo xtensa-modules.lo elf32-xtensa.lo elf32.lo $elf" ;;
83
     z80_coff_vec)               tb="$tb coff-z80.lo reloc16.lo $coffgen" ;;
84
     z8k_coff_vec)               tb="$tb coff-z8k.lo reloc16.lo $coff" ;;
85
+    zip_elf32_vec)              tb="$tb elf32-zip.lo elf32.lo $elf" ;;
86
 
87
     # These appear out of order in targets.c
88
     srec_vec)                   tb="$tb srec.lo" ;;
89
@@ -14924,6 +14925,9 @@
90
   x86_64-*-netbsd* | x86_64-*-openbsd*)
91
        COREFILE=netbsd-core.lo
92
        ;;
93
+  zip*)
94
+       COREFILE=netbsd-core.lo
95
+       ;;
96
   esac
97
 
98
   case "$COREFILE" in
99
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/configure.ac binutils-2.27-zip/bfd/configure.ac
100
--- binutils-2.27/bfd/configure.ac      2016-08-03 03:36:50.000000000 -0400
101
+++ binutils-2.27-zip/bfd/configure.ac  2016-12-31 17:13:38.600136486 -0500
102
@@ -717,6 +717,7 @@
103
     xtensa_elf32_le_vec)        tb="$tb xtensa-isa.lo xtensa-modules.lo elf32-xtensa.lo elf32.lo $elf" ;;
104
     z80_coff_vec)               tb="$tb coff-z80.lo reloc16.lo $coffgen" ;;
105
     z8k_coff_vec)               tb="$tb coff-z8k.lo reloc16.lo $coff" ;;
106
+    zip_elf32_vec)              tb="$tb elf32-zip.lo elf32.lo $elf" ;;
107
 
108
     # These appear out of order in targets.c
109
     srec_vec)                   tb="$tb srec.lo" ;;
110
@@ -1092,6 +1093,9 @@
111
   x86_64-*-netbsd* | x86_64-*-openbsd*)
112
        COREFILE=netbsd-core.lo
113
        ;;
114
+  zip*)
115
+       COREFILE=netbsd-core.lo
116
+       ;;
117
   esac
118
 
119
   case "$COREFILE" in
120
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/cpu-zip.c binutils-2.27-zip/bfd/cpu-zip.c
121
--- binutils-2.27/bfd/cpu-zip.c 1969-12-31 19:00:00.000000000 -0500
122
+++ binutils-2.27-zip/bfd/cpu-zip.c     2017-01-04 14:26:27.000000000 -0500
123
@@ -0,0 +1,65 @@
124
+////////////////////////////////////////////////////////////////////////////////
125
+//
126
+// Filename:   tc-zip.c
127
+//
128
+// Project:    Zip CPU backend for GNU Binutils
129
+//
130
+// Purpose:    BFD support for the Zip CPU architecture.
131
+//
132
+//             This file is part of BFD, the Binary File Descriptor library.
133
+//
134
+// Creator:    Dan Gisselquist, Ph.D.
135
+//             Gisselquist Technology, LLC
136
+//
137
+////////////////////////////////////////////////////////////////////////////////
138
+//
139
+// Copyright (C) 2016-2017, Gisselquist Technology, LLC
140
+//
141
+// This program is free software (firmware): you can redistribute it and/or
142
+// modify it under the terms of  the GNU General Public License as published
143
+// by the Free Software Foundation, either version 3 of the License, or (at
144
+// your option) any later version.
145
+//
146
+// This program is distributed in the hope that it will be useful, but WITHOUT
147
+// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
148
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
149
+// for more details.
150
+//
151
+// You should have received a copy of the GNU General Public License along
152
+// with this program.  (It's in the $(ROOT)/doc directory, run make with no
153
+// target there if the PDF file isn't present.)  If not, see
154
+// <http://www.gnu.org/licenses/> for a copy.
155
+//
156
+// License:    GPL, v3, as defined and found on www.gnu.org,
157
+//             http://www.gnu.org/licenses/gpl.html
158
+//
159
+//
160
+////////////////////////////////////////////////////////////////////////////////
161
+#include "sysdep.h"
162
+#include "bfd.h"
163
+#include "libbfd.h"
164
+
165
+const bfd_arch_info_type
166
+bfd_zip_arch =
167
+{
168
+  32,                          // There's 32 bits_per_word.
169
+  32,                          // There's 34 bits_per_address.
170
+  8,                           // There's 32 bits_per_byte.
171
+  bfd_arch_zip,                        // One of enum bfd_architecture, defined
172
+                               // in archures.c and provided in
173
+                               // generated header files.
174
+  bfd_mach_zip,                        // Random BFD-internal number for this
175
+                               // machine, similarly listed in
176
+                               // archures.c.  Not emitted in output.
177
+  "zip",                       // The arch_name.
178
+  "zip",                       // The printable name is the same.
179
+  2,                           // Section alignment power; each section
180
+                               // is aligned to (only) 2^2 (i.e. 4) bytes.
181
+  TRUE,                                // This is the default "machine".
182
+  bfd_default_compatible,      // Architecture comparison function
183
+  bfd_default_scan,            // String to architecture conversion
184
+  bfd_arch_default_fill,       // Default fill.
185
+  NULL                         // Pointer to next bfd_arch_info_type in
186
+                               // the same family.
187
+};
188
+
189
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/doc/archures.texi binutils-2.27-zip/bfd/doc/archures.texi
190
--- binutils-2.27/bfd/doc/archures.texi 2016-08-03 04:36:22.000000000 -0400
191
+++ binutils-2.27-zip/bfd/doc/archures.texi     2016-12-31 17:14:43.103668704 -0500
192
@@ -492,6 +492,8 @@
193
 #define bfd_mach_nios2r2       2
194
   bfd_arch_visium,     /* Visium */
195
 #define bfd_mach_visium        1
196
+  bfd_mach_zip,
197
+#define bfd_mach_zip           0
198
   bfd_arch_last
199
   @};
200
 @end example
201
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/doc/bfd.info binutils-2.27-zip/bfd/doc/bfd.info
202
--- binutils-2.27/bfd/doc/bfd.info      2016-08-03 04:36:22.000000000 -0400
203
+++ binutils-2.27-zip/bfd/doc/bfd.info  2017-01-04 14:40:21.000000000 -0500
204
@@ -8466,6 +8466,8 @@
205
      #define bfd_mach_nios2r2       2
206
        bfd_arch_visium,     /* Visium */
207
      #define bfd_mach_visium        1
208
+       bfd_arch_zip,        /* ZipCPU */
209
+     #define bfd_mach_zip           0
210
        bfd_arch_last
211
        };
212
 
213
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/doc/reloc.texi binutils-2.27-zip/bfd/doc/reloc.texi
214
--- binutils-2.27/bfd/doc/reloc.texi    2016-08-03 04:36:22.000000000 -0400
215
+++ binutils-2.27-zip/bfd/doc/reloc.texi        2016-12-31 17:17:15.950640091 -0500
216
@@ -4214,6 +4214,19 @@
217
 @deffnx {} BFD_RELOC_VISIUM_IM16_PCREL
218
 Visium Relocations.
219
 @end deffn
220
+@deffn {} BFD_RELOC_ZIP_VALUE
221
+@deffnx {} BFD_RELOC_OPB_IMM
222
+@deffnx {} BFD_RELOC_OPB_OFFSET
223
+@deffnx {} BFD_RELOC_OPB_PCREL
224
+@deffnx {} BFD_RELOC_OPB_GOTREL
225
+@deffnx {} BFD_RELOC_MOV_OFFSET
226
+@deffnx {} BFD_RELOC_MOV_PCREL
227
+@deffnx {} BFD_RELOC_MOV_GOTREL
228
+@deffnx {} BFD_RELOC_ZIP_LDI
229
+@deffnx {} BFD_RELOC_ZIP_LLO
230
+@deffnx {} BFD_RELOC_ZIP_LHI
231
+ZipCPU relocations
232
+@end deffn
233
 
234
 @example
235
 
236
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/elf32-zip.c binutils-2.27-zip/bfd/elf32-zip.c
237
--- binutils-2.27/bfd/elf32-zip.c       1969-12-31 19:00:00.000000000 -0500
238
+++ binutils-2.27-zip/bfd/elf32-zip.c   2017-01-24 13:54:25.214097101 -0500
239
@@ -0,0 +1,1134 @@
240
+////////////////////////////////////////////////////////////////////////////////
241
+//
242
+// Filename:   tc-zip.c
243
+//
244
+// Project:    Zip CPU backend for GNU Binutils
245
+//
246
+// Purpose:    Zip-specific support for 32-bit ELF.
247
+//
248
+//     This file is part of BFD, the Binary File Descriptor library.
249
+//
250
+// Creator:    Dan Gisselquist, Ph.D.
251
+//             Gisselquist Technology, LLC
252
+//
253
+////////////////////////////////////////////////////////////////////////////////
254
+//
255
+// Copyright (C) 2016, Gisselquist Technology, LLC
256
+//
257
+// This program is free software (firmware): you can redistribute it and/or
258
+// modify it under the terms of  the GNU General Public License as published
259
+// by the Free Software Foundation, either version 3 of the License, or (at
260
+// your option) any later version.
261
+//
262
+// This program is distributed in the hope that it will be useful, but WITHOUT
263
+// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
264
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
265
+// for more details.
266
+//
267
+// You should have received a copy of the GNU General Public License along
268
+// with this program.  (It's in the $(ROOT)/doc directory, run make with no
269
+// target there if the PDF file isn't present.)  If not, see
270
+// <http://www.gnu.org/licenses/> for a copy.
271
+//
272
+// License:    GPL, v3, as defined and found on www.gnu.org,
273
+//             http://www.gnu.org/licenses/gpl.html
274
+//
275
+//
276
+////////////////////////////////////////////////////////////////////////////////
277
+#include "sysdep.h"
278
+#include "bfd.h"
279
+#include "bfdlink.h"
280
+#include "libbfd.h"
281
+#include "elf-bfd.h"
282
+#include "elf/zip.h"
283
+#include <limits.h>
284
+#include <stdint.h>
285
+
286
+#define        zip_relocation  bfd_elf_generic_reloc
287
+
288
+static bfd_reloc_status_type
289
+zip_brev_relocation(bfd *, arelent *, asymbol *, void *, asection *,
290
+               bfd *, char **error_messsage);
291
+static uint32_t zip_bitreverse(uint32_t v);
292
+
293
+/* Forward declarations.  */
294
+static reloc_howto_type zip_elf_howto_table [] =
295
+{
296
+  /* This reloc does nothing.  */
297
+  HOWTO (R_ZIP_NONE,           /* type */
298
+        0,                     /* rightshift */
299
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
300
+        32,                    /* bitsize */
301
+        FALSE,                 /* pc_relative */
302
+        0,                     /* bitpos */
303
+        complain_overflow_dont, /* complain_on_overflow */
304
+        zip_relocation,        /* special_function */
305
+        "R_ZIP_NONE",          /* name */
306
+        FALSE,                 /* partial_inplace */
307
+        0,                     /* src_mask */
308
+        0,                     /* dst_mask */
309
+        FALSE),                /* pcrel_offset */
310
+
311
+  /* A 32 bit absolute relocation.  */
312
+  HOWTO (R_ZIP_VALUE,          /* type */
313
+        0,                     /* rightshift */
314
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
315
+        32,                    /* bitsize */
316
+        FALSE,                 /* pc_relative */
317
+        0,                     /* bitpos */
318
+        complain_overflow_dont, /* complain_on_overflow */
319
+        zip_relocation,        /* special_function */
320
+        "R_ZIP_VALUE",         /* name */
321
+        FALSE,                 /* partial_inplace */
322
+        0x00000000,            /* src_mask */
323
+        0xffffffff,            /* dst_mask */
324
+        FALSE),                /* pcrel_offset */
325
+
326
+  HOWTO (R_ZIP_BREV,           /* type -- LDIHI, but with bitreverse */
327
+        0,                     /* rightshift */
328
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
329
+        16,                    /* bitsize */
330
+        FALSE,                 /* pc_relative */
331
+        0,                     /* bitpos */
332
+        complain_overflow_bitfield, /* complain_on_overflow */
333
+        zip_brev_relocation,    /* special_function--needed for the bitreverse */
334
+        "R_ZIP_BREV",          /* name */
335
+        FALSE,                 /* partial_inplace */
336
+        0,                     /* src_mask */
337
+        0x0003ffff,            /* dst_mask */
338
+        FALSE),                /* pcrel_offset */
339
+
340
+  HOWTO (R_ZIP_LLO,            /* type */
341
+        0,                     /* rightshift */
342
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
343
+        23,                    /* bitsize */
344
+        FALSE,                 /* pc_relative */
345
+        0,                     /* bitpos */
346
+        complain_overflow_dont, /* don't complain_on_overflow */
347
+        zip_relocation,         /* special_function */
348
+        "R_ZIP_LLO",           /* name */
349
+        FALSE,                 /* partial_inplace */
350
+        0,                     /* src_mask */
351
+        0x0000ffff,            /* dst_mask */
352
+        FALSE),                /* pcrel_offset */
353
+
354
+  HOWTO (R_ZIP_LDI,            /* type */
355
+        0,                     /* rightshift */
356
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
357
+        23,                    /* bitsize */
358
+        FALSE,                 /* pc_relative */
359
+        0,                     /* bitpos */
360
+        complain_overflow_signed, /* complain_on_overflow */
361
+        zip_relocation,         /* special_function */
362
+        "R_ZIP_LDI",           /* name */
363
+        FALSE,                 /* partial_inplace */
364
+        0,                     /* src_mask */
365
+        0x007fffff,            /* dst_mask */
366
+        FALSE),                /* pcrel_offset */
367
+
368
+  /* An 18 bit pc-relative relocation.  */
369
+  HOWTO (R_ZIP_BRANCH,         /* type */
370
+        0,                     /* rightshift */
371
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
372
+        18,                    /* bitsize */
373
+        TRUE,                  /* pc_relative */
374
+        0,                     /* bitpos */
375
+        complain_overflow_signed, /* complain_on_overflow */
376
+        zip_relocation,        /* special_function */
377
+        "R_ZIP_BRANCH",        /* name */
378
+        FALSE,                 /* partial_inplace */
379
+        0x00000000,            /* src_mask */
380
+        0x0003fffc,            /* dst_mask */
381
+        TRUE),         /* pcrel_offset */
382
+
383
+  /* An 18 bit operand B immediate.  */
384
+  HOWTO (R_ZIP_OPB_IMM,        /* type */
385
+        0,                     /* rightshift */
386
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
387
+        18,                    /* bitsize */
388
+        FALSE,                 /* pc_relative */
389
+        0,                     /* bitpos */
390
+        complain_overflow_signed, /* complain_on_overflow */
391
+        zip_relocation,        /* special_function */
392
+        "R_ZIP_OPB_IMM",       /* name */
393
+        FALSE,                 /* partial_inplace */
394
+        0x00000000,            /* src_mask */
395
+        0x0003ffff,            /* dst_mask */
396
+        FALSE),                        /* pcrel_offset */
397
+
398
+  /* An 18 bit relocation.  */
399
+  HOWTO (R_ZIP_OPB_OFFSET,     /* type */
400
+        0,                     /* rightshift */
401
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
402
+        14,                    /* bitsize */
403
+        FALSE,                 /* pc_relative */
404
+        0,                     /* bitpos */
405
+        complain_overflow_signed, /* complain_on_overflow */
406
+        zip_relocation,        /* special_function */
407
+        "R_ZIP_OPB_OFFSET",    /* name */
408
+        FALSE,                 /* partial_inplace */
409
+        0x00000000,            /* src_mask */
410
+        0x00003fff,            /* dst_mask-14 bits */
411
+        FALSE),                        /* pcrel_offset */
412
+
413
+  /* An 18 bit operand B immediate, but relative to the current PC.  */
414
+  HOWTO (R_ZIP_OPB_PCREL,      /* type */
415
+        2,                     /* rightshift */
416
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
417
+        14,                    /* bitsize */
418
+        TRUE,                  /* pc_relative */
419
+        0,                     /* bitpos */
420
+        complain_overflow_signed, /* complain_on_overflow */
421
+        zip_relocation,        /* special_function */
422
+        "R_ZIP_OPB_PCREL",     /* name */
423
+        FALSE,                 /* partial_inplace */
424
+        0x00000000,            /* src_mask */
425
+        0x00003fff,            /* dst_mask-14 bits */
426
+        TRUE),                 /* pcrel_offset */
427
+
428
+  /* An 18 bit operand B immediate, but relative to the Global Offset Table. */
429
+  //HOWTO (R_ZIP_OPB_GOTREL,   /* type */
430
+        //0,                   /* rightshift */
431
+        //2,                   /* size (0 = byte, 1 = short, 2 = long) */
432
+        //18,                  /* bitsize */
433
+        //FALSE,                       /* pc_relative */
434
+        //0,                   /* bitpos */
435
+        //complain_overflow_signed, /* complain_on_overflow */
436
+        //zip_relocation,      /* special_function */
437
+        //"R_ZIP_OPB_GOTREL",  /* name */
438
+        //FALSE,                       /* partial_inplace */
439
+        //0x00000000,          /* src_mask */
440
+        //0x0003ffff,          /* dst_mask-14 bits */
441
+        //FALSE),              /* pcrel_offset */
442
+
443
+  /* */
444
+  HOWTO (R_ZIP_MOV_OFFSET,     /* type */
445
+        0,                     /* rightshift */
446
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
447
+        13,                    /* bitsize */
448
+        FALSE,                 /* pc_relative */
449
+        0,                     /* bitpos */
450
+        complain_overflow_signed, /* complain_on_overflow */
451
+        zip_relocation,         /* special_function */
452
+        "R_ZIP_MOV_OFFSET",    /* name */
453
+        FALSE,                 /* partial_inplace */
454
+        0,                     /* src_mask */
455
+        0x00001fff,            /* dst_mask */
456
+        FALSE),                /* pcrel_offset */
457
+
458
+  /* */
459
+  HOWTO (R_ZIP_MOV_PCREL,      /* type */
460
+        2,                     /* rightshift */
461
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
462
+        13,                    /* bitsize */
463
+        TRUE,                  /* pc_relative */
464
+        0,                     /* bitpos */
465
+        complain_overflow_signed, /* complain_on_overflow */
466
+        zip_relocation,         /* special_function */
467
+        "R_ZIP_MOV_PCREL",     /* name */
468
+        FALSE,                 /* partial_inplace */
469
+        0,                     /* src_mask */
470
+        0x00001fff,            /* dst_mask */
471
+        TRUE)                  /* pcrel_offset */
472
+
473
+  /* */
474
+  //HOWTO (R_ZIP_MOV_GOTREL,   /* type */
475
+        //0,                   /* rightshift */
476
+        //2,                   /* size (0 = byte, 1 = short, 2 = long) */
477
+        //13,                  /* bitsize */
478
+        //FALSE,                       /* pc_relative */
479
+        //0,                   /* bitpos */
480
+        //complain_overflow_signed, /* complain_on_overflow */
481
+        //zip_relocation,       /* special_function */
482
+        //"R_ZIP_MOV_GOTREL",  /* name */
483
+        //FALSE,                       /* partial_inplace */
484
+        //0,                   /* src_mask */
485
+        //0x00001fff,          /* dst_mask */
486
+        //FALSE),              /* pcrel_offset */
487
+
488
+};
489
+
490
+/* This structure is used to map BFD reloc codes to Zip ELF relocations */
491
+
492
+struct elf_reloc_map
493
+{
494
+  bfd_reloc_code_real_type bfd_reloc_val;
495
+  unsigned int elf_reloc_val;
496
+};
497
+
498
+static const struct elf_reloc_map zip_reloc_map [] =
499
+{
500
+  { BFD_RELOC_NONE,            R_ZIP_NONE },
501
+  { BFD_RELOC_ZIP_VALUE,       R_ZIP_VALUE },
502
+  { BFD_RELOC_ZIP_BRANCH,      R_ZIP_BRANCH },
503
+  { BFD_RELOC_ZIP_OPB_IMM,     R_ZIP_OPB_IMM },
504
+  { BFD_RELOC_ZIP_OPB_OFFSET,  R_ZIP_OPB_OFFSET },
505
+  { BFD_RELOC_ZIP_MOV_OFFSET,  R_ZIP_MOV_OFFSET },
506
+  { BFD_RELOC_ZIP_LDI,         R_ZIP_LDI },
507
+  { BFD_RELOC_ZIP_LLO,         R_ZIP_LLO },
508
+  { BFD_RELOC_ZIP_BREV,                R_ZIP_BREV },
509
+  { BFD_RELOC_14,              R_ZIP_OPB_OFFSET },
510
+  { BFD_RELOC_16,              R_ZIP_LLO },
511
+  { BFD_RELOC_32,              R_ZIP_VALUE },
512
+  { BFD_RELOC_ZIP_OPB_PCREL,   R_ZIP_OPB_PCREL },
513
+  { BFD_RELOC_ZIP_MOV_PCREL,   R_ZIP_MOV_PCREL }
514
+  // { BFD_RELOC_ZIP_OPB_GOTREL,       R_ZIP_OPB_GOTREL },
515
+  // { BFD_RELOC_ZIP_MOV_GOTREL,       R_ZIP_MOV_GOTREL },
516
+};
517
+
518
+/* Given a BFD reloc code, return the howto structure for the corresponding
519
+ * Zip ELF relocation. */
520
+static reloc_howto_type *
521
+zip_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
522
+                       bfd_reloc_code_real_type code)
523
+{
524
+  unsigned int i;
525
+
526
+  for (i = 0; i < sizeof (zip_reloc_map) / sizeof (zip_reloc_map[0]); i++)
527
+    if (zip_reloc_map [i].bfd_reloc_val == code)
528
+      return & zip_elf_howto_table [(int)zip_reloc_map[i].elf_reloc_val];
529
+
530
+  return NULL;
531
+}
532
+
533
+static reloc_howto_type *
534
+zip_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
535
+{
536
+  unsigned int i;
537
+
538
+  for (i = 0;
539
+       i < sizeof (zip_elf_howto_table) / sizeof (zip_elf_howto_table[0]);
540
+       i++)
541
+    if (zip_elf_howto_table[i].name != NULL
542
+       && strcasecmp (zip_elf_howto_table[i].name, r_name) == 0)
543
+      return &zip_elf_howto_table[i];
544
+
545
+  return NULL;
546
+}
547
+
548
+/* Given an ELF reloc, fill in the howto field of a relent. */
549
+static void
550
+zip_elf_info_to_howto(bfd * abfd ATTRIBUTE_UNUSED,
551
+                        arelent * cache_ptr,
552
+                        Elf_Internal_Rela * dst)
553
+{
554
+       unsigned int r;
555
+
556
+       r = ELF32_R_TYPE(dst->r_info);
557
+       BFD_ASSERT (r < (unsigned int) R_ZIP_max);
558
+       cache_ptr->howto = &zip_elf_howto_table [r];
559
+}
560
+
561
+static bfd_boolean
562
+zip_elf_relocate_section(bfd *output_bfd,
563
+                       struct bfd_link_info *info,
564
+                       bfd *input_bfd,
565
+                       asection *input_section,
566
+                       bfd_byte *contents,
567
+                       Elf_Internal_Rela *relocs,
568
+                       Elf_Internal_Sym *local_syms,
569
+                       asection **local_sections)
570
+{
571
+       Elf_Internal_Shdr               *symtab_hdr;
572
+       struct  elf_link_hash_entry     **sym_hashes;
573
+       Elf_Internal_Rela               *rel, *relend;
574
+
575
+       symtab_hdr = &elf_tdata(input_bfd)->symtab_hdr;
576
+       sym_hashes = elf_sym_hashes(input_bfd);
577
+       relend     = relocs+input_section->reloc_count;
578
+
579
+       for(rel=relocs; rel<relend; rel++) {
580
+               reloc_howto_type                *howto;
581
+               unsigned long                   r_symndx;
582
+               Elf_Internal_Sym                *sym;
583
+               asection                        *sec;
584
+               struct elf_link_hash_entry      *h;
585
+               bfd_vma                         relocation;
586
+               bfd_reloc_status_type           r;
587
+               const char                      *name = NULL;
588
+               int                             r_type;
589
+
590
+               r_type = ELF32_R_TYPE(rel->r_info);
591
+               r_symndx = ELF32_R_SYM(rel->r_info);
592
+
593
+               if ((r_type < 0) || (r_type >= (int)R_ZIP_max))
594
+               {
595
+                       bfd_set_error(bfd_error_bad_value);
596
+                       return FALSE;
597
+               }
598
+
599
+               howto = zip_elf_howto_table + ELF32_R_TYPE(rel->r_info);
600
+               h = NULL;
601
+               sym = NULL;
602
+               sec = NULL;
603
+
604
+               if (r_symndx < symtab_hdr->sh_info)
605
+               {
606
+                       sym = local_syms + r_symndx;
607
+                       sec = local_sections[r_symndx];
608
+                       relocation = _bfd_elf_rela_local_sym(output_bfd, sym, &sec, rel);
609
+                       name = bfd_elf_string_from_elf_section
610
+                               (input_bfd, symtab_hdr->sh_link, sym->st_name);
611
+                       name = (name == NULL) ? bfd_section_name(input_bfd, sec)
612
+                                       : name;
613
+               } else {
614
+                       bfd_boolean unresolved_reloc, warned, ignored;
615
+
616
+                       RELOC_FOR_GLOBAL_SYMBOL(info, input_bfd, input_section,
617
+                               rel, r_symndx, symtab_hdr, sym_hashes, h, sec,
618
+                               relocation, unresolved_reloc, warned, ignored);
619
+               }
620
+
621
+               if ((sec != NULL)&&(discarded_section(sec))) {
622
+                       RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd,
623
+                               input_section, rel, 1, relend, howto, 0,
624
+                               contents);
625
+               }
626
+
627
+               if (bfd_link_relocatable(info))
628
+                       continue;
629
+
630
+               if (howto->type == R_ZIP_BREV) {
631
+                       if (rel->r_offset > bfd_get_section_limit(input_bfd, input_section)) {
632
+                               r = bfd_reloc_outofrange;
633
+                       } else {
634
+                               uint32_t        brev_reloc;
635
+                               bfd_byte        *location;
636
+                               bfd_vma         insn;
637
+
638
+                               location = contents + rel->r_offset * bfd_octets_per_byte(input_bfd);
639
+
640
+                               relocation += rel->r_addend;
641
+                               brev_reloc= zip_bitreverse(relocation);
642
+                               insn = bfd_get_32(input_bfd, location);
643
+                               insn = ((insn & ~howto->dst_mask)
644
+                               |(((insn & howto->src_mask)+brev_reloc)&howto->dst_mask));
645
+                               bfd_put_32(input_bfd, insn, location);
646
+                               r = bfd_reloc_ok;
647
+                       }
648
+               } else {
649
+                       r = _bfd_final_link_relocate(howto, input_bfd,
650
+                               input_section,
651
+                               contents, rel->r_offset,
652
+                               relocation,
653
+                               rel->r_addend);
654
+               }
655
+
656
+
657
+               if (r != bfd_reloc_ok)
658
+               {
659
+                       const char *msg = NULL;
660
+
661
+                       switch(r)
662
+                       {
663
+                               case bfd_reloc_overflow:
664
+                                       info->callbacks->reloc_overflow(
665
+                                               info, (h?&h->root:NULL),
666
+                                               name, howto->name,
667
+                                               (bfd_vma)0, input_bfd,
668
+                                               input_section, rel->r_offset);
669
+                                       break;
670
+                               case bfd_reloc_undefined:
671
+                                       info->callbacks->undefined_symbol(
672
+                                               info, name, input_bfd,
673
+                                               input_section, rel->r_offset,
674
+                                               TRUE);
675
+                                       break;
676
+                               case bfd_reloc_outofrange:
677
+                                       msg = _("internal error: out of range error");
678
+                                       break;
679
+                               case bfd_reloc_notsupported:
680
+                                       msg = _("internal error: unsupported relocation");
681
+                                       break;
682
+                               case bfd_reloc_dangerous:
683
+                                       msg = _("internal error: dangerous relocation");
684
+                                       break;
685
+                               default:
686
+                                       msg = _("internal error: unknown error");
687
+                                       break;
688
+                       }
689
+
690
+                       if (msg)
691
+                               info->callbacks->warning(info, msg, name,
692
+                                       input_bfd, input_section,
693
+                                       rel->r_offset);
694
+
695
+                       if (!r)
696
+                               return FALSE;
697
+               }
698
+       }
699
+       return TRUE;
700
+}
701
+
702
+static uint32_t
703
+zip_bitreverse(uint32_t v) {
704
+       unsigned r = 0, b;
705
+
706
+       for(b=0; b<32; b++, v>>=1)
707
+               r = (r<<1)|(v&1);
708
+
709
+       return r;
710
+}
711
+
712
+static bfd_reloc_status_type
713
+zip_brev_relocation(bfd *abfd,
714
+               arelent *reloc_entry,
715
+               asymbol *symbol,
716
+               void *data,
717
+               asection *input_section,
718
+               bfd *output_bfd,
719
+               char **error_message)
720
+{
721
+       bfd_vma relocation;
722
+       bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte(abfd);
723
+       bfd_vma output_base = 0;
724
+       reloc_howto_type *howto = reloc_entry->howto;
725
+
726
+       // If this isn't a final relocation, then just use the generic
727
+       // relocation function.
728
+       if (output_bfd != NULL) {
729
+               return zip_relocation(abfd, reloc_entry, symbol, data,
730
+                       input_section, output_bfd, error_message);
731
+       }
732
+
733
+       // Otherwise, we need to adjust our file itself with this value ...
734
+       // Check that our relocation lies within the file, and particularly
735
+       // the section we think it should.  (This should really be an assert...)
736
+       if (reloc_entry->address > bfd_get_section_limit(abfd, input_section))
737
+               return bfd_reloc_outofrange;
738
+
739
+       // Get symbol value
740
+       if (bfd_is_com_section(symbol->section))
741
+               relocation = 0;
742
+       else
743
+               relocation = symbol->value;
744
+
745
+       /* Convert input-section relative symbol value to absolute */
746
+       if ((!howto->partial_inplace)
747
+                       ||(symbol->section->output_section == NULL))
748
+               output_base = 0;
749
+       else
750
+               output_base = symbol->section->output_section->vma;
751
+
752
+       relocation += output_base + symbol->section->output_offset;
753
+
754
+       /* Add in supplied addend. */
755
+       relocation += reloc_entry->addend;
756
+
757
+       // BREV does not handle PC relative offsets
758
+
759
+       // Ignore overflow checking ... BREV handles the top 18 bits of a 32-bit
760
+       // number.  Overflow would mean overflowing the 32-bit address space--
761
+       // not possible.
762
+       //
763
+       // if howto->complain_on_overflow ...
764
+
765
+       // relocation >>= howto->rightshift;    // = 0
766
+       // relocation <<= howto->bitpos;        // = 0
767
+
768
+       // Logic (nearly) copied from reloc.c:bfd_perform_relocation
769
+       unsigned        insn = bfd_get_32(abfd, (bfd_byte *)data + octets);
770
+
771
+       // Here's why we are going through this pain!
772
+       insn = zip_bitreverse((unsigned)insn);
773
+
774
+       // Now we can continue as before....
775
+       insn = ((insn&(~howto->dst_mask))
776
+               |(((insn&howto->src_mask)+relocation)&howto->dst_mask));
777
+       bfd_put_32(abfd, (bfd_vma)insn, (bfd_byte *)data + octets);
778
+
779
+       return bfd_reloc_ok;
780
+}
781
+
782
+// Zip Defines
783
+#define        TARGET_BIG_SYM  zip_elf32_vec
784
+#define        TARGET_BIG_NAME "elf32-zip"
785
+
786
+#define        ELF_ARCH        bfd_arch_zip
787
+// #define ELF_TARGET_ID               ZIP_ELF_DATA
788
+#define        ELF_MACHINE_CODE        EM_ZIP
789
+
790
+#define        ELF_MAXPAGESIZE         0x1000
791
+#define        ARCH_SIZE               32
792
+
793
+
794
+#define bfd_elf32_bfd_define_common_symbol     bfd_generic_define_common_symbol
795
+
796
+
797
+#define        bfd_elf32_bfd_reloc_type_lookup zip_elf_reloc_type_lookup
798
+#define        bfd_elf32_bfd_reloc_name_lookup zip_elf_reloc_name_lookup
799
+#define        elf_info_to_howto_rel   0
800
+#define        elf_info_to_howto       zip_elf_info_to_howto
801
+#define        elf_backend_relocate_section    zip_elf_relocate_section
802
+#define        elf_backend_rela_normal         1
803
+
804
+// Default ELF32 defines from elf32-target.h that we would've normally included
805
+// here, had we not been a OCTETS_PER_BYTE=4 machine
806
+
807
+#define        bfd_elf32_close_and_cleanup _bfd_elf_close_and_cleanup
808
+#define bfd_elf32_bfd_free_cached_info _bfd_free_cached_info
809
+#define bfd_elf32_get_section_contents _bfd_generic_get_section_contents
810
+#define bfd_elf32_canonicalize_dynamic_symtab \
811
+  _bfd_elf_canonicalize_dynamic_symtab
812
+#define bfd_elf32_get_synthetic_symtab _bfd_elf_get_synthetic_symtab
813
+#define bfd_elf32_canonicalize_reloc   _bfd_elf_canonicalize_reloc
814
+#define bfd_elf32_find_nearest_line    _bfd_elf_find_nearest_line
815
+#define bfd_elf32_find_line            _bfd_elf_find_line
816
+#define bfd_elf32_find_inliner_info    _bfd_elf_find_inliner_info
817
+#define bfd_elf32_read_minisymbols     _bfd_elf_read_minisymbols
818
+#define bfd_elf32_minisymbol_to_symbol _bfd_elf_minisymbol_to_symbol
819
+#define bfd_elf32_get_dynamic_symtab_upper_bound \
820
+  _bfd_elf_get_dynamic_symtab_upper_bound
821
+#define bfd_elf32_get_lineno           _bfd_elf_get_lineno
822
+#define bfd_elf32_get_reloc_upper_bound _bfd_elf_get_reloc_upper_bound
823
+#define bfd_elf32_get_symbol_info      _bfd_elf_get_symbol_info
824
+#define bfd_elf32_get_symbol_version_string    _bfd_elf_get_symbol_version_string
825
+#define bfd_elf32_canonicalize_symtab  _bfd_elf_canonicalize_symtab
826
+#define bfd_elf32_get_symtab_upper_bound _bfd_elf_get_symtab_upper_bound
827
+#define bfd_elf32_make_empty_symbol    _bfd_elf_make_empty_symbol
828
+#define bfd_elf32_new_section_hook     _bfd_elf_new_section_hook
829
+#define bfd_elf32_set_arch_mach                _bfd_elf_set_arch_mach
830
+#define bfd_elf32_set_section_contents _bfd_elf_set_section_contents
831
+#define bfd_elf32_sizeof_headers       _bfd_elf_sizeof_headers
832
+#define bfd_elf32_write_object_contents _bfd_elf_write_object_contents
833
+#define bfd_elf32_write_corefile_contents _bfd_elf_write_corefile_contents
834
+
835
+#define bfd_elf32_get_section_contents_in_window \
836
+  _bfd_generic_get_section_contents_in_window
837
+
838
+#define elf_backend_can_refcount 0
839
+#define elf_backend_want_got_plt 0
840
+#define elf_backend_plt_readonly 0
841
+#define elf_backend_want_plt_sym 0
842
+#define elf_backend_plt_not_loaded 0
843
+#define elf_backend_plt_alignment 2
844
+#define elf_backend_want_dynbss 1
845
+#define elf_backend_want_p_paddr_set_to_zero 0
846
+#define elf_backend_default_execstack 1
847
+#define elf_backend_caches_rawsize 0
848
+#define elf_backend_extern_protected_data 0
849
+#define elf_backend_stack_align 4
850
+#define elf_backend_strtab_flags 0
851
+
852
+#define bfd_elf32_bfd_debug_info_start bfd_void
853
+#define bfd_elf32_bfd_debug_info_end   bfd_void
854
+#define bfd_elf32_bfd_debug_info_accumulate \
855
+  ((void (*) (bfd*, struct bfd_section *)) bfd_void)
856
+
857
+#define bfd_elf32_bfd_get_relocated_section_contents \
858
+  bfd_generic_get_relocated_section_contents
859
+
860
+#define bfd_elf32_bfd_relax_section bfd_generic_relax_section
861
+
862
+#define elf_backend_can_gc_sections 1
863
+#define elf_backend_can_refcount 0
864
+#define elf_backend_want_got_sym 1
865
+#define elf_backend_gc_keep            _bfd_elf_gc_keep
866
+#define elf_backend_gc_mark_dynamic_ref        bfd_elf_gc_mark_dynamic_ref_symbol
867
+#define elf_backend_gc_mark_hook       _bfd_elf_gc_mark_hook
868
+#define elf_backend_gc_mark_extra_sections _bfd_elf_gc_mark_extra_sections
869
+#define elf_backend_gc_sweep_hook      NULL
870
+#define bfd_elf32_bfd_gc_sections bfd_elf_gc_sections
871
+
872
+#ifndef bfd_elf32_bfd_merge_sections
873
+#define bfd_elf32_bfd_merge_sections _bfd_elf_merge_sections
874
+#endif
875
+
876
+#ifndef bfd_elf32_bfd_is_group_section
877
+#define bfd_elf32_bfd_is_group_section bfd_elf_is_group_section
878
+#endif
879
+
880
+#ifndef bfd_elf32_bfd_discard_group
881
+#define bfd_elf32_bfd_discard_group bfd_generic_discard_group
882
+#endif
883
+
884
+#ifndef bfd_elf32_section_already_linked
885
+#define bfd_elf32_section_already_linked _bfd_elf_section_already_linked
886
+#endif
887
+
888
+#ifndef bfd_elf32_bfd_define_common_symbol
889
+#define bfd_elf32_bfd_define_common_symbol bfd_generic_define_common_symbol
890
+#endif
891
+
892
+#ifndef bfd_elf32_bfd_lookup_section_flags
893
+#define bfd_elf32_bfd_lookup_section_flags bfd_elf_lookup_section_flags
894
+#endif
895
+
896
+#ifndef bfd_elf32_bfd_make_debug_symbol
897
+#define bfd_elf32_bfd_make_debug_symbol \
898
+  ((asymbol * (*) (bfd *, void *, unsigned long)) bfd_nullvoidptr)
899
+#endif
900
+
901
+#ifndef bfd_elf32_bfd_copy_private_symbol_data
902
+#define bfd_elf32_bfd_copy_private_symbol_data _bfd_elf_copy_private_symbol_data
903
+#endif
904
+
905
+#ifndef bfd_elf32_bfd_copy_private_section_data
906
+#define bfd_elf32_bfd_copy_private_section_data \
907
+  _bfd_elf_copy_private_section_data
908
+#endif
909
+#ifndef bfd_elf32_bfd_copy_private_header_data
910
+#define bfd_elf32_bfd_copy_private_header_data \
911
+  _bfd_elf_copy_private_header_data
912
+#endif
913
+#ifndef bfd_elf32_bfd_copy_private_bfd_data
914
+#define bfd_elf32_bfd_copy_private_bfd_data \
915
+  _bfd_elf_copy_private_bfd_data
916
+#endif
917
+#ifndef bfd_elf32_bfd_print_private_bfd_data
918
+#define bfd_elf32_bfd_print_private_bfd_data \
919
+  _bfd_elf_print_private_bfd_data
920
+#endif
921
+#ifndef bfd_elf32_bfd_merge_private_bfd_data
922
+#define bfd_elf32_bfd_merge_private_bfd_data \
923
+  ((bfd_boolean (*) (bfd *, bfd *)) bfd_true)
924
+#endif
925
+#ifndef bfd_elf32_bfd_set_private_flags
926
+#define bfd_elf32_bfd_set_private_flags \
927
+  ((bfd_boolean (*) (bfd *, flagword)) bfd_true)
928
+#endif
929
+#ifndef bfd_elf32_bfd_is_local_label_name
930
+#define bfd_elf32_bfd_is_local_label_name _bfd_elf_is_local_label_name
931
+#endif
932
+#ifndef bfd_elf32_bfd_is_target_special_symbol
933
+#define bfd_elf32_bfd_is_target_special_symbol \
934
+  ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
935
+#endif
936
+
937
+#ifndef bfd_elf32_get_dynamic_reloc_upper_bound
938
+#define bfd_elf32_get_dynamic_reloc_upper_bound \
939
+  _bfd_elf_get_dynamic_reloc_upper_bound
940
+#endif
941
+#ifndef bfd_elf32_canonicalize_dynamic_reloc
942
+#define bfd_elf32_canonicalize_dynamic_reloc _bfd_elf_canonicalize_dynamic_reloc
943
+#endif
944
+
945
+#define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
946
+#define bfd_elf32_bfd_link_add_symbols bfd_elf_link_add_symbols
947
+#define bfd_elf32_bfd_final_link       bfd_elf_final_link
948
+
949
+#define bfd_elf32_bfd_link_just_syms   _bfd_elf_link_just_syms
950
+
951
+#define bfd_elf32_bfd_copy_link_hash_symbol_type \
952
+  _bfd_elf_copy_link_hash_symbol_type
953
+
954
+#define bfd_elf32_bfd_link_split_section _bfd_generic_link_split_section
955
+#define bfd_elf32_bfd_link_check_relocs _bfd_generic_link_check_relocs
956
+#define bfd_elf32_archive_p bfd_generic_archive_p
957
+#define bfd_elf32_write_archive_contents _bfd_write_archive_contents
958
+#define bfd_elf32_mkobject bfd_elf_make_object
959
+#define bfd_elf32_mkcorefile bfd_elf_mkcorefile
960
+#define bfd_elf32_mkarchive _bfd_generic_mkarchive
961
+#define bfd_elf32_print_symbol bfd_elf_print_symbol
962
+#define elf_symbol_leading_char 0
963
+
964
+#define elf_backend_arch_data NULL
965
+
966
+#ifndef ELF_TARGET_ID
967
+#define ELF_TARGET_ID  GENERIC_ELF_DATA
968
+#endif
969
+
970
+#ifndef ELF_OSABI
971
+#define ELF_OSABI ELFOSABI_NONE
972
+#endif
973
+
974
+#define ELF_COMMONPAGESIZE ELF_MAXPAGESIZE
975
+#define ELF_MINPAGESIZE ELF_COMMONPAGESIZE
976
+
977
+#if ELF_COMMONPAGESIZE > ELF_MAXPAGESIZE
978
+# error ELF_COMMONPAGESIZE > ELF_MAXPAGESIZE
979
+#endif
980
+#if ELF_MINPAGESIZE > ELF_COMMONPAGESIZE
981
+# error ELF_MINPAGESIZE > ELF_COMMONPAGESIZE
982
+#endif
983
+
984
+#ifndef ELF_DYNAMIC_SEC_FLAGS
985
+/* Note that we set the SEC_IN_MEMORY flag for these sections.  */
986
+#define ELF_DYNAMIC_SEC_FLAGS                  \
987
+  (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS     \
988
+   | SEC_IN_MEMORY | SEC_LINKER_CREATED)
989
+#endif
990
+
991
+#define elf_backend_collect FALSE
992
+#define elf_backend_type_change_ok FALSE
993
+
994
+#define elf_backend_sym_is_global      0
995
+#define elf_backend_object_p           0
996
+#define elf_backend_symbol_processing  0
997
+#define elf_backend_symbol_table_processing    0
998
+#define elf_backend_get_symbol_type 0
999
+#define elf_backend_archive_symbol_lookup _bfd_elf_archive_symbol_lookup
1000
+#define elf_backend_name_local_section_symbols 0
1001
+#define elf_backend_section_processing 0
1002
+#define elf_backend_section_from_shdr  _bfd_elf_make_section_from_shdr
1003
+#define elf_backend_section_flags      0
1004
+#define elf_backend_get_sec_type_attr  _bfd_elf_get_sec_type_attr
1005
+#define elf_backend_section_from_phdr  _bfd_elf_make_section_from_phdr
1006
+#define elf_backend_fake_sections      0
1007
+#define elf_backend_section_from_bfd_section   0
1008
+#define elf_backend_add_symbol_hook    0
1009
+#define elf_backend_link_output_symbol_hook 0
1010
+#define elf_backend_create_dynamic_sections 0
1011
+#define elf_backend_omit_section_dynsym _bfd_elf_link_omit_section_dynsym
1012
+#define elf_backend_relocs_compatible _bfd_elf_default_relocs_compatible
1013
+#define elf_backend_check_relocs       0
1014
+#define elf_backend_check_directives   0
1015
+#define elf_backend_notice_as_needed   _bfd_elf_notice_as_needed
1016
+#define elf_backend_adjust_dynamic_symbol 0
1017
+#define elf_backend_always_size_sections 0
1018
+#define elf_backend_size_dynamic_sections 0
1019
+#define elf_backend_init_index_section \
1020
+ ((void (*) (bfd *, struct bfd_link_info *)) bfd_void)
1021
+#define elf_backend_finish_dynamic_symbol      0
1022
+#define elf_backend_finish_dynamic_sections    0
1023
+#define elf_backend_begin_write_processing     0
1024
+#define elf_backend_final_write_processing     0
1025
+#define elf_backend_additional_program_headers 0
1026
+#define elf_backend_modify_segment_map 0
1027
+#ifndef elf_backend_modify_program_headers
1028
+#define elf_backend_modify_program_headers     0
1029
+#endif
1030
+#ifndef elf_backend_ecoff_debug_swap
1031
+#define elf_backend_ecoff_debug_swap   0
1032
+#endif
1033
+#ifndef elf_backend_bfd_from_remote_memory
1034
+#define elf_backend_bfd_from_remote_memory _bfd_elf32_bfd_from_remote_memory
1035
+#endif
1036
+#ifndef elf_backend_got_header_size
1037
+#define elf_backend_got_header_size    0
1038
+#endif
1039
+#ifndef elf_backend_got_elt_size
1040
+#define elf_backend_got_elt_size _bfd_elf_default_got_elt_size
1041
+#endif
1042
+#ifndef elf_backend_obj_attrs_vendor
1043
+#define elf_backend_obj_attrs_vendor           NULL
1044
+#endif
1045
+#ifndef elf_backend_obj_attrs_section
1046
+#define elf_backend_obj_attrs_section          NULL
1047
+#endif
1048
+#ifndef elf_backend_obj_attrs_arg_type
1049
+#define elf_backend_obj_attrs_arg_type         NULL
1050
+#endif
1051
+#ifndef elf_backend_obj_attrs_section_type
1052
+#define elf_backend_obj_attrs_section_type             SHT_GNU_ATTRIBUTES
1053
+#endif
1054
+#ifndef elf_backend_obj_attrs_order
1055
+#define elf_backend_obj_attrs_order            NULL
1056
+#endif
1057
+#define elf_backend_obj_attrs_handle_unknown   NULL
1058
+#define elf_backend_static_tls_alignment       1
1059
+#define elf_backend_post_process_headers       _bfd_elf_post_process_headers
1060
+#define elf_backend_print_symbol_all           NULL
1061
+#define elf_backend_output_arch_local_syms     NULL
1062
+#define elf_backend_output_arch_syms           NULL
1063
+#define elf_backend_copy_indirect_symbol  _bfd_elf_link_hash_copy_indirect
1064
+#define elf_backend_hide_symbol                _bfd_elf_link_hash_hide_symbol
1065
+#define elf_backend_fixup_symbol               NULL
1066
+#define elf_backend_merge_symbol_attribute     NULL
1067
+#define elf_backend_get_target_dtag            NULL
1068
+#define elf_backend_ignore_undef_symbol                NULL
1069
+#define elf_backend_emit_relocs                        _bfd_elf_link_output_relocs
1070
+#define elf_backend_count_relocs               NULL
1071
+#define elf_backend_count_additional_relocs    NULL
1072
+#define elf_backend_sort_relocs_p              NULL
1073
+#define elf_backend_grok_prstatus              NULL
1074
+#define elf_backend_grok_psinfo                        NULL
1075
+#define elf_backend_write_core_note            NULL
1076
+#define elf_backend_lookup_section_flags_hook  NULL
1077
+#define elf_backend_reloc_type_class           _bfd_elf_reloc_type_class
1078
+#define elf_backend_discard_info               NULL
1079
+#define elf_backend_ignore_discarded_relocs    NULL
1080
+#define elf_backend_action_discarded _bfd_elf_default_action_discarded
1081
+#define elf_backend_eh_frame_address_size _bfd_elf_eh_frame_address_size
1082
+#define elf_backend_can_make_relative_eh_frame _bfd_elf_can_make_relative
1083
+#define elf_backend_can_make_lsda_relative_eh_frame    _bfd_elf_can_make_relative
1084
+#define elf_backend_encode_eh_address          _bfd_elf_encode_eh_address
1085
+#define elf_backend_write_section              NULL
1086
+#define elf_backend_mips_irix_compat           NULL
1087
+#define elf_backend_mips_rtype_to_howto                NULL
1088
+
1089
+/* Previously, backends could only use SHT_REL or SHT_RELA relocation
1090
+   sections, but not both.  They defined USE_REL to indicate SHT_REL
1091
+   sections, and left it undefined to indicated SHT_RELA sections.
1092
+   For backwards compatibility, we still support this usage.  */
1093
+#ifndef USE_REL
1094
+#define USE_REL 0
1095
+#endif
1096
+
1097
+/* Use these in new code.  */
1098
+#define elf_backend_may_use_rel_p USE_REL
1099
+#define elf_backend_may_use_rela_p !USE_REL
1100
+#define elf_backend_default_use_rela_p !USE_REL
1101
+#define elf_backend_rela_plts_and_copies_p elf_backend_default_use_rela_p
1102
+
1103
+#ifndef elf_backend_rela_normal
1104
+#define elf_backend_rela_normal 0
1105
+#endif
1106
+
1107
+#define elf_backend_plt_sym_val NULL
1108
+#define elf_backend_relplt_name NULL
1109
+
1110
+#define ELF_MACHINE_ALT1 0
1111
+#define ELF_MACHINE_ALT2 0
1112
+
1113
+#ifndef elf_backend_size_info
1114
+#define elf_backend_size_info _bfd_elf32_size_info
1115
+#endif
1116
+
1117
+#define elf_backend_special_sections NULL
1118
+#define elf_backend_sign_extend_vma 0
1119
+#define elf_backend_link_order_error_handler _bfd_default_error_handler
1120
+#define elf_backend_common_definition _bfd_elf_common_definition
1121
+#define elf_backend_common_section_index _bfd_elf_common_section_index
1122
+#define elf_backend_common_section _bfd_elf_common_section
1123
+
1124
+#define elf_backend_merge_symbol NULL
1125
+#define elf_backend_hash_symbol _bfd_elf_hash_symbol
1126
+#define elf_backend_is_function_type _bfd_elf_is_function_type
1127
+#define elf_backend_maybe_function_sym _bfd_elf_maybe_function_sym
1128
+#define elf_backend_get_reloc_section _bfd_elf_get_reloc_section
1129
+#define elf_backend_copy_special_section_fields NULL
1130
+#define elf_backend_compact_eh_encoding NULL
1131
+#define elf_backend_cant_unwind_opcode 0
1132
+
1133
+#define elf_match_priority \
1134
+  (ELF_ARCH == bfd_arch_unknown ? 2 : ELF_OSABI == ELFOSABI_NONE ? 1 : 0)
1135
+
1136
+extern const struct elf_size_info _bfd_elf32_size_info;
1137
+
1138
+static struct elf_backend_data elf32_bed =
1139
+{
1140
+  ELF_ARCH,                    /* arch */
1141
+  ELF_TARGET_ID,               /* target_id */
1142
+  ELF_MACHINE_CODE,            /* elf_machine_code */
1143
+  ELF_OSABI,                   /* elf_osabi  */
1144
+  ELF_MAXPAGESIZE,             /* maxpagesize */
1145
+  ELF_MINPAGESIZE,             /* minpagesize */
1146
+  ELF_COMMONPAGESIZE,          /* commonpagesize */
1147
+  ELF_DYNAMIC_SEC_FLAGS,       /* dynamic_sec_flags */
1148
+  elf_backend_arch_data,
1149
+  elf_info_to_howto,
1150
+  elf_info_to_howto_rel,
1151
+  elf_backend_sym_is_global,
1152
+  elf_backend_object_p,
1153
+  elf_backend_symbol_processing,
1154
+  elf_backend_symbol_table_processing,
1155
+  elf_backend_get_symbol_type,
1156
+  elf_backend_archive_symbol_lookup,
1157
+  elf_backend_name_local_section_symbols,
1158
+  elf_backend_section_processing,
1159
+  elf_backend_section_from_shdr,
1160
+  elf_backend_section_flags,
1161
+  elf_backend_get_sec_type_attr,
1162
+  elf_backend_section_from_phdr,
1163
+  elf_backend_fake_sections,
1164
+  elf_backend_section_from_bfd_section,
1165
+  elf_backend_add_symbol_hook,
1166
+  elf_backend_link_output_symbol_hook,
1167
+  elf_backend_create_dynamic_sections,
1168
+  elf_backend_omit_section_dynsym,
1169
+  elf_backend_relocs_compatible,
1170
+  elf_backend_check_relocs,
1171
+  elf_backend_check_directives,
1172
+  elf_backend_notice_as_needed,
1173
+  elf_backend_adjust_dynamic_symbol,
1174
+  elf_backend_always_size_sections,
1175
+  elf_backend_size_dynamic_sections,
1176
+  elf_backend_init_index_section,
1177
+  elf_backend_relocate_section,
1178
+  elf_backend_finish_dynamic_symbol,
1179
+  elf_backend_finish_dynamic_sections,
1180
+  elf_backend_begin_write_processing,
1181
+  elf_backend_final_write_processing,
1182
+  elf_backend_additional_program_headers,
1183
+  elf_backend_modify_segment_map,
1184
+  elf_backend_modify_program_headers,
1185
+  elf_backend_gc_keep,
1186
+  elf_backend_gc_mark_dynamic_ref,
1187
+  elf_backend_gc_mark_hook,
1188
+  elf_backend_gc_mark_extra_sections,
1189
+  elf_backend_gc_sweep_hook,
1190
+  elf_backend_post_process_headers,
1191
+  elf_backend_print_symbol_all,
1192
+  elf_backend_output_arch_local_syms,
1193
+  elf_backend_output_arch_syms,
1194
+  elf_backend_copy_indirect_symbol,
1195
+  elf_backend_hide_symbol,
1196
+  elf_backend_fixup_symbol,
1197
+  elf_backend_merge_symbol_attribute,
1198
+  elf_backend_get_target_dtag,
1199
+  elf_backend_ignore_undef_symbol,
1200
+  elf_backend_emit_relocs,
1201
+  elf_backend_count_relocs,
1202
+  elf_backend_count_additional_relocs,
1203
+  elf_backend_sort_relocs_p,
1204
+  elf_backend_grok_prstatus,
1205
+  elf_backend_grok_psinfo,
1206
+  elf_backend_write_core_note,
1207
+  elf_backend_lookup_section_flags_hook,
1208
+  elf_backend_reloc_type_class,
1209
+  elf_backend_discard_info,
1210
+  elf_backend_ignore_discarded_relocs,
1211
+  elf_backend_action_discarded,
1212
+  elf_backend_eh_frame_address_size,
1213
+  elf_backend_can_make_relative_eh_frame,
1214
+  elf_backend_can_make_lsda_relative_eh_frame,
1215
+  elf_backend_encode_eh_address,
1216
+  elf_backend_write_section,
1217
+  elf_backend_mips_irix_compat,
1218
+  elf_backend_mips_rtype_to_howto,
1219
+  elf_backend_ecoff_debug_swap,
1220
+  elf_backend_bfd_from_remote_memory,
1221
+  elf_backend_plt_sym_val,
1222
+  elf_backend_common_definition,
1223
+  elf_backend_common_section_index,
1224
+  elf_backend_common_section,
1225
+  elf_backend_merge_symbol,
1226
+  elf_backend_hash_symbol,
1227
+  elf_backend_is_function_type,
1228
+  elf_backend_maybe_function_sym,
1229
+  elf_backend_get_reloc_section,
1230
+  elf_backend_copy_special_section_fields,
1231
+  elf_backend_link_order_error_handler,
1232
+  elf_backend_relplt_name,
1233
+  ELF_MACHINE_ALT1,
1234
+  ELF_MACHINE_ALT2,
1235
+  &elf_backend_size_info,
1236
+  elf_backend_special_sections,
1237
+  elf_backend_got_header_size,
1238
+  elf_backend_got_elt_size,
1239
+  elf_backend_obj_attrs_vendor,
1240
+  elf_backend_obj_attrs_section,
1241
+  elf_backend_obj_attrs_arg_type,
1242
+  elf_backend_obj_attrs_section_type,
1243
+  elf_backend_obj_attrs_order,
1244
+  elf_backend_obj_attrs_handle_unknown,
1245
+  elf_backend_compact_eh_encoding,
1246
+  elf_backend_cant_unwind_opcode,
1247
+  elf_backend_static_tls_alignment,
1248
+  elf_backend_stack_align,
1249
+  elf_backend_strtab_flags,
1250
+  elf_backend_collect,
1251
+  elf_backend_type_change_ok,
1252
+  elf_backend_may_use_rel_p,
1253
+  elf_backend_may_use_rela_p,
1254
+  elf_backend_default_use_rela_p,
1255
+  elf_backend_rela_plts_and_copies_p,
1256
+  elf_backend_rela_normal,
1257
+  elf_backend_sign_extend_vma,
1258
+  elf_backend_want_got_plt,
1259
+  elf_backend_plt_readonly,
1260
+  elf_backend_want_plt_sym,
1261
+  elf_backend_plt_not_loaded,
1262
+  elf_backend_plt_alignment,
1263
+  elf_backend_can_gc_sections,
1264
+  elf_backend_can_refcount,
1265
+  elf_backend_want_got_sym,
1266
+  elf_backend_want_dynbss,
1267
+  elf_backend_want_p_paddr_set_to_zero,
1268
+  elf_backend_default_execstack,
1269
+  elf_backend_caches_rawsize,
1270
+  elf_backend_extern_protected_data
1271
+};
1272
+
1273
+/* Forward declaration for use when initialising alternative_target field.  */
1274
+
1275
+#ifdef TARGET_BIG_SYM
1276
+const bfd_target TARGET_BIG_SYM =
1277
+{
1278
+  /* name: identify kind of target */
1279
+  TARGET_BIG_NAME,
1280
+
1281
+  /* flavour: general indication about file */
1282
+  bfd_target_elf_flavour,
1283
+
1284
+  /* byteorder: data is big endian */
1285
+  BFD_ENDIAN_BIG,
1286
+
1287
+  /* header_byteorder: header is also big endian */
1288
+  BFD_ENDIAN_BIG,
1289
+
1290
+  /* object_flags: mask of all file flags */
1291
+  (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS
1292
+   | DYNAMIC | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS
1293
+   | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON),
1294
+
1295
+  /* section_flags: mask of all section flags */
1296
+  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY
1297
+   | SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_EXCLUDE | SEC_SORT_ENTRIES
1298
+   | SEC_SMALL_DATA | SEC_MERGE | SEC_STRINGS | SEC_GROUP),
1299
+
1300
+   /* leading_symbol_char: is the first char of a user symbol
1301
+      predictable, and if so what is it */
1302
+  elf_symbol_leading_char,
1303
+
1304
+  /* ar_pad_char: pad character for filenames within an archive header
1305
+     FIXME:  this really has nothing to do with ELF, this is a characteristic
1306
+     of the archiver and/or os and should be independently tunable */
1307
+  '/',
1308
+
1309
+  /* ar_max_namelen: maximum number of characters in an archive header
1310
+     FIXME:  this really has nothing to do with ELF, this is a characteristic
1311
+     of the archiver and should be independently tunable.  The System V ABI,
1312
+     Chapter 7 (Formats & Protocols), Archive section sets this as 15.  */
1313
+  15,
1314
+
1315
+  elf_match_priority,
1316
+
1317
+  /* Routines to byte-swap various sized integers from the data sections */
1318
+  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1319
+    bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1320
+    bfd_getb16, bfd_getb_signed_16, bfd_putb16,
1321
+
1322
+  /* Routines to byte-swap various sized integers from the file headers */
1323
+  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1324
+    bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1325
+    bfd_getb16, bfd_getb_signed_16, bfd_putb16,
1326
+
1327
+  /* bfd_check_format: check the format of a file being read */
1328
+  { _bfd_dummy_target,         /* unknown format */
1329
+    bfd_elf32_object_p,                /* assembler/linker output (object file) */
1330
+    bfd_elf32_archive_p,       /* an archive */
1331
+    bfd_elf32_core_file_p      /* a core file */
1332
+  },
1333
+
1334
+  /* bfd_set_format: set the format of a file being written */
1335
+  { bfd_false,
1336
+    bfd_elf32_mkobject,
1337
+    bfd_elf32_mkarchive,
1338
+    bfd_elf32_mkcorefile
1339
+  },
1340
+
1341
+  /* bfd_write_contents: write cached information into a file being written */
1342
+  { bfd_false,
1343
+    bfd_elf32_write_object_contents,
1344
+    bfd_elf32_write_archive_contents,
1345
+    bfd_elf32_write_corefile_contents,
1346
+  },
1347
+
1348
+  BFD_JUMP_TABLE_GENERIC (bfd_elf32),
1349
+  BFD_JUMP_TABLE_COPY (bfd_elf32),
1350
+  BFD_JUMP_TABLE_CORE (bfd_elf32),
1351
+#ifdef bfd_elf32_archive_functions
1352
+  BFD_JUMP_TABLE_ARCHIVE (bfd_elf32_archive),
1353
+#else
1354
+  BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1355
+#endif
1356
+  BFD_JUMP_TABLE_SYMBOLS (bfd_elf32),
1357
+  BFD_JUMP_TABLE_RELOCS (bfd_elf32),
1358
+  BFD_JUMP_TABLE_WRITE (bfd_elf32),
1359
+  BFD_JUMP_TABLE_LINK (bfd_elf32),
1360
+  BFD_JUMP_TABLE_DYNAMIC (bfd_elf32),
1361
+
1362
+  /* Alternative endian target.  */
1363
+#ifdef TARGET_LITTLE_SYM
1364
+  & TARGET_LITTLE_SYM,
1365
+#else
1366
+  NULL,
1367
+#endif
1368
+
1369
+  /* backend_data: */
1370
+  &elf32_bed
1371
+};
1372
+#endif
1373
+
1374
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/libbfd.h binutils-2.27-zip/bfd/libbfd.h
1375
--- binutils-2.27/bfd/libbfd.h  2016-08-03 03:36:51.000000000 -0400
1376
+++ binutils-2.27-zip/bfd/libbfd.h      2017-01-04 22:04:11.000000000 -0500
1377
@@ -3125,6 +3125,16 @@
1378
   "BFD_RELOC_VISIUM_HI16_PCREL",
1379
   "BFD_RELOC_VISIUM_LO16_PCREL",
1380
   "BFD_RELOC_VISIUM_IM16_PCREL",
1381
+  "BFD_RELOC_ZIP_VALUE",
1382
+  "BFD_RELOC_ZIP_BRANCH",
1383
+  "BFD_RELOC_ZIP_OPB_IMM",
1384
+  "BFD_RELOC_ZIP_OPB_OFFSET",
1385
+  "BFD_RELOC_ZIP_OPB_PCREL",
1386
+  "BFD_RELOC_ZIP_MOV_OFFSET",
1387
+  "BFD_RELOC_ZIP_MOV_PCREL",
1388
+  "BFD_RELOC_ZIP_LDI",
1389
+  "BFD_RELOC_ZIP_LLO",
1390
+  "BFD_RELOC_ZIP_BREV",
1391
  "@@overflow: BFD_RELOC_UNUSED@@",
1392
 };
1393
 #endif
1394
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/Makefile.am binutils-2.27-zip/bfd/Makefile.am
1395
--- binutils-2.27/bfd/Makefile.am       2016-08-03 03:36:50.000000000 -0400
1396
+++ binutils-2.27-zip/bfd/Makefile.am   2016-12-31 17:26:07.151146300 -0500
1397
@@ -173,7 +173,8 @@
1398
        cpu-xstormy16.lo \
1399
        cpu-xtensa.lo \
1400
        cpu-z80.lo \
1401
-       cpu-z8k.lo
1402
+       cpu-z8k.lo \
1403
+       cpu-zip.lo
1404
 
1405
 ALL_MACHINES_CFILES = \
1406
        cpu-aarch64.c \
1407
@@ -260,7 +261,8 @@
1408
        cpu-xstormy16.c \
1409
        cpu-xtensa.c \
1410
        cpu-z80.c \
1411
-       cpu-z8k.c
1412
+       cpu-z8k.c \
1413
+       cpu-zip.c
1414
 
1415
 # The .o files needed by all of the 32 bit vectors that are configured into
1416
 # target_vector in targets.c if configured with --enable-targets=all.
1417
@@ -382,6 +384,7 @@
1418
        elf32-xgate.lo \
1419
        elf32-xstormy16.lo \
1420
        elf32-xtensa.lo \
1421
+       elf32-zip.lo \
1422
        elf32.lo \
1423
        elflink.lo \
1424
        elfxx-sparc.lo \
1425
@@ -574,6 +577,7 @@
1426
        elf32-xgate.c \
1427
        elf32-xstormy16.c \
1428
        elf32-xtensa.c \
1429
+       elf32-zip.c \
1430
        elf32.c \
1431
        elflink.c \
1432
        elfxx-sparc.c \
1433
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/Makefile.in binutils-2.27-zip/bfd/Makefile.in
1434
--- binutils-2.27/bfd/Makefile.in       2016-08-03 04:06:27.000000000 -0400
1435
+++ binutils-2.27-zip/bfd/Makefile.in   2017-01-04 14:29:55.000000000 -0500
1436
@@ -505,7 +505,8 @@
1437
        cpu-xstormy16.lo \
1438
        cpu-xtensa.lo \
1439
        cpu-z80.lo \
1440
-       cpu-z8k.lo
1441
+       cpu-z8k.lo \
1442
+       cpu-zip.lo
1443
 
1444
 ALL_MACHINES_CFILES = \
1445
        cpu-aarch64.c \
1446
@@ -592,7 +593,8 @@
1447
        cpu-xstormy16.c \
1448
        cpu-xtensa.c \
1449
        cpu-z80.c \
1450
-       cpu-z8k.c
1451
+       cpu-z8k.c \
1452
+       cpu-zip.c
1453
 
1454
 
1455
 # The .o files needed by all of the 32 bit vectors that are configured into
1456
@@ -715,6 +717,7 @@
1457
        elf32-xgate.lo \
1458
        elf32-xstormy16.lo \
1459
        elf32-xtensa.lo \
1460
+       elf32-zip.lo \
1461
        elf32.lo \
1462
        elflink.lo \
1463
        elfxx-sparc.lo \
1464
@@ -907,6 +910,7 @@
1465
        elf32-xgate.c \
1466
        elf32-xstormy16.c \
1467
        elf32-xtensa.c \
1468
+       elf32-zip.c \
1469
        elf32.c \
1470
        elflink.c \
1471
        elfxx-sparc.c \
1472
@@ -1437,6 +1441,7 @@
1473
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpu-xtensa.Plo@am__quote@
1474
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpu-z80.Plo@am__quote@
1475
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpu-z8k.Plo@am__quote@
1476
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpu-zip.Plo@am__quote@
1477
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/demo64.Plo@am__quote@
1478
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dwarf1.Plo@am__quote@
1479
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dwarf2.Plo@am__quote@
1480
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/merge.c binutils-2.27-zip/bfd/merge.c
1481
--- binutils-2.27/bfd/merge.c   2016-08-03 03:36:51.000000000 -0400
1482
+++ binutils-2.27-zip/bfd/merge.c       2017-01-02 14:48:54.000000000 -0500
1483
@@ -826,7 +826,7 @@
1484
   else
1485
     {
1486
       contents = NULL;
1487
-      pos = sec->output_section->filepos + sec->output_offset;
1488
+      pos = sec->output_section->filepos + sec->output_offset * bfd_octets_per_byte(output_bfd);
1489
       if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
1490
        return FALSE;
1491
     }
1492
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/reloc.c binutils-2.27-zip/bfd/reloc.c
1493
--- binutils-2.27/bfd/reloc.c   2016-08-03 03:36:51.000000000 -0400
1494
+++ binutils-2.27-zip/bfd/reloc.c       2017-01-04 22:03:52.000000000 -0500
1495
@@ -7697,7 +7697,32 @@
1496
   BFD_RELOC_VISIUM_IM16_PCREL
1497
 ENUMDOC
1498
   Visium Relocations.
1499
-
1500
+ENUM
1501
+  BFD_RELOC_ZIP_VALUE
1502
+ENUMDOC
1503
+  ZipCPU - 32 bit absolute value for LJMP instruction
1504
+ENUM
1505
+  BFD_RELOC_ZIP_BRANCH
1506
+ENUMDOC
1507
+  ZipCPU - 18-bit PC-relative offset for BRA (ADD #x,PC) instructions
1508
+ENUM
1509
+  BFD_RELOC_ZIP_OPB_IMM
1510
+ENUMX
1511
+  BFD_RELOC_ZIP_OPB_OFFSET
1512
+ENUMX
1513
+  BFD_RELOC_ZIP_OPB_PCREL
1514
+ENUMX
1515
+  BFD_RELOC_ZIP_MOV_OFFSET
1516
+ENUMX
1517
+  BFD_RELOC_ZIP_MOV_PCREL
1518
+ENUMX
1519
+  BFD_RELOC_ZIP_LDI
1520
+ENUMX
1521
+  BFD_RELOC_ZIP_LLO
1522
+ENUMX
1523
+  BFD_RELOC_ZIP_BREV
1524
+ENUMDOC
1525
+  ZipCPU value relocations
1526
 ENDSENUM
1527
   BFD_RELOC_UNUSED
1528
 CODE_FRAGMENT
1529
diff -Naur '--exclude=*.swp' binutils-2.27/bfd/targets.c binutils-2.27-zip/bfd/targets.c
1530
--- binutils-2.27/bfd/targets.c 2016-08-03 03:36:51.000000000 -0400
1531
+++ binutils-2.27-zip/bfd/targets.c     2016-12-31 17:37:14.021847080 -0500
1532
@@ -910,6 +910,7 @@
1533
 extern const bfd_target xtensa_elf32_le_vec;
1534
 extern const bfd_target z80_coff_vec;
1535
 extern const bfd_target z8k_coff_vec;
1536
+extern const bfd_target zip_elf32_vec;
1537
 
1538
 /* These are always included.  */
1539
 extern const bfd_target srec_vec;
1540
@@ -1441,6 +1442,8 @@
1541
        &z80_coff_vec,
1542
 
1543
        &z8k_coff_vec,
1544
+
1545
+       &zip_elf32_vec,
1546
 #endif /* not SELECT_VECS */
1547
 
1548
 /* Always support S-records, for convenience.  */
1549
diff -Naur '--exclude=*.swp' binutils-2.27/binutils/readelf.c binutils-2.27-zip/binutils/readelf.c
1550
--- binutils-2.27/binutils/readelf.c    2016-08-03 03:36:51.000000000 -0400
1551
+++ binutils-2.27-zip/binutils/readelf.c        2016-12-31 17:40:19.908241961 -0500
1552
@@ -154,6 +154,7 @@
1553
 #include "elf/xgate.h"
1554
 #include "elf/xstormy16.h"
1555
 #include "elf/xtensa.h"
1556
+#include "elf/zip.h"
1557
 
1558
 #include "getopt.h"
1559
 #include "libiberty.h"
1560
@@ -800,6 +801,7 @@
1561
     case EM_XTENSA_OLD:
1562
     case EM_MICROBLAZE:
1563
     case EM_MICROBLAZE_OLD:
1564
+    case EM_ZIP:
1565
       return TRUE;
1566
 
1567
     case EM_68HC05:
1568
@@ -1476,6 +1478,10 @@
1569
        case EM_ALTERA_NIOS2:
1570
          rtype = elf_nios2_reloc_type (type);
1571
          break;
1572
+
1573
+       case EM_ZIP:
1574
+         rtype = elf_zip_reloc_type (type);
1575
+         break;
1576
        }
1577
 
1578
       if (rtype == NULL)
1579
@@ -2339,6 +2345,7 @@
1580
     case EM_TILEGX:            return "Tilera TILE-Gx multicore architecture family";
1581
     case EM_CUDA:              return "NVIDIA CUDA architecture";
1582
     case EM_XGATE:             return "Motorola XGATE embedded processor";
1583
+    case EM_ZIP:               return "Gisselquist Technology ZipCPU";
1584
     default:
1585
       snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
1586
       return buff;
1587
@@ -11659,6 +11666,8 @@
1588
     case EM_XTENSA_OLD:
1589
     case EM_XTENSA:
1590
       return reloc_type == 1; /* R_XTENSA_32.  */
1591
+    case EM_ZIP:
1592
+      return reloc_type == 1; /* R_ZIP_32.  */
1593
     default:
1594
       {
1595
        static unsigned int prev_warn = 0;
1596
@@ -11735,6 +11744,8 @@
1597
     case EM_XTENSA_OLD:
1598
     case EM_XTENSA:
1599
       return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
1600
+    case EM_ZIP:
1601
+      return FALSE; /*  */
1602
     default:
1603
       /* Do not abort or issue an error message here.  Not all targets use
1604
         pc-relative 32-bit relocs in their DWARF debug information and we
1605
@@ -11944,6 +11955,7 @@
1606
     case EM_TI_C6000:/* R_C6000_NONE.  */
1607
     case EM_X86_64:  /* R_X86_64_NONE.  */
1608
     case EM_XC16X:
1609
+    case EM_ZIP:
1610
       return reloc_type == 0;
1611
 
1612
     case EM_AARCH64:
1613
diff -Naur '--exclude=*.swp' binutils-2.27/config.sub binutils-2.27-zip/config.sub
1614
--- binutils-2.27/config.sub    2016-08-03 03:36:51.000000000 -0400
1615
+++ binutils-2.27-zip/config.sub        2017-01-11 14:20:34.804049801 -0500
1616
@@ -316,7 +316,7 @@
1617
        | visium \
1618
        | we32k \
1619
        | x86 | xc16x | xstormy16 | xtensa \
1620
-       | z8k | z80)
1621
+       | z8k | z80 )
1622
                basic_machine=$basic_machine-unknown
1623
                ;;
1624
        c54x)
1625
@@ -355,6 +355,14 @@
1626
        xscaleel)
1627
                basic_machine=armel-unknown
1628
                ;;
1629
+       zip-*-linux*)
1630
+               basic_machine=zip
1631
+               os=-linux
1632
+               ;;
1633
+       zip*)
1634
+               basic_machine=zip-unknown
1635
+               os=-elf
1636
+               ;;
1637
 
1638
        # We use `pc' rather than `unknown'
1639
        # because (1) that's what they normally are, and
1640
diff -Naur '--exclude=*.swp' binutils-2.27/configure binutils-2.27-zip/configure
1641
--- binutils-2.27/configure     2016-08-03 03:54:55.000000000 -0400
1642
+++ binutils-2.27-zip/configure 2017-01-08 20:37:33.566336786 -0500
1643
@@ -3548,6 +3548,9 @@
1644
   ft32-*-*)
1645
     noconfigdirs="$noconfigdirs ${libgcj}"
1646
     ;;
1647
+  zip*)
1648
+    noconfigdirs="$noconfigdirs ${libgcj}"
1649
+    ;;
1650
   *-*-lynxos*)
1651
     noconfigdirs="$noconfigdirs ${libgcj}"
1652
     ;;
1653
@@ -3575,6 +3578,9 @@
1654
     *-*-aix*)
1655
        noconfigdirs="$noconfigdirs target-libgo"
1656
        ;;
1657
+    zip*)
1658
+       noconfigdirs="$noconfigdirs target-libgo"
1659
+       ;;
1660
     esac
1661
 fi
1662
 
1663
@@ -3974,6 +3980,9 @@
1664
   vax-*-*)
1665
     noconfigdirs="$noconfigdirs target-newlib target-libgloss"
1666
     ;;
1667
+  zip*)
1668
+    noconfigdirs="$noconfigdirs gdb gprof"
1669
+    ;;
1670
 esac
1671
 
1672
 # If we aren't building newlib, then don't build libgloss, since libgloss
1673
diff -Naur '--exclude=*.swp' binutils-2.27/configure.ac binutils-2.27-zip/configure.ac
1674
--- binutils-2.27/configure.ac  2016-08-03 04:37:38.000000000 -0400
1675
+++ binutils-2.27-zip/configure.ac      2017-01-08 20:41:54.836485336 -0500
1676
@@ -884,6 +884,9 @@
1677
   ft32-*-*)
1678
     noconfigdirs="$noconfigdirs ${libgcj}"
1679
     ;;
1680
+  zip*)
1681
+    noconfigdirs="$noconfigdirs ${libgcj}"
1682
+    ;;
1683
   *-*-lynxos*)
1684
     noconfigdirs="$noconfigdirs ${libgcj}"
1685
     ;;
1686
@@ -911,6 +914,9 @@
1687
     *-*-aix*)
1688
        noconfigdirs="$noconfigdirs target-libgo"
1689
        ;;
1690
+    zip*)
1691
+       noconfigdirs="$noconfigdirs target-libgo"
1692
+       ;;
1693
     esac
1694
 fi
1695
 
1696
@@ -1310,6 +1316,9 @@
1697
   vax-*-*)
1698
     noconfigdirs="$noconfigdirs target-newlib target-libgloss"
1699
     ;;
1700
+  zip*)
1701
+    noconfigdirs="$noconfigdirs ${libgcj} gdb sim gprof"
1702
+    ;;
1703
 esac
1704
 
1705
 # If we aren't building newlib, then don't build libgloss, since libgloss
1706
diff -Naur '--exclude=*.swp' binutils-2.27/gas/config/tc-zip.c binutils-2.27-zip/gas/config/tc-zip.c
1707
--- binutils-2.27/gas/config/tc-zip.c   1969-12-31 19:00:00.000000000 -0500
1708 206 dgisselq
+++ binutils-2.27-zip/gas/config/tc-zip.c       2017-03-15 23:03:15.801504568 -0400
1709
@@ -0,0 +1,3340 @@
1710 202 dgisselq
+////////////////////////////////////////////////////////////////////////////////
1711
+//
1712
+// Filename:   tc-zip.c
1713
+//
1714
+// Project:    Zip CPU backend for GNU Binutils
1715
+//
1716
+// Purpose:    This is the main file associated with the Zip Assembler.  By
1717
+//             that I mean that it handles all of the ZipCPU specifics.  The
1718
+//     rest of the files you find in this directory, mostly tc-cpuname.c and
1719
+//     such, handle other CPUs.  This one handles the ZipCPU.  The goal in
1720
+//     doing this is so that nothing else changes when changes need to be made
1721
+//     to a CPU, and that changes to the assembler in general shouldn't impact
1722
+//     the CPU specific processing.
1723
+//
1724
+//     I'll let you be the judge as to how well this file meets that goal.
1725
+//
1726
+// Creator:    Dan Gisselquist, Ph.D.
1727
+//             Gisselquist Technology, LLC
1728
+//
1729
+////////////////////////////////////////////////////////////////////////////////
1730
+//
1731
+// Copyright (C) 2016-2017, Gisselquist Technology, LLC
1732
+//
1733
+// This program is free software (firmware): you can redistribute it and/or
1734
+// modify it under the terms of  the GNU General Public License as published
1735
+// by the Free Software Foundation, either version 3 of the License, or (at
1736
+// your option) any later version.
1737
+//
1738
+// This program is distributed in the hope that it will be useful, but WITHOUT
1739
+// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
1740
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1741
+// for more details.
1742
+//
1743
+// You should have received a copy of the GNU General Public License along
1744
+// with this program.  (It's in the $(ROOT)/doc directory, run make with no
1745
+// target there if the PDF file isn't present.)  If not, see
1746
+// <http://www.gnu.org/licenses/> for a copy.
1747
+//
1748
+// License:    GPL, v3, as defined and found on www.gnu.org,
1749
+//             http://www.gnu.org/licenses/gpl.html
1750
+//
1751
+//
1752
+////////////////////////////////////////////////////////////////////////////////
1753
+#include "as.h"
1754
+
1755
+#include <stdlib.h>
1756
+#include <string.h>
1757
+#include <stdint.h>
1758
+#include <ctype.h>
1759
+
1760
+#include "frags.h"
1761
+#include "struc-symbol.h"
1762
+#include "symbols.h"
1763
+#include "config/tc-zip.h"
1764
+#include "elf/zip.h"
1765
+
1766
+// #define     ZIP_DEBUG
1767
+
1768
+const  char    comment_chars[] = ";#";
1769
+const  char    line_comment_chars[] = ";#";
1770
+// Characters which separate lines (newline need to be listed)
1771
+const  char    line_separator_chars[] = "";
1772
+
1773
+// Characters defining floating point numbers, such as 0f<floatingpt#>
1774
+const  char    FLT_CHARS[] = "fFrR";
1775
+
1776
+// Characters that may be used for an exponent in floating point numbers
1777
+const  char    EXP_CHARS[] = "eE";
1778
+
1779
+static int     cis_mergable = 0; // tc_check_label, tc_frob_label
1780
+
1781
+// zip_param_got is a flag that we'll set to know if we need to reference a
1782
+// global offset table.  Since nothing we've done has yet needed that table,
1783
+// we'll keep this undefined for now.
1784
+//
1785
+// static int  zip_param_got = 0;
1786
+static int     zip_param_cis = 1;
1787
+static int     zip_param_small = 0;
1788
+static int     zip_param_use_machine  = 1;
1789
+
1790
+typedef        struct  {
1791
+       int     m_known;
1792
+       int32_t m_value;
1793
+       symbolS *m_addsy, *m_subsy;
1794
+} MACHINEVALUE;
1795
+
1796
+typedef        struct  {
1797
+       MACHINEVALUE    r[ZIP_USER_REGS];
1798
+} MACHINEREGS;
1799
+
1800
+MACHINEREGS    zipm;
1801
+
1802
+static void zip_clear_machine(MACHINEREGS *pzipm);
1803
+
1804
+void   md_begin(void) {
1805
+       cis_mergable = FALSE;
1806
+       //
1807
+       record_alignment(text_section, 2);
1808
+       record_alignment(data_section, 2);
1809
+       //
1810
+       lex_type['['] = lex_type['A'];
1811
+       //
1812
+       zip_clear_machine(&zipm);
1813
+}
1814
+
1815
+void   md_end(void) { }
1816
+
1817
+void   zip_cons_align(int n) {
1818
+       int     lg;
1819
+       for(lg=0; ((1<<lg)<n)&&(lg<2); lg++)
1820
+               ;
1821
+       do_align(lg, NULL, 0, (1<<lg)-1);
1822
+}
1823
+
1824
+void   zip_flush_pending_output(void) {
1825
+       // If the Assembler is going to stuff things into our output stream,
1826
+       // then we need to make certain that any instructions that follow
1827
+       // dont try to merge with anything previous.  i.e., CIS = false.
1828
+       cis_mergable = FALSE;
1829
+}
1830
+
1831
+int    zip_address_bytes(void) { return 4; }
1832
+
1833
+const  pseudo_typeS    md_pseudo_table[] =
1834
+{
1835
+       { "word", cons, 4 },    // ZipCPU word size is 4, not 2
1836
+       { "long", cons, 8 },    // ZipCPU longs are 64-bits, not 32
1837
+       { "quad", cons,16 },    // ZipCPU QUAD-words are 128-bits, not 64
1838
+       { NULL, NULL, 0 }
1839
+};
1840
+
1841
+#define        ZIP_CC_SLEEP    0x010
1842
+#define        ZIP_CC_GIE      0x020
1843
+#define        ZIP_CC_STEP     0x040
1844
+
1845
+#define        SIM_OPCODE      0x77800000      // 0111.x111.1000. .... 0_111.x_111.10....
1846
+#define        NOOP_OPCODE     0x77c00000      // 0111.x111.1100. .... 0_111.x_111.11....
1847
+
1848
+typedef  struct {
1849
+       bfd_reloc_code_real_type        r_type;
1850
+       symbolS                         *r_sym;
1851
+       int                             r_pcrel;
1852
+       long                            r_fr_offset;
1853
+       fixS                            *r_fix;
1854
+       // Could also be char *name and bfd_reloc_code_real_type
1855
+} ZIPRELOC;
1856
+
1857
+// In case the instruction ...
1858
+#define        ZIP_MAX_NAUX    3 // Number of auxilliary instruction codes used
1859
+typedef        struct {
1860
+       int             i_naux;
1861
+       unsigned        i_code, // The actual machine language instruction
1862
+                       i_aux[ZIP_MAX_NAUX];
1863
+       ZIP_OPCODE      i_op;
1864
+       ZIP_CONDITION   i_cnd;
1865
+       ZIP_REG         i_areg; // = ZIP_RNONE for no register
1866
+       ZIP_REG         i_breg;
1867
+       int             i_imm;
1868
+       ZIPRELOC        *i_rp;
1869
+} ZIPIS;
1870
+
1871
+static ZIPIS *
1872
+zip_copy_insn(const ZIPIS *old) {
1873
+       ZIPIS *nw = (ZIPIS *)xmalloc(sizeof(ZIPIS));
1874
+
1875
+       memcpy((char *)nw, (char *)old, sizeof(ZIPIS));
1876
+
1877
+       return nw;
1878
+}
1879
+
1880
+static uint32_t
1881
+zip_brev(uint32_t v) {
1882
+       unsigned r=0, b;
1883
+
1884
+       for(b=0; b<32; b++, v>>=1)
1885
+               r = (r<<1)|(v&1);
1886
+
1887
+       return r;
1888
+}
1889
+
1890
+static const   int MACH_VUNKNOWN = 0, MACH_VKNOWN = 1, MACH_VUPPERKNOWN = 2,
1891
+                       MACH_VSYMKNOWN = 3;
1892
+
1893
+static void
1894
+zip_clear_machine(MACHINEREGS *pzipm) {
1895
+       int     i;
1896
+
1897
+#ifdef ZIP_DEBUG
1898
+       fprintf(stderr, "MACHINE, CLEAR\n");
1899
+#endif
1900
+       for(i=0; i<ZIP_USER_REGS; i++)
1901
+               pzipm->r[i].m_known = MACH_VUNKNOWN;
1902
+       for(i=0; i<ZIP_USER_REGS; i++)
1903
+               pzipm->r[i].m_addsy = NULL;
1904
+       for(i=0; i<ZIP_USER_REGS; i++)
1905
+               pzipm->r[i].m_subsy = NULL;
1906
+}
1907
+
1908
+static void
1909
+zip_advance_machine(MACHINEREGS *pzipm, ZIPIS *insn) {
1910
+       int             bval = insn->i_imm;
1911
+       int             bknown;
1912
+       MACHINEVALUE    *av, *bv;
1913
+
1914
+       if (!zip_param_use_machine) {
1915
+               // zip_clear_machine(pzipm);
1916
+               return;
1917
+       }
1918
+
1919
+       // The next three instructions don't change any machine values
1920
+       if((insn->i_op == ZIPO_SW)||(insn->i_op == ZIPO_SH)
1921
+               ||(insn->i_op==ZIPO_SB)
1922
+               ||(insn->i_op == ZIPO_CMP)||(insn->i_op == ZIPO_TST))
1923
+               return;
1924
+
1925
+       //
1926
+       // What remains to be done is to include symbols into the machine
1927
+       // state.  For now, we state that anything with a symbol becomes
1928
+       // unknown.
1929
+       //
1930
+       if ((insn->i_op == ZIPO_LJSR)||(insn->i_op == ZIPO_JSR)) {
1931
+               zip_clear_machine(pzipm);
1932
+               return;
1933
+       }
1934
+       if ((insn->i_rp)&&(insn->i_areg<ZIP_CC)) {
1935
+               bknown = MACH_VUNKNOWN;
1936
+               if((insn->i_op==ZIPO_LDI)&&(insn->i_cnd == ZIPC_ALWAYS)) {
1937
+                       pzipm->r[insn->i_areg].m_known = MACH_VSYMKNOWN;
1938
+                       pzipm->r[insn->i_areg].m_value = insn->i_imm;
1939
+                       pzipm->r[insn->i_areg].m_addsy = insn->i_rp->r_sym;
1940
+                       pzipm->r[insn->i_areg].m_subsy = NULL;
1941
+                       return;
1942
+               } else if ((insn->i_breg == ZIP_PC)&&(insn->i_op == ZIPO_MOV)
1943
+                               &&(insn->i_cnd == ZIPC_ALWAYS)) {
1944
+                       pzipm->r[insn->i_areg].m_known = MACH_VSYMKNOWN;
1945
+                       pzipm->r[insn->i_areg].m_value = insn->i_imm;
1946
+                       pzipm->r[insn->i_areg].m_addsy = insn->i_rp->r_sym;
1947
+                       pzipm->r[insn->i_areg].m_subsy = NULL;
1948
+                       return;
1949
+               }
1950
+       } else if (insn->i_rp) {
1951
+               bknown = MACH_VUNKNOWN;
1952
+       } else if (ZIP_RNONE == insn->i_breg)
1953
+               // B-Op is an immediate only
1954
+               bknown = MACH_VKNOWN;
1955
+       else if (insn->i_breg >= ZIP_CC)
1956
+               bknown = MACH_VUNKNOWN;
1957
+       else if (insn->i_imm == 0)
1958
+               bknown = (pzipm->r[insn->i_breg].m_known);
1959
+       else // If we have an immediate plus a value, can't know UPPER anymore
1960
+               bknown = (pzipm->r[insn->i_breg].m_known == MACH_VKNOWN)
1961
+                                       ? MACH_VKNOWN : MACH_VUNKNOWN;
1962
+
1963
+       if (insn->i_breg < ZIP_USER_REGS)
1964
+               bval += pzipm->r[insn->i_breg].m_value;
1965
+
1966
+       if (insn->i_areg >= ZIP_USER_REGS) {
1967
+#ifdef ZIP_DEBUG
1968
+               fprintf(stderr, "MACHINE, A-REG[%02x] out of bounds\n", insn->i_areg);
1969
+#endif
1970
+               return; // Nothing to do -- no change to machine
1971
+       } else if (insn->i_areg == ZIP_PC) {
1972
+#ifdef ZIP_DEBUG
1973
+               fprintf(stderr, "MACHINE, CLEAR ON JUMP\n");
1974
+#endif
1975
+               zip_clear_machine(pzipm);
1976
+               return;
1977
+       } else if ((insn->i_areg == ZIP_CC)&&(insn->i_op == ZIPO_AND)) {
1978
+#ifdef ZIP_DEBUG
1979
+               fprintf(stderr, "MACHINE, CLEAR ON ANDing to CC\n");
1980
+#endif
1981
+               zip_clear_machine(pzipm);
1982
+       } else if ((insn->i_areg == ZIP_CC)&&(
1983
+                       (insn->i_op == ZIPO_LDI)
1984
+                       ||(insn->i_op == ZIPO_LW)
1985
+                       ||(insn->i_op == ZIPO_CLR)
1986
+                       ||(insn->i_op == ZIPO_LDILO))) {
1987
+#ifdef ZIP_DEBUG
1988
+               fprintf(stderr, "MACHINE, CLEAR ON setting CC\n");
1989
+#endif
1990
+               zip_clear_machine(pzipm);
1991
+               return;
1992
+       } else if (insn->i_areg >= ZIP_CC) {
1993
+#ifdef ZIP_DEBUG
1994
+               fprintf(stderr, "MACHINE, Not tracking CC or PC changes\n");
1995
+#endif
1996
+               pzipm->r[insn->i_areg].m_known = MACH_VUNKNOWN;
1997
+               return;
1998
+       }
1999
+
2000
+       av = &pzipm->r[insn->i_areg];
2001
+       if (insn->i_breg < ZIP_CC)
2002
+               bv = &pzipm->r[insn->i_breg];
2003
+       else
2004
+               bv = NULL;
2005
+
2006
+       if (ZIPC_ALWAYS != insn->i_cnd) {
2007
+#ifdef ZIP_DEBUG
2008
+               fprintf(stderr, "MACHINE, CONDITIONAL\n");
2009
+#endif
2010
+               if ((ZIPO_LDILO == insn->i_op)
2011
+                               &&((av->m_known == MACH_VKNOWN)
2012
+                                       ||(av->m_known == MACH_VUPPERKNOWN)))
2013
+                       av->m_known = MACH_VUPPERKNOWN;
2014
+               else if ((ZIPO_LDI == insn->i_op)||(ZIPO_LDIn == insn->i_op))
2015
+                       ; // We'll catch this in a moment
2016
+               else
2017
+                       av->m_known = MACH_VUNKNOWN;
2018
+       } switch(insn->i_op) {
2019
+               case    ZIPO_SUB:
2020
+                       av->m_known = (av->m_known==MACH_VKNOWN)
2021
+                                       ? MACH_VKNOWN:MACH_VUNKNOWN;
2022
+                       av->m_value -= bval;
2023
+                       if (bknown != MACH_VKNOWN)
2024
+                               av->m_known = MACH_VUNKNOWN;
2025
+                       break;
2026
+               case    ZIPO_AND:
2027
+                       av->m_value &= bval;
2028
+                       if ((bknown == MACH_VUPPERKNOWN)&&(bval == 0)) {
2029
+                               if (av->m_known == MACH_VKNOWN)
2030
+                                       av->m_known = MACH_VUPPERKNOWN;
2031
+                               else if (av->m_known != MACH_VUPPERKNOWN)
2032
+                                       av->m_known = MACH_VUNKNOWN;
2033
+                       } else if (bknown != MACH_VKNOWN)
2034
+                               av->m_known = MACH_VUNKNOWN;
2035
+                       break;
2036
+               case    ZIPO_ADD:
2037
+                       av->m_value += bval;
2038
+                       if (bknown != MACH_VKNOWN)
2039
+                               av->m_known = MACH_VUNKNOWN;
2040
+                       break;
2041
+               case    ZIPO_OR:
2042
+                       av->m_value |= bval;
2043
+                       if (bknown == MACH_VUPPERKNOWN) {
2044
+                               if (av->m_known == MACH_VKNOWN)
2045
+                                       av->m_known = MACH_VUPPERKNOWN;
2046
+                               else if (av->m_known != MACH_VUPPERKNOWN)
2047
+                                       av->m_known = MACH_VUNKNOWN;
2048
+                       } else if (bknown != MACH_VKNOWN)
2049
+                               av->m_known = MACH_VUNKNOWN;
2050
+                       break;
2051
+               case    ZIPO_XOR:
2052
+                       av->m_value ^= bval;
2053
+                       if (bknown == MACH_VUPPERKNOWN) {
2054
+                               if (av->m_known == MACH_VKNOWN)
2055
+                                       av->m_known = MACH_VUPPERKNOWN;
2056
+                               else if (av->m_known != MACH_VUPPERKNOWN)
2057
+                                       av->m_known = MACH_VUNKNOWN;
2058
+                       } else if (bknown != MACH_VKNOWN)
2059
+                               av->m_known = MACH_VUNKNOWN;
2060
+                       break;
2061
+               case    ZIPO_LDI: case ZIPO_LDIn:
2062
+                       // Although the h/w instruction has no conditions, our
2063
+                       // decoding may have conditions until we finish
2064
+                       // working out what the actual result is.  Hence, we
2065
+                       // need to be aware of any conditions from above.
2066
+#ifdef ZIP_DEBUG
2067
+                       fprintf(stderr, "MACHINE, LDI -> %02x\n", insn->i_areg);
2068
+#endif
2069
+                       av->m_value = bval;
2070
+                       if (insn->i_cnd != ZIPC_ALWAYS) {
2071
+                               // if ((bknown)&&(bvalue == insn->i_imm))
2072
+                                       // pzipm->r[insn->i_areg].m_known = MACH_VKNOWN;
2073
+                       } else {
2074
+                               av->m_known = MACH_VKNOWN;
2075
+                       } break;
2076
+               case    ZIPO_LDILO:
2077
+#ifdef ZIP_DEBUG
2078
+                       fprintf(stderr, "MACHINE, LDILO -> %02x\n", insn->i_areg);
2079
+#endif
2080
+                       av->m_value &= ~0x0ffff;
2081
+                       av->m_value |= bval &0x0ffff;
2082
+                       if ((av->m_known == MACH_VUPPERKNOWN)
2083
+                                       &&(bknown == MACH_VKNOWN)
2084
+                                       &&(ZIPC_ALWAYS == insn->i_cnd))
2085
+                               av->m_known = MACH_VKNOWN;
2086
+                       break;
2087
+               case    ZIPO_BREV:
2088
+                       av->m_value = zip_brev(bval);
2089
+                       if (ZIPC_ALWAYS == insn->i_cnd)
2090
+                               av->m_known = (bknown == MACH_VKNOWN)?MACH_VKNOWN:MACH_VUNKNOWN;
2091
+                       else if ((bknown == MACH_VKNOWN)
2092
+                               &&(  (av->m_known == MACH_VKNOWN)
2093
+                                  ||(av->m_known == MACH_VUPPERKNOWN))
2094
+                               &&((((zip_brev(bval)^av->m_value)&0x0ffff)==0)))
2095
+                               av->m_known = MACH_VUPPERKNOWN;
2096
+                       break;
2097
+               case    ZIPO_MOV:
2098
+#ifdef ZIP_DEBUG
2099
+                       fprintf(stderr, "MACHINE, MOV -> %02x\n", insn->i_areg);
2100
+#endif
2101
+                       av->m_value = bval;
2102
+                       if (ZIPC_ALWAYS == insn->i_cnd) {
2103
+                               av->m_known = bknown;
2104
+                               if (bknown == MACH_VSYMKNOWN) {
2105
+                                       if (!bv) { av->m_known = MACH_VUNKNOWN;}
2106
+                                       else {
2107
+                                       av->m_value = bv->m_value + insn->i_imm;
2108
+                                       av->m_addsy = bv->m_addsy;
2109
+                                       av->m_subsy = bv->m_subsy;
2110
+                                       }
2111
+                               }
2112
+                       } else if (
2113
+                               ((av->m_known == MACH_VKNOWN)
2114
+                                 ||(av->m_known == MACH_VUPPERKNOWN))
2115
+                               &&((bknown == MACH_VKNOWN)
2116
+                                 ||(bknown == MACH_VUPPERKNOWN))
2117
+                               &&(((bval^av->m_value)&0xffff0000)==0))
2118
+                               av->m_known = MACH_VUPPERKNOWN;
2119
+                       else
2120
+                               av->m_known = MACH_VUNKNOWN;
2121
+                       av->m_value = bval;
2122
+                       break;
2123
+               case    ZIPO_CLR:
2124
+                       if (insn->i_cnd == ZIPC_ALWAYS)
2125
+                               av->m_known = MACH_VKNOWN;
2126
+                       else if ((av->m_value & 0xffff0000)==0) {
2127
+                               if (((av->m_known == MACH_VKNOWN)
2128
+                                       ||(av->m_known == MACH_VUPPERKNOWN))
2129
+                                       &&(((av->m_value ^ bval)&0xffff0000)==0))
2130
+                                       av->m_known = MACH_VUPPERKNOWN;
2131
+                               else
2132
+                                       av->m_known = MACH_VUNKNOWN;
2133
+                       } av->m_value = 0;
2134
+                       break;
2135
+               // Store's don't change any regs
2136
+               case    ZIPO_SW: case ZIPO_SH: case ZIPO_SB:
2137
+               case    ZIPO_SIM: case ZIPO_NOOP:
2138
+               case    ZIPO_SDUMP: case ZIPO_NDUMP:
2139
+               case    ZIPO_BREAK: case ZIPO_LOCK:
2140
+               case    ZIPO_CMP:  case ZIPO_TST:
2141
+                       // These don't change any registers
2142
+                       break;
2143
+               // Result of loading a byte or halfword always clears the upper
2144
+               // half word
2145
+               case    ZIPO_LB: case   ZIPO_LH:
2146
+                       if (insn->i_cnd == ZIPC_ALWAYS) {
2147
+                               av->m_known = MACH_VUPPERKNOWN;
2148
+                               av->m_value = 0;
2149
+                       } else
2150
+                               av->m_known = MACH_VUNKNOWN;
2151
+                       break;
2152
+               case    ZIPO_LW:
2153
+               default:
2154
+#ifdef ZIP_DEBUG
2155
+                       fprintf(stderr, "MACHINE, DEFAULT, %02x -> unknown\n", insn->i_areg);
2156
+#endif
2157
+                       av->m_known = MACH_VUNKNOWN;
2158
+                       break;
2159
+       }
2160
+
2161
+       pzipm->r[ZIP_CC].m_known = MACH_VUNKNOWN;
2162
+       pzipm->r[ZIP_PC].m_known = MACH_VUNKNOWN;
2163
+}
2164
+
2165
+#ifdef ZIP_DEBUG
2166
+static void
2167
+zip_debug_machine(MACHINEREGS *pzipm) {
2168
+       int     i;
2169
+       for(i=0; i<ZIP_USER_REGS; i++) {
2170
+               if (pzipm->r[i].m_known == MACH_VKNOWN)
2171
+                       fprintf(stderr, "MACH-KNOW[Y][%2x] = %08x\n",
2172
+                               i, pzipm->r[i].m_value);
2173
+               else if (pzipm->r[i].m_known == MACH_VUPPERKNOWN)
2174
+                       fprintf(stderr, "MACH-KNOW[U][%2x] = %04x\n",
2175
+                               i, (pzipm->r[i].m_value>>16)&0x0ffff);
2176
+       }
2177
+}
2178
+#endif
2179
+
2180
+
2181
+/*
2182
+ * Option processing
2183
+ *
2184
+ * While not yet implemented, we do have a need for multiple options.  These
2185
+ * include:
2186
+ *
2187
+ *     (not yet supported)
2188
+ *     -got    Use a global offset table to place unknown jump locations into.
2189
+ *
2190
+ *     (not yet supported)
2191
+ *     -relax  Implement only relaxed instructions.  This implies that all
2192
+ *             branches fit within 18-bits of the current PC, or equivalently
2193
+ *             that all of the code fits within 1MB.
2194
+ *
2195
+ *     (not yet supported)
2196
+ *     -no-relax       Don't relax any instructions.  That means that all
2197
+ *             branches will be implemented as LOD (PC),PC ; .int #Address
2198
+ *             and all conditional branches as LOD 1(PC),PC; BRA 1; .int #addr.
2199
+ *             This also implies LDI's of unknown values will always be
2200
+ *             converted to LDILO/LDIHI pairs and never converted back to
2201
+ *             LDI's--even when the final value is known.
2202
+ *
2203
+ *     -cis    Attempt to compress instructions into the CIS instruction
2204
+ *             set.
2205
+ *
2206
+ *     (Something for stating the starting address of the routine in memory...)
2207
+ *
2208
+ *     Other (not yet supported) long options
2209
+ *
2210
+ *     -nopipe Attempts to use a lock instruction will result in an error.
2211
+ *     -nomul  Attempts to use multiply instructions will result in an error.
2212
+ *     -nodiv  Attempts to use divide instructions will result in an error.
2213
+ *     -nofpu  Attempts to use floating point unit insn will cause an error.
2214
+ *
2215
+ *
2216
+ */
2217
+#define        OPTION_CIS      (OPTION_MD_BASE+1)
2218
+#define        OPTION_NOCIS    (OPTION_MD_BASE+2)
2219
+#define        OPTION_ZIPM     (OPTION_MD_BASE+3)
2220
+#define        OPTION_NOZIPM   (OPTION_MD_BASE+4)
2221
+#define        OPTION_GOT      (OPTION_MD_BASE+5)
2222
+#define        OPTION_SMALL    (OPTION_MD_BASE+6)      // No LW(PC),PC insns
2223
+
2224
+const char     *md_shortopts = "";
2225
+struct option  md_longopts[] =
2226
+{
2227
+       { "cis",   no_argument, NULL, OPTION_CIS },
2228
+       { "nocis", no_argument, NULL, OPTION_NOCIS },
2229
+       { "zipm",   no_argument, NULL, OPTION_ZIPM },
2230
+       { "nozipm", no_argument, NULL, OPTION_NOZIPM },
2231
+       { "small",  no_argument, NULL, OPTION_SMALL },
2232
+       // { "got",  no_argument, NULL, OPTION_GOT },
2233
+       { NULL, no_argument, NULL, 0}
2234
+};
2235
+size_t md_longopts_size = sizeof(md_longopts);
2236
+
2237
+/* We have no target specific options yet, so these next two fucntions are
2238
+ * are empty.
2239
+ */
2240
+int
2241
+md_parse_option(int c, const char *arg ATTRIBUTE_UNUSED)
2242
+{
2243
+       // printf("Option %d, %s\n", c, (arg)?arg : "(Null)");
2244
+       if (c==0)
2245
+               return 1;
2246
+       switch(c) {
2247
+               case 0: return 1;
2248
+               case OPTION_CIS:   zip_param_cis = 1; return 1; break;
2249
+               case OPTION_NOCIS: zip_param_cis = 0; return 1; break;
2250
+               case OPTION_ZIPM:   zip_param_use_machine = 1; return 1; break;
2251
+               case OPTION_NOZIPM: zip_param_use_machine = 0; return 1; break;
2252
+               // case OPTION_GOT : zip_param_got  = 1; return 1; break;
2253
+               default: break;
2254
+       }
2255
+       return 0; // We didn't handle this option
2256
+}
2257
+
2258
+void
2259
+md_show_usage(FILE *stream ATTRIBUTE_UNUSED)
2260
+{
2261
+       fprintf(stream, _("Zip CPU options:\n"));
2262
+       fprintf(stream, _("\n"
2263
+"-cis\t\tAttempt to compress instructions into two instructions per word.\n"));
2264
+       //fprintf(stream, _(
2265
+// "-got\t\tGenerate position independent code by referencing all symbols\n"
2266
+// "\t\tthrough a Global Offset Table.\n"));
2267
+}
2268
+
2269
+
2270
+symbolS *
2271
+md_undefined_symbol(char *name ATTRIBUTE_UNUSED)
2272
+{
2273
+//#warning "This is where other architectures check for any GOT references"
2274
+       return NULL;
2275
+}
2276
+
2277
+void
2278
+md_operand(expressionS *op ATTRIBUTE_UNUSED)
2279
+{
2280
+       /* Empty for now -- what is this for? */
2281
+}
2282
+
2283
+#ifdef ZIP_DEBUG
2284
+static void
2285
+zip_dump_sym(symbolS *sym)
2286
+{
2287
+       if (!sym) {
2288
+               fprintf(stderr, "SYM(NULL)");
2289
+       } else {
2290
+               fprintf(stderr, "Dumping symbol fields\n");
2291
+               fprintf(stderr, "SYM(%s) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s - 0x%08x\n",
2292
+                       S_GET_NAME(sym),
2293
+                       (S_IS_LOCAL(sym))?"Local ":"",
2294
+                       (S_IS_DEFINED(sym))?"Defined ":"",
2295
+                       (S_IS_EXTERNAL(sym))?"extern ":"",
2296
+                       (S_IS_FUNCTION(sym))?"(func) ":"",
2297
+                       (S_IS_WEAK(sym))?"Weak ":"",
2298
+                       (S_IS_WEAKREFD(sym))?"(Weak Ref-D) ":"",
2299
+                       (S_IS_WEAKREFR(sym))?"(Weak Refr) ":"",
2300
+                       (S_IS_DEBUG(sym))?"DEBUG ":"",
2301
+                       (S_IS_VOLATILE(sym))?"Volatile ":"",
2302
+                       (S_IS_FORWARD_REF(sym))?"Fwd Ref ":"",
2303
+                       (S_IS_COMMON(sym))?"Common ":"",
2304
+                       (S_GET_SEGMENT(sym)==absolute_section)?"AbsoluteS ":"",
2305
+                       (S_GET_SEGMENT(sym)==expr_section)?"ExpressionS ":"",
2306
+                       (S_GET_SEGMENT(sym)==reg_section)?"RegisterS ":"",
2307
+                       (S_GET_SEGMENT(sym)==undefined_section)?"UndefinedS ":"",
2308
+                       (symbol_resolved_p(sym))?"Resolved ":"",
2309
+                       (unsigned)S_GET_VALUE(sym));
2310
+       }
2311
+}
2312
+
2313
+static void
2314
+zip_dump_insn(ZIPIS *insn) {
2315
+       fprintf(stderr, "INSN:DUMP ");
2316
+       switch(insn->i_op) {
2317
+               case ZIPO_SUB:   fprintf(stderr, "%7s", "SUB"); break;
2318
+               case ZIPO_AND:   fprintf(stderr, "%7s", "AND"); break;
2319
+               case ZIPO_ADD:   fprintf(stderr, "%7s", "ADD"); break;
2320
+               case ZIPO_OR:    fprintf(stderr, "%7s", "OR"); break;
2321
+               case ZIPO_XOR:   fprintf(stderr, "%7s", "XOR"); break;
2322
+               case ZIPO_LSR:   fprintf(stderr, "%7s", "LSR"); break;
2323
+               case ZIPO_LSL:   fprintf(stderr, "%7s", "LSL"); break;
2324
+               case ZIPO_ASR:   fprintf(stderr, "%7s", "ASR"); break;
2325
+               case ZIPO_LDI:   fprintf(stderr, "%7s", "LDI"); break;
2326
+               case ZIPO_MPYUHI:fprintf(stderr, "%7s", "MPYUHI");break;
2327
+               case ZIPO_MPYSHI:fprintf(stderr, "%7s", "MPYSHI");break;
2328
+               case ZIPO_LDILO: fprintf(stderr, "%7s", "LDILO"); break;
2329
+               case ZIPO_BREV:  fprintf(stderr, "%7s", "BREV");  break;
2330
+               case ZIPO_MOV:   fprintf(stderr, "%7s", "MOV");   break;
2331
+               case ZIPO_CMP:   fprintf(stderr, "%7s", "CMP");   break;
2332
+               case ZIPO_TST:   fprintf(stderr, "%7s", "TST");   break;
2333
+               case ZIPO_LW:    fprintf(stderr, "%7s", "LW");    break;
2334
+               case ZIPO_SW:    fprintf(stderr, "%7s", "SW");    break;
2335
+               case ZIPO_LH:    fprintf(stderr, "%7s", "LH");    break;
2336
+               case ZIPO_SH:    fprintf(stderr, "%7s", "SH");    break;
2337
+               case ZIPO_LB:    fprintf(stderr, "%7s", "LB");    break;
2338
+               case ZIPO_SB:    fprintf(stderr, "%7s", "SB");    break;
2339
+               case ZIPO_DIVU:  fprintf(stderr, "%7s", "DIVU");  break;
2340
+               case ZIPO_DIVS:  fprintf(stderr, "%7s", "DIVS");  break;
2341
+               case ZIPO_FPADD: fprintf(stderr, "%7s", "FPADD"); break;
2342
+               case ZIPO_FPSUB: fprintf(stderr, "%7s", "FPSUB"); break;
2343
+               //
2344
+               case ZIPO_NOOP:  fprintf(stderr, "%7s", "NOOP");  break;
2345
+               case ZIPO_BREAK: fprintf(stderr, "%7s", "BREAK"); break;
2346
+               case ZIPO_LOCK:  fprintf(stderr, "%7s", "LOCK");  break;
2347
+               case ZIPO_TRAP:  fprintf(stderr, "%7s", "TRAP");  break;
2348
+               case ZIPO_CLR:   fprintf(stderr, "%7s", "CLR");   break;
2349
+               case ZIPO_HALT:  fprintf(stderr, "%7s", "HALT");  break;
2350
+               case ZIPO_WAIT:  fprintf(stderr, "%7s", "WAIT");  break;
2351
+               case ZIPO_STEP:  fprintf(stderr, "%7s", "STEP");  break;
2352
+               case ZIPO_RTU:   fprintf(stderr, "%7s", "RTU");   break;
2353
+               case ZIPO_BRA:   fprintf(stderr, "%7s", "BRA");   break;
2354
+               case ZIPO_BUSY:  fprintf(stderr, "%7s", "BUSY");  break;
2355
+               case ZIPO_JMP:   fprintf(stderr, "%7s", "JMP");   break;
2356
+               case ZIPO_NOT:   fprintf(stderr, "%7s", "NOT");   break;
2357
+               case ZIPO_NEG:   fprintf(stderr, "%7s", "NEG");   break;
2358
+               //
2359
+               case ZIPO_SIM:   fprintf(stderr, "%7s", "SIM");   break;
2360
+               case ZIPO_SDUMP: fprintf(stderr, "%7s", "SDUMP"); break;
2361
+               case ZIPO_SEXIT: fprintf(stderr, "%7s", "SEXIT"); break;
2362
+               case ZIPO_SOUT:  fprintf(stderr, "%7s", "SOUT");  break;
2363
+               case ZIPO_NDUMP: fprintf(stderr, "%7s", "NDUMP"); break;
2364
+               case ZIPO_NEXIT: fprintf(stderr, "%7s", "NEXIT"); break;
2365
+               case ZIPO_NOUT:  fprintf(stderr, "%7s", "OUT");   break;
2366
+               //
2367
+               case ZIPO_LJMP: fprintf(stderr, "%7s", "LJMP"); break;
2368
+               case ZIPO_LJSR: fprintf(stderr, "%7s", "LJSR");   break;
2369
+               //
2370
+               case ZIPO_SEXTH:fprintf(stderr, "%7s", "SEXTH");   break;
2371
+               case ZIPO_SEXTB:fprintf(stderr, "%7s", "SEXTB");   break;
2372
+               default:
2373
+                       fprintf(stderr, "%d", insn->i_op); break;
2374
+       }
2375
+       switch(insn->i_cnd) {
2376
+               case ZIPC_Z: fprintf(stderr, "%-3s", ".Z"); break;
2377
+               case ZIPC_LT: fprintf(stderr, "%-3s", ".LT"); break;
2378
+               case ZIPC_C: fprintf(stderr, "%-3s", ".C"); break;
2379
+               case ZIPC_V: fprintf(stderr, "%-3s", ".V"); break;
2380
+               case ZIPC_NZ: fprintf(stderr, "%-3s", ".NE"); break;
2381
+               case ZIPC_GE: fprintf(stderr, "%-3s", ".GE"); break;
2382
+               case ZIPC_NC: fprintf(stderr, "%-3s", ".NC"); break;
2383
+               case ZIPC_ALWAYS:
2384
+               default:
2385
+                       break;
2386
+       } fprintf(stderr, " %d", (int)insn->i_cnd);
2387
+
2388
+       fprintf(stderr, "\n\tAREG = %d\n\tB = ", insn->i_areg);
2389
+       if (insn->i_rp) {
2390
+               if (insn->i_imm != 0)
2391
+                       fprintf(stderr, "$%d + ", insn->i_imm);
2392
+               fprintf(stderr, "%s ", (insn->i_rp->r_sym) ?
2393
+                       S_GET_NAME(insn->i_rp->r_sym) : "(null)");
2394
+       } else
2395
+               fprintf(stderr, "%d[%08x] (no sym)", insn->i_imm, insn->i_imm);
2396
+       if (insn->i_breg != ZIP_RNONE)
2397
+               fprintf(stderr, "+ R%d", insn->i_breg);
2398
+       fprintf(stderr, "\n");
2399
+       if (insn->i_rp)
2400
+               fprintf(stderr, "\t@%ld (offset w/in instruction frag)\n", insn->i_rp->r_fr_offset);
2401
+       fprintf(stderr, "\tINSN:CODE %08x", insn->i_code);
2402
+       {
2403
+               int     i;
2404
+               for(i=0; (i<insn->i_naux)&&(i<ZIP_MAX_NAUX); i++)
2405
+                       fprintf(stderr, ":%08x", insn->i_aux[i]);
2406
+       }
2407
+       fprintf(stderr, "\n\tDUMPED\n");
2408
+}
2409
+#endif
2410
+
2411
+static ZIP_CONDITION
2412
+zip_negate_condition(ZIP_CONDITION c) {
2413
+       switch(c) {
2414
+               case ZIPC_Z:    return ZIPC_NZ; break;
2415
+               case ZIPC_LT:   return ZIPC_GE; break;
2416
+               case ZIPC_C:    return ZIPC_NC; break;
2417
+               case ZIPC_NZ:   return ZIPC_Z; break;
2418
+               case ZIPC_GE:   return ZIPC_LT; break;
2419
+               case ZIPC_NC:   return ZIPC_C; break;
2420
+               case ZIPC_ALWAYS:       return ZIPC_ALWAYS; break;
2421
+               default:
2422
+                       break;
2423
+       } gas_assert((0)&&("Cannot negate condition\n"));
2424
+       return ZIPC_ALWAYS;
2425
+}
2426
+
2427
+static const char *zip_skip_white_spaces(const char *str) {
2428
+       if (!str)
2429
+               return NULL;
2430
+       while((*str)&&(isspace(*str)))
2431
+               str++;
2432
+       return str;
2433
+}
2434
+
2435
+static const char *
2436
+zip_parse_condition(const char *str, ZIP_CONDITION *condn) {
2437
+       if ((strcasecmp(str, "Z")==0)
2438
+                       ||(strcasecmp(str, "EQ")==0)) {
2439
+               *condn = ZIPC_Z;
2440
+       } else if ((strcasecmp(str, "LT")==0)
2441
+                       ||(strcasecmp(str, "N")==0)) {
2442
+               *condn = ZIPC_LT;
2443
+       } else if ((strcasecmp(str, "C")==0)
2444
+                       ||(strcasecmp(str, "LTU")==0)) {
2445
+               *condn = ZIPC_C;
2446
+       } else if (strcasecmp(str, "V")==0) {
2447
+               *condn = ZIPC_V;
2448
+       } else if((strcasecmp(str, "NZ")==0)
2449
+                       ||(strcasecmp(str, "NE")==0)) {
2450
+               *condn = ZIPC_NZ;
2451
+       } else if ((strcasecmp(str, "GE")==0)
2452
+                       ||(strcasecmp(str, "GTE")==0)) {
2453
+               *condn = ZIPC_GE;
2454
+       } else if ((strcasecmp(str, "NC")==0)
2455
+                       ||(strcasecmp(str, "GEU")==0)) {
2456
+               *condn = ZIPC_NC;
2457
+       } else {
2458
+               return "Unrecognized condition";
2459
+       }
2460
+
2461
+       return NULL;
2462
+}
2463
+
2464
+static const char *zip_parse_reg(const char *str, ZIP_REG *regid) {
2465
+       const char *ustr = str;
2466
+       int     userreg = 0;
2467
+
2468 206 dgisselq
+       if ((!str)||(str[0]=='\0'))
2469
+               return "No register given";
2470 202 dgisselq
+       ustr = zip_skip_white_spaces(str);
2471
+
2472
+       if (toupper(ustr[0]) == 'U') {
2473
+               ustr = str+1;
2474
+               userreg = 0x10;
2475
+       } else if ((toupper(ustr[0]) == 'S')&&(strcasecmp(ustr, "SP")!=0)) {
2476
+               ustr = str+1;
2477
+       }
2478
+
2479
+       /*
2480
+       if (strcasecmp(ustr, "GBL")==0) {
2481
+               *regid = userreg + 11;
2482
+               ustr += 3;
2483
+       } else
2484
+       */
2485
+       if (strcasecmp(ustr, "LR")==0) {
2486
+               *regid = userreg + 0;
2487
+               ustr += 2;
2488
+       } else if (strcasecmp(ustr, "FP")==0) {
2489
+               *regid = userreg + 12;
2490
+               ustr += 2;
2491
+       } else if (strcasecmp(ustr, "SP")==0) {
2492
+               *regid = userreg + 13;
2493
+               ustr += 2;
2494
+       } else if (strcasecmp(ustr, "CC")==0) {
2495
+               *regid = userreg + 14;
2496
+               ustr += 2;
2497
+       } else if (strcasecmp(ustr, "PC")==0) {
2498
+               *regid = userreg + 15;
2499
+               ustr += 2;
2500
+       } else if (('r' == tolower(ustr[0]))
2501
+                       &&(isdigit(ustr[1]))
2502
+                       &&((!isdigit(ustr[2]))
2503
+                               ||(!isdigit(ustr[3])))) {
2504
+               *regid = atoi(ustr+1);
2505
+               if ((*regid > 15)||(*regid < 0))
2506
+                       return "Register does not exist";
2507
+               *regid += userreg;
2508
+
2509
+               if (!isdigit(ustr[2]))
2510
+                       ustr+=2;
2511
+               else
2512
+                       ustr+=3;
2513
+       } else {
2514
+               *regid = ZIP_RNONE;
2515
+               return "Unknown register";
2516
+       }
2517
+
2518
+       // Registers names are terminated by something other than letters
2519
+       // and numbers.  Things like ')', ',', or '\0' should terminate a
2520
+       // register.  Here, we only double check that the register is not
2521
+       // terminated by another letter or a number.
2522
+       if ((*ustr)&&((isalpha(*ustr))||(isdigit(*ustr))))
2523
+               return "Unrecognized register";
2524
+
2525
+       return NULL;
2526
+}
2527
+
2528
+// Parse a 'B' operand
2529
+static const char *
2530
+zip_parse_bop(const char *bop, ZIPIS *insn) {
2531
+       // There are a couple forms for what we can expect in a B operand:
2532
+       //      The first three require no relocations ...
2533
+       //              1. A simple number
2534
+       //              2. Number + Register
2535
+       //              3. Number(Register)
2536
+       //              4. Register by itself
2537
+       //              Good form is to replace this number with the possibility
2538
+       //              of a constant expression ...  The number may be any of
2539
+       //              [+-](0[xX][0-9a-fA-F]+|(0[0-7]+)|(0-9)+)
2540
+       //      The next four may require a relocation
2541
+       //              4. Label + Register
2542
+       //              5. Label(Register)
2543
+       //              6. Label (Register is implied: PC, if moving or jumping
2544
+       //                      to the PC, or GBL if loading a value or if
2545
+       //                      the offset ends up being unknown)
2546
+       //              Good form allows an expression instead of a label,
2547
+       //              that can be evaluated at ... sometime.
2548
+       //              7. Number(Label)
2549
+       //
2550
+       // We will support:
2551
+       //      (Number|Label)?( "("Register")" | "+"Register )
2552
+       //
2553
+       char    lbl[512], *lblp = lbl;
2554
+       const char      *ptr;
2555
+       insn->i_imm = 0;
2556
+       *lblp = '\0';
2557
+
2558
+       if (strlen(bop)>sizeof(lbl)-1)
2559
+               as_fatal( _("Label length too long"));
2560
+       // printf("RAW-OP-B: %s %s\n", bop, (insn->i_rp)?"(i_rp != NULL)":"");
2561
+
2562
+       // Do we start with a number?
2563
+       {
2564
+               int     sgn = 0;
2565
+
2566
+               ptr = zip_skip_white_spaces(bop);
2567
+               if ('$' == *ptr)
2568
+                       ptr = zip_skip_white_spaces(ptr+1);
2569
+
2570
+               if ('+' == *ptr)
2571
+                       ptr++;
2572
+               else if ('-' == *ptr) {
2573
+                       sgn = 1;
2574
+                       ptr++;
2575
+               }
2576
+
2577
+               if ('$' == *ptr)
2578
+                       ptr = zip_skip_white_spaces(ptr+1);
2579
+               ptr = zip_skip_white_spaces(ptr);
2580
+               if ('$' == *ptr)
2581
+                       ptr = zip_skip_white_spaces(ptr+1);
2582
+
2583
+               if ((*ptr)&&(isdigit(*ptr))) {
2584
+                       char *end = (char *)ptr;
2585
+                       unsigned long v = strtoul(ptr, &end, 0);
2586
+                       // We start with a number
2587
+                       if (sgn)
2588
+                               insn->i_imm = (int)(-v);
2589
+                       else
2590
+                               insn->i_imm = (int)(v);
2591
+                       ptr = (const char *)end;
2592
+
2593
+                       // Permit a string of Num [[+-] Num]*
2594
+                       // This is necessary for the DI instructions within the
2595
+                       // compiler.
2596
+                       while( ((*ptr == '+')||(*ptr == '-'))
2597
+                               &&(isdigit(ptr[1])) ) {
2598
+                               sgn = (*ptr == '-');
2599
+                               v = strtoul(&ptr[1], &end, 0);
2600
+                               if (sgn) v = -v;
2601
+                               insn->i_imm += (int)v;
2602
+                               ptr = (const char *)end;
2603
+                       }
2604
+               } else if ((*ptr)&&(
2605
+                               (isalpha(*ptr))
2606
+                               ||('*'==*ptr)
2607
+                               ||('.'==*ptr)
2608
+                               ||('_'==*ptr))) {
2609
+                       // We start with an identifier
2610
+                       // printf("OP-B ( \'%s\' ) starts with an identifier (%c)\n",
2611
+                               // bop, *ptr);
2612
+
2613
+                       // Skip any compiler inserted prefix (if present)
2614
+                       if ('*' == *ptr)
2615
+                               ptr++;
2616
+                       while((*ptr)&&(
2617
+                                       (isalpha(*ptr))
2618
+                                       ||(isdigit(*ptr))
2619
+                                       ||('_' == *ptr)
2620
+                                       ||('$' == *ptr)
2621
+                                       ||('.' == *ptr)))
2622
+                               *lblp++ = *ptr++;
2623
+                       *lblp = '\0';
2624
+                       // printf("LBL was %s\n", lbl);
2625
+
2626
+                       // This could still be a register ... can't tell yet
2627
+                       if (sgn)
2628
+                               return "ERR: Not expecting a signed label!";
2629
+
2630
+                       ptr = zip_skip_white_spaces(ptr);
2631
+               }
2632
+       }
2633
+
2634
+       ptr = zip_skip_white_spaces(ptr);
2635
+
2636
+       const   char *err = NULL;
2637
+
2638
+       if ((*ptr == '+')&&(ptr[1] == '('))
2639
+               ptr++;
2640
+       if ((*ptr)&&(*ptr == '(')) {
2641
+               // Form #3: Number(register)
2642
+               char *end = strchr(ptr+1, ')');
2643
+               if (NULL == end)
2644
+                       return "Un-matched \'(\', cannot find \')\'";
2645
+               *end = '\0';
2646
+               // printf("Looking for a register in %s\n", ptr+1);
2647
+               err = zip_parse_reg(ptr+1, &insn->i_breg);
2648
+               if (err) {
2649
+                       // Must've been a symbol
2650
+                       if (lbl[0] != '\0') // Already have a symbol in this
2651
+                               return err;     // expression!
2652
+                       ptr++;
2653
+                       while((*ptr)&&(
2654
+                                       (isalpha(*ptr))
2655
+                                       ||(isdigit(*ptr))
2656
+                                       ||('_' == *ptr)
2657
+                                       ||('$' == *ptr)
2658
+                                       ||('.' == *ptr)))
2659
+                               *lblp++ = *ptr++;
2660
+                       *lblp = '\0';
2661
+                       insn->i_breg = ZIP_RNONE;
2662
+                       ptr = zip_skip_white_spaces(ptr);
2663
+                       if (*ptr != '\0')
2664
+                               return "ERR: Expression within parenthesis not supported";
2665
+                       err = NULL;
2666
+               }
2667
+               // printf("Found a register, %s -> %d\n", ptr+1, insn->i_breg);
2668
+       } else if ((*ptr)&&((*ptr == '+')||(*ptr == '-'))) {
2669
+               if ((*lbl)&&(zip_parse_reg(lbl, &insn->i_breg) == NULL)) {
2670
+                       // Register+Number
2671
+                       // Skip to the end to process what follows
2672
+                       *lbl = '\0'; // Label wasn't a symbol, so let's clear it
2673
+               } else {
2674
+                       // Number/label+Register
2675
+                       while((('+' == *ptr)||('-' == *ptr))
2676
+                                       &&(ptr[1])
2677
+                                       &&((isdigit(ptr[1]))
2678
+                                       ||((ptr[0] == '+')
2679
+                                               &&(ptr[1] == '-')
2680
+                                               &&(ptr[2])
2681
+                                               &&(isdigit(ptr[2]))))) {
2682
+                               char *end;
2683
+
2684
+                               if ((*ptr == '+')&&(isdigit(ptr[1])))
2685
+                                       insn->i_imm += strtoul(ptr, &end, 0);
2686
+                               else if ((*ptr == '+')&&(ptr[1] == '-')
2687
+                                               &&(isdigit(ptr[2])))
2688
+                                       insn->i_imm -= strtoul(ptr+2, &end, 0);
2689
+                               else if ((*ptr == '-')&&(isdigit(ptr[1])))
2690
+                                       insn->i_imm -= strtoul(ptr+1, &end, 0);
2691
+                               else {
2692
+                                       fprintf(stderr, "GAS: Cannot comprehend %s\n", ptr);
2693
+                                       gas_assert((0)&&("Should never get here"));
2694
+                               }
2695
+                               ptr = (const char *)end;
2696
+                       }
2697
+                       if (('+' == *ptr)&&((err = zip_parse_reg(ptr+1, &insn->i_breg))==NULL)) {
2698
+                               // Let's skip to the end of the register
2699
+                               ptr++;
2700
+                               while(isalpha(*ptr))
2701
+                                       ptr++;
2702
+                               while(isdigit(*ptr))
2703
+                                       ptr++;
2704
+                       } else if (('(' == *ptr)&&((err = zip_parse_reg(ptr+1, &insn->i_breg))==NULL)) {
2705
+                               ptr++;
2706
+                               while((*ptr)&&(')' != *ptr))
2707
+                                       ptr++;
2708
+                               ptr++;
2709
+                       } else {
2710
+                               // OOps!! Must've been a label + number
2711
+                               err = NULL;
2712
+                       }
2713
+               }
2714
+       } else if ((*lbl)&&(NULL == zip_parse_reg(lbl, &insn->i_breg))) {
2715
+               // Form: Register (only)
2716
+               insn->i_imm = 0;
2717
+               // printf("OP-B ( \'%s\' ) Had a register, %s -> %d\n", bop,
2718
+                       // lbl, insn->i_breg);
2719
+               *lbl = '\0';
2720
+       } else if (*lbl) {
2721
+               // Form: Label or Number (only)
2722
+               insn->i_breg = ZIP_RNONE;
2723
+       }
2724
+
2725
+       // Look for a +number at the end
2726
+       if ((*ptr)&&((*ptr == '+')||(*ptr == '-'))) {
2727
+               // printf("Looking at a plus: %s\n", ptr);
2728
+               int     sgn = (*ptr == '-')?1:0;
2729
+
2730
+               if (sgn) {
2731
+                       // printf("... I meant a minus\n");
2732
+                       ptr++;
2733
+               }
2734
+
2735
+               ptr = zip_skip_white_spaces(ptr);
2736
+               if ('$' == *ptr)
2737
+                       ptr = zip_skip_white_spaces(ptr+1);
2738
+
2739
+               if ('+' == *ptr)
2740
+                       ptr++;
2741
+               if ('-' == *ptr) {
2742
+                       sgn = !sgn;
2743
+                       ptr++;
2744
+               }
2745
+
2746
+               ptr = zip_skip_white_spaces(ptr);
2747
+               if ('$' == *ptr)
2748
+                       ptr = zip_skip_white_spaces(ptr+1);
2749
+               if ('-' == *ptr) {
2750
+                       sgn = !sgn;
2751
+                       ptr++;
2752
+               }
2753
+
2754
+               if ((*ptr)&&(isdigit(*ptr))) {
2755
+                       char *end = (char *)ptr;
2756
+
2757
+                       // printf("Parsing #: %s\n", ptr);
2758
+                       // While the following might make the most sense,
2759
+                       //    if (sgn) *--ptr = '-';
2760
+                       // the string is read-only, so we can't change it.
2761
+                       // Instead we do ...
2762
+                       unsigned long v = (sgn)?
2763
+                               -strtoul(ptr, &end, 0) : strtoul(ptr, &end, 0);
2764
+                       insn->i_imm += v;
2765
+                       ptr = (const char *)end;
2766
+               }
2767
+       }
2768
+
2769
+       if (*lbl) {
2770
+               // printf("i_rp = %s\n", (insn->i_rp)?"(Null)":"not NULL");
2771
+               symbolS *sym = symbol_find_or_make(lbl);
2772
+               sym->sy_flags.sy_used = TRUE;
2773
+               // segT seg = S_GET_SEGMENT(sym);
2774
+#ifdef ZIP_DEBUG
2775
+               zip_dump_sym(sym);
2776
+#endif
2777
+               if (insn->i_breg == ZIP_PC) {
2778
+                       // New pc-relative relocation,
2779
+                       // ???
2780
+                       // symbolP = symbol_create(FAKE_LABEL_NAME,
2781
+                               // absolute_section, 0 &zero_address_frag);
2782
+                       // symbol_set_value_expression(symbolP, expressionP);
2783
+                       // resolve_symbol_value(symbolP);
2784
+                       insn->i_rp =(ZIPRELOC *)xmalloc(sizeof(ZIPRELOC));
2785
+                       insn->i_rp->r_sym = sym;
2786
+                       insn->i_rp->r_pcrel = TRUE;
2787
+                       insn->i_rp->r_fr_offset = 0;
2788
+                       insn->i_rp->r_fix = NULL;
2789
+                       insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_PCREL;
2790
+                       // symbol_make(name??)
2791
+                       // symbol_find_or_make(name)
2792
+                       // symbol_relc_make_sym
2793
+                       // symbol_relc_make_value
2794
+                       // symbol_find(name)
2795
+                       // symbol_find_noref(name, noref)
2796
+                       // symbol_find_exact(name)
2797
+                       // symbol_find_exact_noref(name, noref)
2798
+                       // symbol_find_or_make(name)
2799
+                       // symbol_make(name)
2800
+                       // symbol_new(name, seg, value, frag)
2801
+                       //      preferred call over symbol create
2802
+                       //      calls symbol_create internal
2803
+                       // symbol_create(name, segment, value, frag)
2804
+                       //
2805
+                       // local_symbol_make(name, section, value, frag)
2806
+                       // symbol_clone(sym, int)
2807
+                       // symbol_temp_new(seg, value, frag)
2808
+                       // symbol_temp_new_now(void)
2809
+                       // symbol_temp_make(void)
2810
+                       // colon(void)
2811
+                       //      Called when symbol: starts a line
2812
+                       //      Calls symbol_new(name, now_seg, frag_now_fix(),
2813
+                       //              frag_now);
2814
+                       //              symbol_table_insert(symbolP)
2815
+                       //
2816
+                       // expr_build_dot returns a symbol pointing to the
2817
+                       //      current location ...
2818
+                       //
2819
+                       // Useful:
2820
+                       //      frag_now is current frag
2821
+                       //      S_SET_VALUE(symbolP, frag_now_fix()) ???
2822
+                       //      now_seg must be the current segment
2823
+                       //      S_SET_SETGMENT(symbolP, now_seg);
2824
+               /*
2825
+               } else if((zip_param_got)&&(insn->i_breg == ZIP_GBL)) {
2826
+                       // New GOT-relative relocation
2827
+                       insn->i_rp =(ZIPRELOC *)xmalloc(sizeof(ZIPRELOC));
2828
+                       insn->i_rp->r_sym = sym;
2829
+                       insn->i_rp->r_pcrel = FALSE;
2830
+                       insn->i_rp->r_fr_offset = 0;
2831
+                       insn->i_rp->r_fix = NULL;
2832
+               */
2833
+               } else {
2834
+                       insn->i_rp =(ZIPRELOC *)xmalloc(sizeof(ZIPRELOC));
2835
+                       insn->i_rp->r_sym = sym;
2836
+                       insn->i_rp->r_pcrel = FALSE;
2837
+                       insn->i_rp->r_fr_offset = 0;
2838
+                       insn->i_rp->r_fix = NULL;
2839
+                       if (insn->i_breg != ZIP_RNONE)
2840
+                               insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_OFFSET;
2841
+                       else
2842
+                               insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_IMM;
2843
+               }
2844
+       }
2845
+
2846
+       return  err;
2847
+}
2848
+
2849
+static int     fits_within(int nbits, int value) {
2850
+       // -2 fits_within two bits
2851
+       // -1 fits_within two bits
2852
+       //  1 fits_within two bits
2853
+       //  2 does not
2854
+       //
2855
+       if (value > 0)
2856
+               return (value <   (1l<<(nbits-1))) ? 1:0;
2857
+       else
2858
+               return (value >= -(1l<<(nbits-1))) ? 1:0;
2859
+}
2860
+
2861
+static const char *zip_parse(const char *line, ZIPIS *insn) {
2862
+       const char      *err = NULL, *opstr = NULL;
2863
+       char    *alt;
2864
+       int     i;
2865
+       char *cndp = NULL;
2866
+       // Two (possible) tokens left:
2867
+       //      the left of the comma, and the right of the comma
2868
+       const char      *left, *right, *eol;
2869
+       typedef enum    {
2870
+               TWO_OP,         // Standard for most instructions
2871
+               IMM_OP,         // Immediate operand only
2872
+               BACKWARDS_TWO,  // Only used by STO REG,Off(REG)
2873
+               NO_OP,          // Used by NOOP, BUSY, HALT, RTU, LOCK, etc
2874
+               ONE_OR_TWO_OP,  // Only used by TST
2875
+               MAYBE_ONE_IMM,  // Only used by BREAK, NOOP, SIM
2876
+               MAYBE_ONE_REG,  // Only used by SDUMP, NDUMP
2877
+               MAYBE_ONE_IMM_OR_REG,   // Only used by SEXIT, NEXIT, SOUT, NOUT
2878
+               MAYBE_OPB,      // Only used by TRAP
2879
+               ONE_REGISTER,   // Only used by CLR, CLRF, and NOT
2880
+               OP_ADDRESS,     // Only used by BRA, BRA.C, and LINK
2881
+               OP_B,           // Only used by JMP
2882
+               TWO_REGISTER,   // Only used by FP instructions
2883
+               ILLEGAL_FORM    // Never used, for debugging only
2884
+       } FORMTYPE;
2885
+
2886
+       FORMTYPE        insn_form = ILLEGAL_FORM; // Make sure we set this
2887
+
2888
+
2889
+
2890
+#ifdef ZIP_DEBUG
2891
+       fprintf(stderr, "**** Parsing %s\n", line);
2892
+#endif
2893
+
2894
+       insn->i_naux = 0;
2895
+       insn->i_op   = ZIPO_NOOP;
2896
+       insn->i_cnd  = ZIPC_ALWAYS;
2897
+       insn->i_areg = ZIP_RNONE;
2898
+       insn->i_breg = ZIP_RNONE;
2899
+       insn->i_imm  = 0;
2900
+       insn->i_rp   = NULL;
2901
+       insn->i_code = NOOP_OPCODE;
2902
+       for(i=0; i<ZIP_MAX_NAUX; i++)
2903
+               insn->i_aux[i]  = NOOP_OPCODE;
2904
+
2905
+       // The opcode is given between whitespace and a period, or whitespace
2906
+       // and whitespace
2907
+       alt = strdup(zip_skip_white_spaces(line));
2908
+       if ((*alt)=='[') {
2909
+               // Instruction starts with condition codes-- a feature we
2910
+               // needed to add in order to support GCC conditional execution
2911
+               // macro
2912
+               cndp = strtok(alt,"] \t")+1;
2913
+               if (!cndp)
2914
+                       return "Mismatched parenthesis--an attempt at a condition?";
2915
+               if (strlen(cndp) > 3)
2916
+                       return "Invalid condition (too long)";
2917
+
2918
+               opstr = strtok(NULL, " \t");
2919
+               if (!opstr)
2920
+                       return "Condition not followed by valid opcode";
2921
+       } else {
2922
+               opstr = strtok(alt, " \t"); // Get our opcode
2923
+               if (!opstr) {
2924
+                       free(alt);
2925
+                       return "Invalid Instruction";
2926
+               }
2927
+       }
2928
+       // See if our token contains a '.' within it
2929
+       //      GCC allows conditions beginning a line, as in ...
2930
+       //              (CND)   Opcode  B,A
2931
+       //      such a condition, if present, was detected above.
2932
+       //
2933
+       //      If not, we now look for a condition written in our original
2934
+       //      format of ...
2935
+       //              Opcode.CND B,A
2936
+       //      and we look for it here.
2937
+       //
2938
+       if (!cndp) {
2939
+               cndp = strchr(opstr, '.');
2940
+               // If we found such a condition, we need to fix the opstr
2941
+               // so that it no longer includes the condition.  Hence, let's
2942
+               // place a NULL within the alt string and push our condition
2943
+               // forward to the first non '.' value.
2944
+               if (cndp)
2945
+                       *cndp++ = '\0';
2946
+       }
2947
+       if (strcasecmp(opstr, "SUB")==0) {
2948
+               insn->i_op = ZIPO_SUB;
2949
+               insn_form = TWO_OP;
2950
+       } else if (strcasecmp(opstr, "AND")==0) {
2951
+               insn->i_op = ZIPO_AND;
2952
+               insn_form = TWO_OP;
2953
+       } else if (strcasecmp(opstr, "ADD")==0) {
2954
+               insn->i_op = ZIPO_ADD;
2955
+               insn_form = TWO_OP;
2956
+       } else if (strcasecmp(opstr, "OR")==0) {
2957
+               insn->i_op = ZIPO_OR;
2958
+               insn_form = TWO_OP;
2959
+       } else if (strcasecmp(opstr, "XOR")==0) {
2960
+               insn->i_op = ZIPO_XOR;
2961
+               insn_form = TWO_OP;
2962
+       } else if (strcasecmp(opstr, "LSR")==0) {
2963
+               insn->i_op = ZIPO_LSR;
2964
+               insn_form = TWO_OP;
2965
+       } else if (strcasecmp(opstr, "LSL")==0) {
2966
+               insn->i_op = ZIPO_LSL;
2967
+               insn_form = TWO_OP;
2968
+       } else if (strcasecmp(opstr, "ASR")==0) {
2969
+               insn->i_op = ZIPO_ASR;
2970
+               insn_form = TWO_OP;
2971
+       } else if (strcasecmp(opstr, "BREV")==0) {
2972
+               insn->i_op = ZIPO_BREV;                 // BREV b+Rb,Ra, or
2973
+               insn_form = ONE_OR_TWO_OP;              // BREV Rx -> BREV Rx,Rx
2974
+       } else if((strcasecmp(opstr, "LDILO")==0)
2975
+                       ||(strcasecmp(opstr, "LLO")==0)) {
2976
+               insn->i_op = ZIPO_LDILO;
2977
+               insn_form = TWO_OP;
2978
+       } else if (strcasecmp(opstr, "MPYUHI")==0) {    // MPUHI
2979
+               insn->i_op = ZIPO_MPYUHI;
2980
+               insn_form = TWO_OP;
2981
+       } else if (strcasecmp(opstr, "MPYSHI")==0) {    // MPSHI
2982
+               insn->i_op = ZIPO_MPYSHI;
2983
+               insn_form = TWO_OP;
2984
+       } else if (strcasecmp(opstr, "MPY")==0) {
2985
+               insn_form = TWO_OP;
2986
+               insn->i_op = ZIPO_MPY;
2987
+       } else if (strcasecmp(opstr, "MOV")==0) {
2988
+               insn->i_op = ZIPO_MOV;
2989
+               insn_form = TWO_OP;
2990
+       } else if (strcasecmp(opstr, "DIVU")==0) {
2991
+               insn->i_op = ZIPO_DIVU;
2992
+               insn_form = TWO_OP;
2993
+       } else if (strcasecmp(opstr, "DIVS")==0) {
2994
+               insn->i_op = ZIPO_DIVS;
2995
+               insn_form = TWO_OP;
2996
+       } else if (strcasecmp(opstr, "CMP")==0) {
2997
+               insn->i_op = ZIPO_CMP;
2998
+               insn_form = TWO_OP;
2999
+       } else if ((strcasecmp(opstr, "TST")==0)
3000
+               ||(strcasecmp(opstr, "TEST")==0)) {
3001
+               insn->i_op = ZIPO_TST;                  // TST b+Rb,Ra, or
3002
+               insn_form = ONE_OR_TWO_OP;              // TST Rx -> TST -1,Rx
3003
+       } else if (strcasecmp(opstr, "LW")==0) {
3004
+               insn->i_op = ZIPO_LW;
3005
+               insn_form = TWO_OP;
3006
+       } else if (strcasecmp(opstr, "SW")==0) {
3007
+               insn->i_op = ZIPO_SW;
3008
+               insn_form = BACKWARDS_TWO;
3009
+       } else if (strcasecmp(opstr, "LH")==0) {
3010
+               insn->i_op = ZIPO_LH;
3011
+               insn_form = TWO_OP;
3012
+       } else if (strcasecmp(opstr, "SH")==0) {
3013
+               insn->i_op = ZIPO_SH;
3014
+               insn_form = BACKWARDS_TWO;
3015
+       } else if (strcasecmp(opstr, "LB")==0) {
3016
+               insn->i_op = ZIPO_LB;
3017
+               insn_form = TWO_OP;
3018
+       } else if (strcasecmp(opstr, "SB")==0) {
3019
+               insn->i_op = ZIPO_SB;
3020
+               insn_form = BACKWARDS_TWO;
3021
+       } else if (strcasecmp(opstr, "LDI")==0) {
3022
+               insn->i_op = ZIPO_LDI;
3023
+               insn_form = IMM_OP;
3024
+       } else if (strcasecmp(opstr, "FPADD")==0) {
3025
+               insn->i_op = ZIPO_FPADD;
3026
+               insn_form = TWO_REGISTER;
3027
+       } else if (strcasecmp(opstr, "FPSUB")==0) {
3028
+               insn->i_op = ZIPO_FPSUB;
3029
+               insn_form = TWO_REGISTER;
3030
+       } else if (strcasecmp(opstr, "FPMUL")==0) {
3031
+               insn->i_op = ZIPO_FPMPY;
3032
+               insn_form = TWO_REGISTER;
3033
+       } else if (strcasecmp(opstr, "FPDIV")==0) {
3034
+               insn->i_op = ZIPO_FPDIV;
3035
+               insn_form = TWO_REGISTER;
3036
+       } else if (strcasecmp(opstr, "FPI2F")==0) {
3037
+               insn->i_op = ZIPO_FPI2F;
3038
+               insn_form = TWO_OP;
3039
+       } else if (strcasecmp(opstr, "FPF2I")==0) {
3040
+               insn->i_op = ZIPO_FPF2I;
3041
+               insn_form = TWO_REGISTER;
3042
+       } else if ((strcasecmp(opstr, "BRK")==0)
3043
+               ||(strcasecmp(opstr, "BREAK")==0)) {
3044
+               insn->i_op = ZIPO_BREAK;
3045
+               insn->i_imm= 0;
3046
+               insn_form = MAYBE_ONE_IMM;
3047
+       } else if (strcasecmp(opstr, "LOCK")==0) {
3048
+               insn->i_op = ZIPO_LOCK;
3049
+               insn_form = NO_OP;
3050
+       // Now for some derived instructions
3051
+       } else if (strcasecmp(opstr, "TRAP")==0) {
3052
+               insn->i_op = ZIPO_TRAP;
3053
+               insn_form = MAYBE_OPB;
3054
+       } else if (strcasecmp(opstr, "CLR")==0) {
3055
+               insn->i_op = ZIPO_CLR;
3056
+               insn_form = ONE_REGISTER;
3057
+       } else if (strcasecmp(opstr, "BRA")==0) {
3058
+               insn->i_op = ZIPO_BRA;
3059
+               insn_form = OP_ADDRESS;
3060
+       } else if (strcasecmp(opstr, "LJMP")==0) {
3061
+               insn->i_op = ZIPO_LJMP;
3062
+               insn->i_breg = ZIP_RNONE;
3063
+               insn->i_imm  = 0;
3064
+               insn_form = OP_ADDRESS;
3065
+       } else if (strcasecmp(opstr, "BZ")==0) {
3066
+               insn->i_op = ZIPO_BRA;
3067
+               insn->i_cnd = ZIPC_Z;
3068
+               insn_form = OP_ADDRESS;
3069
+       } else if (strcasecmp(opstr, "BLT")==0) {
3070
+               insn->i_op = ZIPO_BRA;
3071
+               insn->i_cnd = ZIPC_LT;
3072
+               insn_form = OP_ADDRESS;
3073
+       } else if (strcasecmp(opstr, "BC")==0) {
3074
+               insn->i_op = ZIPO_BRA;
3075
+               insn->i_cnd = ZIPC_C;
3076
+               insn_form = OP_ADDRESS;
3077
+       } else if (strcasecmp(opstr, "BV")==0) {
3078
+               insn->i_op = ZIPO_BRA;
3079
+               insn->i_cnd = ZIPC_V;
3080
+               insn_form = OP_ADDRESS;
3081
+       } else if (strcasecmp(opstr, "BNZ")==0) {
3082
+               insn->i_op = ZIPO_BRA;
3083
+               insn->i_cnd = ZIPC_NZ;
3084
+               insn_form = OP_ADDRESS;
3085
+       } else if (strcasecmp(opstr, "BGE")==0) {
3086
+               insn->i_op = ZIPO_BRA;
3087
+               insn->i_cnd = ZIPC_GE;
3088
+               insn_form = OP_ADDRESS;
3089
+       } else if (strcasecmp(opstr, "BNC")==0) {
3090
+               insn->i_op = ZIPO_BRA;
3091
+               insn->i_cnd = ZIPC_NC;
3092
+               insn_form = OP_ADDRESS;
3093
+       } else if (strcasecmp(opstr, "HALT")==0) {
3094
+               insn->i_op = ZIPO_HALT;
3095
+               insn->i_imm = ZIP_CC_SLEEP;
3096
+               insn->i_areg= ZIP_CC;
3097
+               insn_form = NO_OP;
3098
+       } else if (strcasecmp(opstr, "WAIT")==0) {
3099
+               insn->i_op = ZIPO_WAIT;
3100
+               insn->i_imm = ZIP_CC_SLEEP | ZIP_CC_GIE;
3101
+               insn->i_areg= ZIP_CC;
3102
+               insn_form = NO_OP;
3103
+       } else if (strcasecmp(opstr, "STEP")==0) {
3104
+               insn->i_op = ZIPO_STEP;
3105
+               insn->i_imm = ZIP_CC_STEP | ZIP_CC_GIE;
3106
+               insn->i_areg= ZIP_CC;
3107
+               insn_form = NO_OP;
3108
+       } else if ((strcasecmp(opstr, "RTU")==0)
3109
+                       ||(strcasecmp(opstr, "IRET")==0)) {
3110
+               insn->i_op = ZIPO_RTU;
3111
+               insn->i_imm = ZIP_CC_GIE;
3112
+               insn->i_areg = ZIP_CC;
3113
+               insn_form = NO_OP;
3114
+       } else if (strcasecmp(opstr, "BUSY")==0) {
3115
+               insn->i_op = ZIPO_BUSY;
3116
+               insn->i_imm= -4;
3117
+               insn->i_areg = ZIP_PC;
3118
+               insn->i_breg = ZIP_RNONE;
3119
+               insn_form = NO_OP;
3120
+       } else if (strcasecmp(opstr, "JMP")==0) {
3121
+               insn->i_op = ZIPO_JMP;
3122
+               insn->i_areg = ZIP_PC;
3123
+               insn_form = OP_B;
3124
+       } else if ((strcasecmp(opstr, "RETN")==0)
3125
+                       ||(strcasecmp(opstr, "RTN")==0)) {
3126
+               insn->i_op = ZIPO_JMP;
3127
+               insn->i_areg = ZIP_PC;
3128
+               insn->i_breg = ZIP_LR;
3129
+               insn->i_imm  = 0;
3130
+               insn_form = NO_OP;
3131
+       } else if (strcasecmp(opstr, "NOT")==0) {
3132
+               insn->i_op = ZIPO_NOT;
3133
+               insn->i_imm = -1;
3134
+               insn_form = ONE_REGISTER;
3135
+       } else if (strcasecmp(opstr, "NEG")==0) {
3136
+               insn->i_op = ZIPO_NEG;
3137
+               insn->i_imm = 0;
3138
+               insn_form = ONE_OR_TWO_OP;
3139
+       } else if (strcasecmp(opstr, "LJSR")==0) {
3140
+               insn->i_op   = ZIPO_LJSR;
3141
+               insn->i_breg = ZIP_RNONE;
3142
+               insn->i_imm  = 0;
3143
+               insn_form    = OP_ADDRESS;
3144
+       } else if (strcasecmp(opstr, "JSR")==0) {
3145
+               insn->i_op = ZIPO_JSR;
3146
+               insn->i_imm = 0;
3147
+               insn_form = OP_ADDRESS;
3148
+       } else if ((strcasecmp(opstr, "NOP")==0)
3149
+                       ||(strcasecmp(opstr, "NOOP")==0)) {
3150
+               insn->i_op = ZIPO_NOOP;
3151
+               insn_form = MAYBE_ONE_IMM;
3152
+       } else if (strcasecmp(opstr, "SIM")==0) {
3153
+               insn->i_op = ZIPO_SIM;
3154
+               insn->i_imm= 0;
3155
+               insn_form = MAYBE_ONE_IMM;
3156
+       } else if (strcasecmp(opstr, "SNOOP")==0) {
3157
+               insn->i_op = ZIPO_SIM;
3158
+               insn->i_imm= 0;
3159
+               insn_form = MAYBE_ONE_IMM;
3160
+       } else if (strcasecmp(opstr, "SDUMP")==0) {
3161
+               insn->i_op = ZIPO_SDUMP;
3162
+               insn->i_imm= 0;
3163
+               insn_form = MAYBE_ONE_REG;
3164
+       } else if (strcasecmp(opstr, "SEXIT")==0) {
3165
+               insn->i_op = ZIPO_SEXIT;
3166
+               insn->i_imm= 0;
3167
+               insn_form = MAYBE_ONE_IMM_OR_REG;
3168
+       } else if (strcasecmp(opstr, "SOUT")==0) {
3169
+               insn->i_op = ZIPO_SOUT;
3170
+               insn->i_imm= 0;
3171
+               insn_form = MAYBE_ONE_IMM_OR_REG;
3172
+       } else if (strcasecmp(opstr, "NDUMP")==0) {
3173
+               insn->i_op = ZIPO_NDUMP;
3174
+               insn->i_imm= 0;
3175
+               insn_form = MAYBE_ONE_REG;
3176
+       } else if (strcasecmp(opstr, "NEXIT")==0) {
3177
+               insn->i_op = ZIPO_NEXIT;
3178
+               insn->i_imm= 0;
3179
+               insn_form = MAYBE_ONE_IMM_OR_REG;
3180
+       } else if (strcasecmp(opstr, "SEXTB")==0) {
3181
+               insn->i_op = ZIPO_SEXTB;
3182
+               insn->i_imm= 0;
3183
+               insn_form = ONE_REGISTER;
3184
+       } else if (strcasecmp(opstr, "SEXTH")==0) {
3185
+               insn->i_op = ZIPO_SEXTH;
3186
+               insn->i_imm= 0;
3187
+               insn_form = ONE_REGISTER;
3188
+       } else if (strcasecmp(opstr, "NOUT")==0) {
3189
+               insn->i_op = ZIPO_NOUT;
3190
+               insn->i_imm= 0;
3191
+               insn_form = MAYBE_ONE_IMM_OR_REG;
3192
+       } else {
3193
+               free(alt);
3194
+               return "Unrecognized op-code";
3195
+       }
3196
+
3197
+       if (cndp) {
3198
+               // We have a condition
3199
+               if (insn->i_cnd != ZIPC_ALWAYS) {
3200
+                       err = "Instruction cannot take an additional condition";
3201
+               } else {
3202
+                       err = zip_parse_condition(cndp, &insn->i_cnd);
3203
+               }
3204
+       } if (err) {
3205
+               free(alt);
3206
+               return err;
3207
+       }
3208
+
3209
+       if (strchr(line, ',')!=NULL)
3210
+               left = strtok(NULL, ",");
3211
+       else
3212
+               left  = strtok(NULL, " \t,");
3213
+       right = (left)  ? strtok(NULL, " \t") : NULL;
3214
+       eol   = (right) ? strtok(NULL, " \t") : NULL;
3215
+       if (eol != NULL) {
3216
+               free(alt);
3217
+               return "Too many tokens on one line";
3218
+       }
3219
+
3220
+       switch(insn_form) {
3221
+               case TWO_OP:
3222
+                       err = zip_parse_bop(left,insn);
3223
+                       if (!err)
3224
+                               err = zip_parse_reg(right,&insn->i_areg);
3225
+                       break;
3226
+               case IMM_OP:
3227
+                       err = zip_parse_bop(left,insn);
3228
+                       if ((!err)&&(insn->i_breg == ZIP_RNONE))
3229
+                               err = zip_parse_reg(right,&insn->i_areg);
3230
+                       else if (!err)
3231
+                               err = "LDI can only load immediates, not registers";
3232
+                       break;
3233
+               case BACKWARDS_TWO:
3234
+                       err = zip_parse_reg(left,&insn->i_areg);
3235
+                       if (!err)
3236
+                               err = zip_parse_bop(right,insn);
3237
+                       break;
3238
+               case MAYBE_ONE_IMM:
3239
+                       if (NULL != right)
3240
+                               err = "Wrong number of operands!";
3241
+                       else if (left) {
3242
+                               err = zip_parse_bop(left,insn);
3243
+                               if ((!err)&&(insn->i_breg != ZIP_RNONE))
3244
+                                       err = "BREAK arguments can only be immediates";
3245
+                       }
3246
+                       break;
3247
+               case MAYBE_ONE_IMM_OR_REG:
3248
+                       if (NULL != right)
3249
+                               err = "Wrong number of operands!";
3250
+                       else if (left) {
3251
+                               err = zip_parse_bop(left,insn);
3252
+                               if ((!err)&&(insn->i_breg != ZIP_RNONE)
3253
+                                       &&(insn->i_imm != 0))
3254
+                                       err = "EXIT arguments can only be immediates or register, not both";
3255
+                       }
3256
+                       break;
3257
+               case MAYBE_ONE_REG:
3258
+                       if (NULL != right)
3259
+                               err = "Wrong number of operands!";
3260
+                       else if (left) {
3261
+                               err = zip_parse_bop(left,insn);
3262
+                               if ((!err)&&(insn->i_breg != ZIP_RNONE)&&(insn->i_imm))
3263
+                                       err = "xDUMP instructions can handle immediates or registers, not both";
3264
+                       } else {
3265
+                               insn->i_breg = ZIP_RNONE;
3266
+                               insn->i_imm  = 0;
3267
+                       } break;
3268
+               case NO_OP:
3269
+                       if ((NULL != left)||(NULL != right))
3270
+                               err = "Wrong number of operands!";
3271
+                       break;
3272
+               case ONE_OR_TWO_OP:
3273
+                       // This is a reference to the test instruction, which
3274
+                       // can either be
3275
+                       //      A. TEST a,Rx, or TEST Rx,.
3276
+                       // or   B. BRA  a,Rx, or BRA Rx,.
3277
+                       if (NULL != right) {
3278
+                               err = zip_parse_bop(left,insn);
3279
+                               if (!err)
3280
+                                       err = zip_parse_reg(right,&insn->i_areg);
3281
+                               if (!err)
3282
+                                       err = zip_parse_bop(left,insn);
3283
+                       } else {
3284
+                               zip_parse_reg(left, &insn->i_areg);
3285
+                               if (insn->i_op == ZIPO_BREV) {
3286
+                                       insn->i_breg = insn->i_areg;
3287
+                                       insn->i_imm = 0;
3288
+                               } else {
3289
+                                       insn->i_breg = ZIP_RNONE;
3290
+                                       insn->i_imm = -1;
3291
+                               }
3292
+                       } break;
3293
+               case ONE_REGISTER:
3294
+                       // CLR, CLRF, or NOT
3295
+                       insn->i_breg = ZIP_RNONE;
3296
+                       if (NULL != right)
3297
+                               err = "Instruction opcode expects only one operand";
3298
+                       else
3299
+                               err = zip_parse_reg(left,&insn->i_areg);
3300
+                       break;
3301
+               case OP_ADDRESS:
3302
+                       // JSR or BRA instruction
3303
+                       //      This is different from OP_B below, in that this
3304
+                       //      form implies a PC relative addressing, which
3305
+                       //      may not be clear from the operands given
3306
+                       if (NULL != right)
3307
+                               err = "Instruction opcode expects only one operand";
3308
+                       else {
3309 206 dgisselq
+                               if ((NULL == left)||(left[0] == '\0')) {
3310
+                                       err = "No address given";
3311
+                               } else if ((NULL == strchr(left,'('))
3312 202 dgisselq
+                                       &&(NULL == strchr(left,'+'))) {
3313
+                                       char    *longerstr = (char *)xmalloc(strlen(left)+6);
3314
+                                       // If not stated, assume PC relative
3315
+                                       strcpy(longerstr,left);
3316
+                                       strcat(longerstr, "(PC)");
3317
+                                       err = zip_parse_bop(longerstr, insn);
3318
+                                       free(longerstr);
3319
+                               } else
3320
+                                       err = zip_parse_bop(left, insn);
3321
+                       }
3322
+
3323
+                       break;
3324
+               case MAYBE_OPB:
3325
+                       // JMP or TRAP instruction
3326
+                       if (NULL != right)
3327
+                               err = "Instruction opcode expects only one operand";
3328
+                       else if (NULL == left) {
3329
+                               insn->i_breg = ZIP_RNONE;
3330
+                               insn->i_imm = 0;
3331
+                               break;
3332
+                       } // Fall through
3333
+               case OP_B: // Operand B only (A is implied)
3334
+                       // JMP or TRAP instruction
3335
+                       if (NULL != right)
3336
+                               err = "Instruction opcode expects only one operand";
3337
+                       else
3338
+                               err = zip_parse_bop(left, insn);
3339
+                       if (insn->i_op == ZIPO_JMP)
3340
+                               insn->i_areg = ZIP_PC;
3341
+                       else // if insn->i_op == TRAP
3342
+                               insn->i_areg = ZIP_CC;
3343
+                       break;
3344
+               case TWO_REGISTER:
3345
+                       // These are the floating point instructions, which
3346
+                       // can't handle immediate offsets.
3347
+                       err = zip_parse_reg(right, &insn->i_areg);
3348
+                       if (!err)
3349
+                               err = zip_parse_reg(left, &insn->i_breg);
3350
+                       break;
3351
+               default:        // case ILLEGAL_FORM
3352
+                       err = "Unknown instruction format!";
3353
+                       break;
3354
+       }
3355
+
3356
+       if (err)
3357
+               return (err);
3358
+
3359
+       switch(insn->i_op) {
3360
+               case ZIPO_SUB:  case ZIPO_AND: case ZIPO_ADD: case ZIPO_OR:
3361
+               case ZIPO_XOR:  case ZIPO_LSR: case ZIPO_LSL: case ZIPO_ASR:
3362
+               case ZIPO_BREV:
3363
+               case ZIPO_MPYUHI: case ZIPO_MPYSHI:
3364
+               case ZIPO_MPY:
3365
+                       break;
3366
+               case ZIPO_LDILO:
3367
+                       if (!fits_within(16, insn->i_imm))
3368
+                               gas_assert((insn->i_imm & (~0x0ffff))==0);
3369
+                       insn->i_imm &= 0x0ffff;
3370
+                       break;
3371
+               case ZIPO_DIVU: case ZIPO_DIVS:
3372
+                       if (0x0e == (insn->i_areg & 0x0f))
3373
+                               err = "Divide instructions cannot result in CC or PC regs";
3374
+                       else if (insn->i_rp)
3375
+                               err = "Relocations not applicable for divides";
3376
+                       break;
3377
+                       break;
3378
+               case ZIPO_MOV:
3379
+                       if (insn->i_breg == ZIP_RNONE)
3380
+                               insn->i_op = ZIPO_LDI;
3381
+                       break;
3382
+               case ZIPO_CMP: case ZIPO_TST:
3383
+                       break;
3384
+               case ZIPO_LW: case ZIPO_SW:
3385
+               case ZIPO_LH: case ZIPO_SH:
3386
+               case ZIPO_LB: case ZIPO_SB:
3387
+                       break;
3388
+               case ZIPO_LDI: case ZIPO_LDIn:
3389
+                       break;
3390
+               case ZIPO_FPADD: case ZIPO_FPSUB:
3391
+               case ZIPO_FPMPY: case ZIPO_FPDIV:
3392
+               case ZIPO_FPF2I: case ZIPO_FPI2F:
3393
+                       if (0x0e == (insn->i_areg & 0x0f))
3394
+                               err = "Floating point operations cannot result in CC or PC regs";
3395
+                       else if (insn->i_rp)
3396
+                               err = "Relocations not applicable for floating point ops";
3397
+                       break;
3398
+               case ZIPO_TRAP:
3399
+                       insn->i_areg = ZIP_CC;
3400
+                       if (insn->i_breg == ZIP_RNONE) {
3401
+                               if (insn->i_cnd == ZIPC_ALWAYS)
3402
+                                       insn->i_op = ZIPO_LDI;
3403
+                               else if (fits_within(16, insn->i_imm))
3404
+                                       insn->i_op = ZIPO_LDILO;
3405
+                               else if (fits_within(18, zip_brev(insn->i_imm)))
3406
+                                       insn->i_op = ZIPO_BREV;
3407
+                               else
3408
+                                       err = "TRAP immediate not supported";
3409
+                       } else
3410
+                               insn->i_op = ZIPO_MOV;
3411
+                       break;
3412
+               case ZIPO_CLR:
3413
+                       insn->i_op = ZIPO_LDI;
3414
+                       insn->i_imm = 0;
3415
+                       break;
3416
+               case ZIPO_JMP:
3417
+                       if (insn->i_breg != ZIP_RNONE)
3418
+                               insn->i_op = ZIPO_MOV;  // JMP A+Rx
3419
+                       else
3420
+                               insn->i_op = ZIPO_BRA;  // JMP lbl = BRA lbl
3421
+                       // Fall through
3422
+               case ZIPO_BRA: // Leave as ZIPO_BRA until we assemble it
3423
+                       insn->i_areg = ZIP_PC;
3424
+                       break;
3425
+               case ZIPO_HALT: case ZIPO_WAIT: case ZIPO_RTU: case ZIPO_STEP:
3426
+                       insn->i_op = ZIPO_OR;
3427
+                       break;
3428
+               case ZIPO_BUSY:
3429
+                       insn->i_op = ZIPO_ADD;
3430
+                       break;
3431
+               case ZIPO_NOT:
3432
+                       insn->i_op = ZIPO_XOR;
3433
+                       insn->i_imm  = -1;
3434
+                       insn->i_breg = ZIP_RNONE;
3435
+                       break;
3436
+               case ZIPO_NEG:
3437
+                       if (insn->i_breg == ZIP_RNONE) {
3438
+                               insn->i_breg = insn->i_areg;
3439
+                               // This would've been set to -1 by the ONE or
3440
+                               // TWO op code.  Here, we set it back to zero.
3441
+                               insn->i_imm  = 0;
3442
+                       }
3443
+                       // insn->i_op = ZIPO_NEG; /// Can't collapse this yet
3444
+                       break;
3445
+               case ZIPO_BREAK:
3446
+                       insn->i_op = ZIPO_BREAK;
3447
+                       insn->i_areg = ZIP_RNONE;
3448
+                       insn->i_breg = ZIP_RNONE;
3449
+                       // insn->i_imm  = ... whatever it was set to
3450
+                       break;
3451
+               case ZIPO_LOCK:
3452
+                       insn->i_op = ZIPO_LOCK;
3453
+                       insn->i_areg = ZIP_RNONE;
3454
+                       insn->i_breg = ZIP_RNONE;
3455
+                       insn->i_imm  = 0;
3456
+                       break;
3457
+               case ZIPO_SDUMP:
3458
+                       insn->i_op = ZIPO_SIM;
3459
+                       if (insn->i_breg != ZIP_RNONE) {
3460
+                               insn->i_imm = (int)insn->i_breg + 0x0200;
3461
+                               insn->i_breg = ZIP_RNONE;
3462
+                       } else
3463
+                               insn->i_imm = 0x2ff;
3464
+                       break;
3465
+               case ZIPO_NDUMP:
3466
+                       insn->i_op = ZIPO_NOOP;
3467
+                       if (insn->i_breg != ZIP_RNONE) {
3468
+                               insn->i_imm = (int)insn->i_breg + 0x0200;
3469
+                               insn->i_breg = ZIP_RNONE;
3470
+                       } else
3471
+                               insn->i_imm = 0x2ff;
3472
+                       break;
3473
+               case ZIPO_SEXIT:
3474
+                       insn->i_op = ZIPO_SIM;
3475
+                       if (insn->i_breg != ZIP_RNONE)
3476
+                               insn->i_imm = 0x300 + (insn->i_breg & 0x01f);
3477
+                       else
3478
+                               insn->i_imm = 0x100 + (insn->i_imm & 0x0ff);
3479
+                       insn->i_breg = ZIP_RNONE;
3480
+                       break;
3481
+               case ZIPO_NEXIT:
3482
+                       insn->i_op = ZIPO_NOOP;
3483
+                       if (insn->i_breg != ZIP_RNONE)
3484
+                               insn->i_imm = 0x300 + (insn->i_breg & 0x01f);
3485
+                       else
3486
+                               insn->i_imm = 0x100 + (insn->i_imm & 0x0ff);
3487
+                       insn->i_breg = ZIP_RNONE;
3488
+                       break;
3489
+               case ZIPO_SOUT:
3490
+                       insn->i_op = ZIPO_SIM;
3491
+                       if (insn->i_breg != ZIP_RNONE)
3492
+                               insn->i_imm = 0x220 + (insn->i_breg & 0x01f);
3493
+                       else
3494
+                               insn->i_imm = 0x400 + (insn->i_imm & 0x0ff);
3495
+                       insn->i_breg = ZIP_RNONE;
3496
+                       break;
3497
+               case ZIPO_NOUT:
3498
+                       insn->i_op = ZIPO_NOOP;
3499
+                       if (insn->i_breg != ZIP_RNONE)
3500
+                               insn->i_imm = 0x220 + (insn->i_breg & 0x01f);
3501
+                       else
3502
+                               insn->i_imm = 0x400 + (insn->i_imm & 0x0ff);
3503
+                       insn->i_breg = ZIP_RNONE;
3504
+                       break;
3505
+               case ZIPO_SIM:
3506
+               case ZIPO_NOOP:
3507
+                       break;
3508
+               case ZIPO_LJMP: case ZIPO_LJSR:
3509
+                       if (insn->i_breg == ZIP_PC)
3510
+                               insn->i_breg = ZIP_RNONE;
3511
+                       if (insn->i_breg != ZIP_RNONE) {
3512
+                               err = "Indirect long jumps and long JSRs are not supported";
3513
+                       } break;
3514
+               case ZIPO_JSR:
3515
+                       break;
3516
+               case ZIPO_SEXTB: case ZIPO_SEXTH:
3517
+                       break;
3518
+               default:
3519
+                       return "Internal error -- unrecognized internal opcode";
3520
+                       break;
3521
+       } if (err) {
3522
+               free(alt);
3523
+               return err;
3524
+       }
3525
+
3526
+       if ((insn->i_op != ZIPO_MOV)&&(
3527
+                       ((insn->i_areg != ZIP_RNONE)&&(insn->i_areg >= 0x10))
3528
+                       ||((insn->i_breg != ZIP_RNONE)&&(insn->i_breg >= 0x10)))) {
3529
+               if (insn->i_rp)
3530
+                       free(insn->i_rp);
3531
+               insn->i_rp = NULL;
3532
+               free(alt);
3533
+               return "Only MOV instructions can reference explicit user registers";
3534
+       }
3535
+
3536
+       free(alt);
3537
+
3538
+       // We do nothing more to build the opcode here --- all we are doing
3539
+       // is parsing.
3540
+       return err;
3541
+}
3542
+
3543
+#define        NOOPGROUP(OP,IMM) ((0x78000000)|((((OP)+0x1c-ZIPO_BREAK)&0x01f)<<22)|((IMM) & 0x003fffff))
3544
+#define        DBLREGOP(OP,CND,IMM,B,A) ((((OP)&0x01f)<<22)|(((A)&0x0f)<<27)   \
3545
+                       | (((CND)&0x07)<<19)|(1<<18)|(((B)&0x0f)<<14)   \
3546
+                       | ((IMM) & 0x03fff))
3547
+#define        SMPLMOV(CND,IMM,B,A)    (((ZIPO_MOV&0x01f)<<22)|(((A)&0x0f)<<27)        \
3548
+                       | (((CND)&0x07)<<19)|(((B)&0x0f)<<14)|((IMM) & 0x01fff))
3549
+#define        IMMOP(OP,CND,IMM,A)     ((((OP)&0x01f)<<22)|(((A)&0x0f)<<27)    \
3550
+                       | (((CND)&0x07)<<19)|((IMM) & 0x03ffff))
3551
+#define        LDIOP(IMM,A)    ((ZIPO_LDI<<22)|(((A)&0x0f)<<27)|((IMM) & 0x07fffff))
3552
+
3553
+
3554
+/*
3555
+ *     CIS support code
3556
+ *
3557
+ *     Assuming that the instruction (a) is not a CIS instruction, this looks
3558
+ *     up the immediate value encoded in the instruction.  The purpose is to
3559
+ *     determine whether or not this instruction can be merged with the
3560
+ *     previous (or next) instruction in CIS mode.
3561
+ *
3562
+ */
3563
+static int
3564
+zip_non_cis_immediate(const unsigned a)
3565
+{
3566
+       ZIP_OPCODE      op = (ZIP_OPCODE)((a>>22)&0x1f);
3567
+       int             imm;
3568
+
3569
+       switch(op) {
3570
+               case    ZIPO_MOV:
3571
+                       imm = (a & 0x01fff); if (a&0x1000) imm |= -0x1000; break;
3572
+               case    ZIPO_LDI:
3573
+                       imm = (a & 0x03fffff); break;
3574
+               case    ZIPO_LDIn:
3575
+                       imm = (a & 0x03fffff); imm |= -0x0200000; break;
3576
+/*
3577
+ * While this makes conceptual sense, it doesn't match the CPU.  Hence let's
3578
+ * comment it out and make certain things still work.
3579
+               case    ZIPO_LDILO:
3580
+#ifndef        LONG_MPY
3581
+               case ZIPO_LDIHI: // BREVx would rm LDIHI
3582
+#endif
3583
+                       imm = (a & 0x0ffff); break;
3584
+*/
3585
+               default:
3586
+                       if (a & 0x040000) {
3587
+                               imm = (a & 0x03fff);
3588
+                               if (a & 0x2000) imm |= -0x02000;
3589
+                       } else {
3590
+                               imm = (a & 0x03ffff);
3591
+                               if (a & 0x020000)
3592
+                                       imm |= -0x020000;
3593
+                       }
3594
+       }
3595
+
3596
+       return imm;
3597
+}
3598
+
3599
+static int
3600
+zip_can_merge(const unsigned a, const unsigned b)
3601
+{
3602
+       // Can't merge instructions if merging isnt turned on
3603
+       if (!zip_param_cis)
3604
+               return 0;
3605
+
3606
+       // 1. Can't merge anything that's already merged
3607
+       if ((a|b) & 0x80000000)
3608
+               return 0;
3609
+       ZIP_OPCODE      opa, opb;
3610
+       ZIP_CONDITION   ac, bc;
3611
+       int     imma, immb;
3612
+
3613
+       // Get the 5-bit operands for each
3614
+       opa = (a>>22)&0x1f;
3615
+       opb = (b>>22)&0x1f;
3616
+
3617
+       // 2. Can only merge if both are unconditional
3618
+       ac = ((a>>19)&0x07);
3619
+       bc = ((b>>19)&0x07);
3620
+       if ((opa != ZIPO_LDI)&&(opa != ZIPO_LDIn)&&(ac != ZIPC_ALWAYS))
3621
+               return 0;
3622
+       if ((opb != ZIPO_LDI)&&(opb != ZIPO_LDIn)&&(bc != ZIPC_ALWAYS))
3623
+               return 0;
3624
+
3625
+       // Only some instructions can be merged
3626
+       switch(opa) {
3627
+               case ZIPO_SUB: case ZIPO_AND: case ZIPO_ADD: case ZIPO_CMP:
3628
+               case ZIPO_LW:  case ZIPO_SW:  case ZIPO_LDI: case ZIPO_MOV:
3629
+               case ZIPO_LDIn:
3630
+                       break;
3631
+               default:
3632
+                       return 0;
3633
+       } switch(opb) {
3634
+               case ZIPO_SUB: case ZIPO_AND: case ZIPO_ADD: case ZIPO_CMP:
3635
+               case ZIPO_LW:  case ZIPO_SW:  case ZIPO_LDI: case ZIPO_MOV:
3636
+               case ZIPO_LDIn:
3637
+                       break;
3638
+               default:
3639
+                       return 0;
3640
+       }
3641
+
3642
+       // Prohibit moves to/from user regs to merge
3643
+       if ((opa == ZIPO_MOV)&&(a & 0x44000))
3644
+               return 0;
3645
+       if ((opb == ZIPO_MOV)&&(b & 0x44000))
3646
+               return 0;
3647 206 dgisselq
+
3648 202 dgisselq
+       imma = zip_non_cis_immediate(a);
3649
+       immb = zip_non_cis_immediate(b);
3650
+
3651
+       if (!fits_within(8,imma)) {
3652
+               // fprintf(stderr, "As immediate is out of range\n");
3653
+               return 0;
3654
+       } if (!fits_within(8,immb)) {
3655
+               // fprintf(stderr, "Bs immediate is out of range\n");
3656
+               return 0;
3657
+       }
3658
+
3659
+       // if abreg & 0x010, or bbreg & 0x010, then the register is being
3660
+       // used.
3661
+       int aareg = (a>>27)&0x00f;
3662
+       int abreg = (a>>14)&0x01f;
3663
+       int bbreg = (b>>14)&0x01f;
3664
+
3665
+       switch(opa) {
3666
+               case ZIPO_MOV:
3667
+                       if (!fits_within(3,imma))
3668
+                               return 0;
3669 206 dgisselq
+                       // Prohibit a MOV x(PC),Ry to move into a first
3670
+                       // instruction position.  While the CPU supports it,
3671
+                       // making sure x remains valid isn't yet supported
3672
+                       // here.
3673
+                       if ((ZIP_PC == aareg)||(ZIP_CC == aareg))
3674 202 dgisselq
+                               return 0;
3675
+                       break;
3676
+               case ZIPO_LDI: case ZIPO_LDIn:
3677
+                       if (!fits_within(8,imma))
3678
+                               return 0;
3679
+                       if (aareg == ZIP_CC)
3680
+                               return 0;
3681
+                       break;
3682
+               case ZIPO_ADD: case ZIPO_SUB: case ZIPO_AND:
3683
+                       if (aareg == ZIP_CC)
3684
+                               return 0;
3685
+               case ZIPO_CMP:
3686
+                       if ((abreg&0x10)==0) {
3687
+                               if (!fits_within(7,imma)) {
3688
+                                       return 0;
3689
+                               }
3690
+                       } else { // if (abreg&0x10)
3691
+                               if (!fits_within(3,imma))
3692
+                                       return 0;
3693
+                       } break;
3694
+               case ZIPO_LW: case ZIPO_SW:
3695
+                       if (aareg == ZIP_CC)
3696
+                               return 0;
3697
+                       if ((abreg&0x010)==0) {
3698
+                               return 0;
3699
+                       } else if (abreg== 0x10 + ZIP_SP) {
3700
+                               if (!fits_within(7,imma))
3701
+                                       return 0;
3702
+                       } else {
3703
+                               if (!fits_within(3,imma))
3704
+                                       return 0;
3705
+                       } break;
3706
+               default:
3707
+                       fprintf(stderr, "Unknown op, %d\n", opa);
3708
+                       return 0;
3709
+       }
3710
+
3711
+       switch(opb) {
3712
+               case ZIPO_MOV:
3713
+                       if (!fits_within(3,immb)) {
3714
+                               return 0;
3715
+                       } break;
3716
+               case ZIPO_LDI: case ZIPO_LDIn:
3717
+                       if (!fits_within(8,immb)) {
3718
+                               return 0;
3719
+                       } break;
3720
+               case ZIPO_ADD: case ZIPO_SUB: case ZIPO_CMP: case ZIPO_AND:
3721
+                       if ((bbreg&0x10)==0) {
3722
+                               if (!fits_within(7,immb))
3723
+                                       return 0;
3724
+                       } else { // if (bbreg&0x10)
3725
+                               if (!fits_within(3,immb))
3726
+                                       return 0;
3727
+                       } break;
3728
+               case ZIPO_LW: case ZIPO_SW:
3729
+                       if ((bbreg&0x010)==0) {
3730
+                               return 0;
3731
+                       } else if (bbreg== 0x10+ZIP_SP) {
3732
+                               if (!fits_within(7,immb))
3733
+                                       return 0;
3734
+                       } else {
3735
+                               if (!fits_within(3,immb))
3736
+                                       return 0;
3737
+                       } break;
3738
+               default:
3739
+                       return 0;
3740
+       }
3741
+
3742
+       return 1;
3743
+}
3744
+
3745
+static unsigned
3746
+zip_insn_merge(const unsigned a, const unsigned b)
3747
+{
3748
+       // 1. We already know we can merge these, so skip our can-merge checks
3749
+       ZIP_OPCODE      opa, opb;
3750
+       unsigned int    retv = 0;
3751
+
3752
+       // Get our two opcodes
3753
+       opa = (a>>22)&0x1f; opb = (b>>22)&0x1f;
3754
+
3755
+       // Truncate LDI opcodes back to their original values
3756
+       if (opa == 0x019) opa = 0x018;
3757
+       if (opb == 0x019) opb = 0x018;
3758
+       retv = 0x80008000;
3759
+
3760
+       // Start with the destination registers
3761
+       retv |= (a & 0x78000000);
3762
+       retv |= (b & 0x78000000) >> (27-11);
3763
+
3764
+       // Then the new OpCodes
3765
+       switch(opa) {
3766
+       case ZIPO_SUB:  retv |= (ZIPV_SUB<<24); break;
3767
+       case ZIPO_AND:  retv |= (ZIPV_AND<<24); break;
3768
+       case ZIPO_ADD:  retv |= (ZIPV_ADD<<24); break;
3769
+       case ZIPO_CMP:  retv |= (ZIPV_CMP<<24); break;
3770
+       case ZIPO_LW:   retv |= (ZIPV_LW <<24); break;
3771
+       case ZIPO_SW:   retv |= (ZIPV_SW <<24); break;
3772
+// 8e40
3773
+// 1.0001.110_0100_0000
3774
+       case ZIPO_LDI:  retv |= (ZIPV_LDI<<24); break;
3775
+       case ZIPO_MOV:  retv |= (ZIPV_MOV<<24); break;
3776
+       default: gas_assert(0);
3777
+       }
3778
+
3779
+       switch(opb) {
3780
+       case ZIPO_SUB:  retv |= (ZIPV_SUB<<8); break;
3781
+       case ZIPO_AND:  retv |= (ZIPV_AND<<8); break;
3782
+       case ZIPO_ADD:  retv |= (ZIPV_ADD<<8); break;
3783
+       case ZIPO_CMP:  retv |= (ZIPV_CMP<<8); break;
3784
+       case ZIPO_LW:   retv |= (ZIPV_LW <<8); break;
3785
+       case ZIPO_SW:   retv |= (ZIPV_SW <<8); break;
3786
+       case ZIPO_LDI:  retv |= (ZIPV_LDI<<8); break;
3787
+       case ZIPO_MOV:  retv |= (ZIPV_MOV<<8); break;
3788
+       default: gas_assert(0);
3789
+       }
3790
+
3791
+       // The new CIS has no condition codes
3792
+
3793
+       // Now for OpB, instruction A
3794
+       if (opa == ZIPO_LDI)
3795
+               retv |= (a & 0xff)<<16;
3796
+       else if (opa == ZIPO_MOV) {
3797
+               retv |= 1<<23;                  // Using a register? always4mov
3798
+               retv |= ((a>>14)&0x0f)<<19;     // OpB register
3799
+               retv |= (a & 0x7)<<16;          // Offset
3800
+       } else if(((opa == ZIPO_LW)||(opa == ZIPO_SW))
3801
+                       &&(((a>>14)&0x1f)==0x10 + ZIP_SP)) {
3802
+               // 7-bit immediate for loads or stores referencing the stack
3803
+               retv |= (a & 0x07f)<<16;
3804
+       } else if ((a>>18)&1) {
3805
+               // OpB ... with a register
3806
+               retv |= 1<<23;                  // Using a register?
3807
+               retv |= ((a>>14)&0x0f)<<19;     // OpB register
3808
+               retv |= (a&0x07)<<16;           // OpB constant
3809
+       } else {
3810
+               // OpB ... with constant offsets only
3811
+               retv |= (a&0x07f)<<16;
3812
+       }
3813
+
3814
+
3815
+       // Now for OpB, instruction B
3816
+       if (opb == ZIPO_LDI)
3817
+               retv |= (b & 0xff);
3818
+       else if (opb == ZIPO_MOV) {
3819
+               retv |= (1<<7);                 // Using a register? always4mov
3820
+               retv |= ((b>>14)&0x0f)<<3;      // OpB register
3821
+               retv |= (b & 0x7);              // Offset
3822
+       } else if(((opb == ZIPO_LW)||(opb == ZIPO_SW))
3823
+                       &&(((b>>14)&0x1f)==0x10 + ZIP_SP)) {
3824
+               retv |= (b & 0x07f);
3825
+       } else if ((b>>18)&1) {
3826
+               // OpB ... with a register
3827
+               retv |= 1<<7;                   // Using a register?
3828
+               retv |= ((b>>14)&0x0f)<<3;      // OpB register
3829
+               retv |= (b&0x07);               // OpB constant
3830
+       } else {
3831
+               // OpB ... with constant offsets only
3832
+               retv |= (b&0x07f);
3833
+       }
3834
+
3835
+       return retv;
3836
+}
3837
+
3838
+void
3839
+zip_check_label(symbolS *label ATTRIBUTE_UNUSED)
3840
+{
3841
+       // On any symbol, we need to make sure that we can jump to this
3842
+       // address, therefore we cannot merge the previous instruction with
3843
+       // another one that might follow.
3844
+       cis_mergable = FALSE;
3845
+       //
3846
+       // Likewise, our shadow virtual machine values may be ... unknown
3847
+       // upon any jump to this location.  Hence, we declare them unknown
3848
+       // here.
3849
+       zip_clear_machine(&zipm);
3850
+}
3851
+
3852
+// UNITS:
3853
+//     Offset  (target byte units)
3854
+//     Address (host byte units)
3855
+//     Sym     (target byte units)
3856
+//     Stretch (target byte units)
3857
+//
3858
+static void
3859
+zip_assemble_insn_words(fragS *fragP, segT seg, ZIPIS *insn, int relax_state,
3860
+               long stretch, MACHINEREGS *pzipm)
3861
+{
3862
+#ifdef ZIP_DEBUG
3863
+fprintf(stderr, "ZIP-ASSEMBLE-INSN-WORDS\n");
3864
+       zip_dump_insn(insn);
3865
+       if ((insn->i_rp)||(relax_state))
3866
+               zip_dump_sym(insn->i_rp->r_sym);
3867
+#endif
3868
+       unsigned long   symv = 0;
3869
+       int     sym_defined = 0, this_segment = 0, sym_known = 0;
3870
+       symbolS *sym = NULL;
3871
+       unsigned        immv = insn->i_imm;
3872
+
3873
+       if (insn->i_rp) {
3874
+               fragS   *sym_frag;
3875
+
3876
+               sym = insn->i_rp->r_sym;
3877
+               symv = ((sym)&&(S_IS_DEFINED(sym))) ? (S_GET_VALUE(sym)) : 0;
3878
+               sym_frag = symbol_get_frag(sym);
3879
+               this_segment = (S_GET_SEGMENT(sym) == seg);
3880
+
3881
+#ifdef ZIP_DEBUG
3882
+               fprintf(stderr, "SYMV(%s) = %08lx + %08lx -> %08lx\n",
3883
+                       S_GET_NAME(sym),
3884
+                       S_GET_VALUE(sym), fragP->fr_offset,
3885
+                       S_GET_VALUE(sym) + fragP->fr_offset);
3886
+#endif
3887
+               symv += fragP->fr_offset;
3888
+               // The immediate value has been included in the fragP->fr_offset
3889
+               // So, to keep us from applying it twice, we'll zero it here.
3890
+               immv = 0;
3891
+
3892
+#ifdef ZIP_DEBUG
3893
+               { int this_frag = (sym_frag == fragP);
3894
+               fprintf(stderr, "Determined symbol is %sin this frag, and %sin this segment\n", (this_frag)?"":"not ", (this_segment)?"":"not ");
3895
+               }
3896
+#endif
3897
+
3898
+               if ((stretch != 0)
3899
+                       &&( sym_frag->relax_marker != fragP->relax_marker)
3900
+                       &&(this_segment)) {
3901
+                       if ((stretch < 0)
3902
+                               ||(sym_frag->region == fragP->region))
3903
+                               symv += stretch;
3904
+                       else if (symv < fragP->fr_address)
3905
+                               symv = fragP->fr_next->fr_address;
3906
+               }
3907
+       }
3908
+
3909
+       // At this point, if the symbol is "defined" that only means that
3910
+       // the address is known--not that we can know it via GET_VALUE above
3911
+       // here.  There are a couple of possibilities: it could be a known
3912
+       // offset into a fragment, it could be a known offset into a segment,
3913
+       // it could be a known memory address, or it could be a known value.
3914
+       //
3915
+       // If the value is an offset into our current fragment, then we can
3916
+       // get at it via PC relative addressing.
3917
+       //
3918
+       // If the symbol is an offset into our current segment, often the
3919
+       // .text segment, we may also be able to get to it via PC relative
3920
+       // addressing.  However, we won't know until the fragment's are placed
3921
+       // together to create this segment.  This will be before writing
3922
+       // the file to disk.
3923
+       //
3924
+       // If the symbol is in the absolute segment, then we should try to
3925
+       // get to it via an absolute 32-bit offset reference--since we don't
3926
+       // know (and won't know until the final link) where we are currently
3927
+       // located within memory.
3928
+       //
3929
+       // The same is true of the undefined segment--we just don't know any
3930
+       // PC offsets to that segment.
3931
+       //
3932
+       // But if the address is to a GOT segment, we should be able to assume
3933
+       // it's offset from the beginning of that segment.
3934
+       //
3935
+       //
3936
+       if ((relax_state)&&(insn->i_rp))
3937
+               sym_defined = S_IS_DEFINED(insn->i_rp->r_sym);
3938
+
3939
+       /*
3940
+       sym_known = (sym_defined)&&
3941
+                       ((S_GET_SEGMENT(sym) == absolute_section)
3942
+                       ||(S_GET_SEGMENT(sym) == expr_section)
3943
+                       ||(this_frag)
3944
+                       ||((this_segment)&&(fragP->fr_address != 0))
3945
+                       );
3946
+       */
3947
+       sym_known = (sym_defined)&&(insn->i_rp->r_pcrel)&&(this_segment);
3948
+       if ((!sym_known)&&(sym_defined)&&(!S_FORCE_RELOC(sym, 0))) {
3949
+               if (this_segment)
3950
+                       sym_known = 1;
3951
+               else if (S_GET_SEGMENT(sym)==absolute_section)
3952
+                       sym_known = 1;
3953
+       }
3954
+#ifdef ZIP_DEBUG
3955
+       if (sym_known)
3956
+               fprintf(stderr, "%08lx@%08lx/%08lx - SYM(%s)-KNOWN %s%s%s%s%s\n",
3957
+                       (unsigned long)fragP->fr_literal,
3958
+                       fragP->fr_address,
3959
+                       (unsigned long)symv, S_GET_NAME(sym),
3960
+                       (S_GET_SEGMENT(sym) == absolute_section)?" abs":"",
3961
+                       (S_GET_SEGMENT(sym) == expr_section)?" expr":"",
3962
+                       (symbol_get_frag(sym)==fragP)?" this-frag ":"",
3963
+                       ((this_segment)&&(fragP->fr_address != 0))?" this-seg ":"",
3964
+                       insn->i_rp->r_pcrel?" (PC-REL)":" (ABS)");
3965
+       else if (insn->i_rp)
3966
+               fprintf(stderr, "%08lx@%08lx/%08lx - SYM(%s) -- not known (yet) %s%s%s%s%s%s\n",
3967
+                       (unsigned long)fragP->fr_literal, fragP->fr_address,
3968
+                       (unsigned long)symv, S_GET_NAME(sym),
3969
+                       (!sym_defined)?"-- not defined":"",
3970
+                       (S_GET_SEGMENT(sym) == absolute_section)?" abs":"",
3971
+                       (S_GET_SEGMENT(sym) == expr_section)?" expr":"",
3972
+                       (symbol_get_frag(sym)==fragP)?" this-frag ":"",
3973
+                       (this_segment)?" this-seg ":"",
3974
+                       ((this_segment)&&(fragP->fr_address != 0))?" this-off ":""
3975
+                       );
3976
+
3977
+       fprintf(stderr, "SYM-DEF %d,%d,%d,%d, IMM = %08x\n",
3978
+               sym_defined, sym_known,
3979
+               (insn->i_rp)&&(symbol_get_frag(insn->i_rp->r_sym)==fragP)?1:0,
3980
+               this_segment, insn->i_imm);
3981
+#endif
3982
+       switch(insn->i_op) {
3983
+       case ZIPO_LDI: // May or may not be conditional
3984
+               if ((sym_known)&&(this_segment)
3985 206 dgisselq
+                       &&(fits_within(15,immv+symv-fragP->fr_address-insn->i_rp->r_fr_offset-sizeof(uint32_t)))) {
3986 202 dgisselq
+                       // Turn this into a MOV x(PC),Rx
3987 206 dgisselq
+                       //
3988
+                       // Although moves can normally only handle 13 bits,
3989
+                       // when the immediate is an offset to the PC, the bottom
3990
+                       // two bits are always zero, and so the immediate
3991
+                       // gets an extra two free bits--hence the test for
3992
+                       // fitting into 15 bits above.
3993 202 dgisselq
+                       insn->i_breg = ZIP_PC;
3994
+                       insn->i_op = ZIPO_MOV;
3995
+                       insn->i_naux = 0;
3996
+                       zip_assemble_insn_words(fragP, seg, insn, relax_state,
3997
+                               stretch, pzipm);
3998
+                       // Tested--this works
3999
+                       return;
4000
+               }
4001
+
4002
+               // 0.111.x111.11
4003
+               insn->i_aux[0] = 0x7fc00000; // NOOP -- if never used.
4004
+               immv += symv;
4005 206 dgisselq
+               if ((!insn->i_rp)&&(insn->i_cnd == ZIPC_ALWAYS)
4006 202 dgisselq
+                               &&(fits_within(23, immv))) {
4007
+                       insn->i_naux = 0;
4008
+                       insn->i_code = LDIOP(immv,insn->i_areg);
4009
+               } else if (((!insn->i_rp)||(sym_known))
4010
+                               &&(fits_within(18, zip_brev(immv)))) {
4011
+                       // Can we do this with a BREV instruction using an
4012
+                       // immediate?  If so, we can then conditionally load
4013
+                       // the top 18 bist of any value ...
4014
+                       //
4015
+                       insn->i_naux = 0;
4016
+                       insn->i_code = IMMOP(ZIPO_BREV, insn->i_cnd,
4017
+                                       zip_brev(immv),
4018
+                                       insn->i_areg&0x0f);
4019
+                       if (insn->i_rp)
4020
+                               insn->i_rp->r_type = BFD_RELOC_NONE;
4021
+// 0000 1110 0000 0000 0000 0000 0100 0000
4022
+               } else if ((zip_param_use_machine)&&(pzipm)&&((!insn->i_rp)||(sym_known))
4023
+                       &&(pzipm->r[insn->i_areg].m_known)
4024
+                       &&(0==((immv^pzipm->r[insn->i_areg].m_value)
4025
+                                       & 0x0ffff0000))) {
4026
+                       // Replace LDI with LDILO
4027
+                       insn->i_naux = 0;
4028
+                       insn->i_code=IMMOP(ZIPO_LDILO, insn->i_cnd,
4029
+                                       (immv&0x0ffff), insn->i_areg);
4030
+
4031
+                       pzipm->r[insn->i_areg].m_value = immv;
4032
+                       if (ZIPC_ALWAYS == insn->i_cnd) {
4033
+                               // Henceforth, we only know the bottom 16bits
4034
+                               // of this register
4035
+                               pzipm->r[insn->i_areg].m_known = MACH_VUPPERKNOWN;
4036
+                       } else
4037
+                               pzipm->r[insn->i_areg].m_known = MACH_VKNOWN;
4038
+               } else {
4039
+                       //
4040
+                       // If the symbol isn't defined, then any immv value
4041
+                       // will work--we have to come back anyway.
4042
+                       //
4043
+                       int known_bypass = 0, i;
4044
+
4045
+                       if ((zip_param_use_machine)&&(pzipm)&&(
4046
+                                       ((!insn->i_rp)
4047
+                                               &&(!fits_within(4,immv)))
4048
+                                       ||((insn->i_rp)&&(sym_known)))) {
4049
+                               for(i=0; i<14; i++) {
4050
+                                       int offset = immv-pzipm->r[i].m_value;
4051
+                                       if ((pzipm->r[i].m_known==MACH_VKNOWN)
4052
+                                               &&(fits_within(13, offset))
4053
+                                               &&((insn->i_rp)
4054
+                                                       ||(!fits_within(4, immv)))
4055
+                                               ) {
4056
+                                               // Pick the closest value ... if
4057
+                                               // there's a choice
4058
+                                               if ((!known_bypass)
4059
+                                                       ||(abs(offset)<known_bypass))
4060
+                                                       continue;
4061
+                                               insn->i_naux = 0;
4062
+                                               insn->i_op = ZIPO_MOV;
4063
+                                               insn->i_breg = i;
4064
+                                               insn->i_imm = offset;
4065
+
4066
+                                               insn->i_code = SMPLMOV(
4067
+                                                       insn->i_cnd, offset,
4068
+                                                       i, insn->i_areg);
4069
+                                               known_bypass = abs(offset);
4070
+                                               if (known_bypass==0)
4071
+                                                       known_bypass = 1;
4072
+                                       }
4073
+                               } known_bypass = (known_bypass)?1:0;
4074
+                       } if (!known_bypass) {
4075
+                               // BREV Extension would modify this statement
4076
+                               insn->i_naux = 1;
4077
+                               insn->i_code = IMMOP(ZIPO_BREV, insn->i_cnd,
4078
+                                       zip_brev(immv)&0x01ffff, insn->i_areg);
4079
+                               insn->i_aux[0]=IMMOP(ZIPO_LDILO, insn->i_cnd,
4080
+                                       (immv&0x0ffff), insn->i_areg);
4081
+                               if (insn->i_rp)
4082
+                                       insn->i_rp->r_type = BFD_RELOC_ZIP_LDI;
4083
+                               else if ((zip_param_cis)&&(zip_can_merge(insn->i_code, insn->i_aux[0]))) {
4084
+                                       insn->i_code = zip_insn_merge(insn->i_code, insn->i_aux[0]);
4085
+                                       insn->i_naux = 0;
4086
+                               }
4087
+                       } else {
4088
+fprintf(stderr, "known-bypass\n");
4089
+                       }
4090
+               }
4091
+#ifdef ZIP_DEBUG
4092
+               fprintf(stderr, "LDI %04x:%04x,%d Instruction assembled into %08x : %08x\n",
4093
+                       (immv>>16)&0x0ffff,
4094
+                       immv & 0x0ffff, insn->i_areg,
4095
+                       insn->i_code, insn->i_aux[0]);
4096
+#endif
4097
+               break;
4098
+       case ZIPO_BRA: // Includes both conditional and unconditional branches
4099
+#ifdef ZIP_DEBUG
4100
+               fprintf(stderr, "ZIPO--BRA (ADDR = %08lx, OFFSET = %08lx)\n",
4101
+                               fragP->fr_address,
4102
+                               (insn->i_rp)?insn->i_rp->r_fr_offset:0);
4103
+#endif
4104
+               if (insn->i_rp)
4105
+                       immv += (int)(symv
4106
+                               - fragP->fr_address
4107
+                               - (insn->i_rp->r_fr_offset+sizeof(uint32_t)));
4108
+
4109
+#ifdef ZIP_DEBUG
4110
+               if (!fits_within(18,immv))
4111
+                       fprintf(stderr, "doesn't fit in 18 bits\n");
4112
+#endif
4113
+               if (zip_param_small) {
4114
+                       //
4115
+                       // We can fit (or so we are told), so short jump code
4116
+                       // to an unknown location ...
4117
+                       //
4118
+                       insn->i_naux = 0;
4119
+                       insn->i_aux[0] = NOOP_OPCODE;
4120
+                       insn->i_aux[1] = NOOP_OPCODE;
4121
+                       insn->i_code = IMMOP(ZIPO_ADD, insn->i_cnd,
4122
+                               immv, ZIP_PC); // Add value to PC
4123
+                       insn->i_rp->r_pcrel = TRUE;
4124
+                       // No relocation is required--we just did it
4125
+                       insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_PCREL;
4126
+                       // Tested--this works
4127
+               } else if ((!sym_known)||(!fits_within(18,immv))) {
4128
+                       insn->i_op = ZIPO_LJMP;
4129
+
4130
+                       zip_assemble_insn_words(fragP, seg, insn, relax_state, stretch, pzipm);
4131
+                       insn->i_op = ZIPO_BRA;
4132
+               } else {
4133
+                       //
4134
+                       // We can fit, so short jump code ...
4135
+                       //
4136
+                       insn->i_naux = 0;
4137
+                       insn->i_aux[0] = NOOP_OPCODE;
4138
+                       insn->i_aux[1] = NOOP_OPCODE;
4139
+                       insn->i_code = IMMOP(ZIPO_ADD, insn->i_cnd,
4140
+                               immv, ZIP_PC); // Add value to PC
4141
+                       insn->i_rp->r_pcrel = TRUE;
4142
+                       // No relocation is required--we just did it
4143
+                       insn->i_rp->r_type = BFD_RELOC_NONE;
4144
+               }
4145
+#ifdef ZIP_DEBUG
4146
+               fprintf(stderr, "BRA %08lx->%08x(%d,%d) assembled into(%d) %08x : %08x : %08x\n",
4147
+                       symv, immv, sym_defined, relax_state,
4148
+                       insn->i_naux, insn->i_code,
4149
+                       insn->i_aux[0], insn->i_aux[1]);
4150
+#endif
4151
+               break;
4152
+       case    ZIPO_LJMP:
4153
+               //
4154
+               // Long jump code
4155
+               //
4156
+               // I expect this to be true, since we should always
4157
+               // be branching (or jumping) to a location defined by a
4158
+               // symbol.
4159
+               if (insn->i_rp) {
4160
+                       insn->i_rp->r_pcrel = FALSE;
4161
+                       insn->i_rp->r_type = BFD_RELOC_ZIP_VALUE;
4162
+               } insn->i_breg = ZIP_RNONE;
4163
+#ifdef ZIP_DEBUG
4164
+               fprintf(stderr, "LJMP IMM = %08x = %d\n", immv, immv);
4165
+#endif
4166
+               if (insn->i_cnd == ZIPC_ALWAYS) {
4167
+                       insn->i_naux = 1;
4168
+                       insn->i_code=DBLREGOP(ZIPO_LW,ZIPC_ALWAYS,
4169
+                               0,ZIP_PC,ZIP_PC); // Load into PC register
4170
+                       insn->i_aux[0] = 0; // Value to load
4171
+               } else {
4172
+                       // A conditional long jump
4173
+                       insn->i_naux = 2;
4174
+                       insn->i_code=DBLREGOP(ZIPO_LW,insn->i_cnd,
4175
+                               1,ZIP_PC,ZIP_PC); // Conditional load to PC
4176
+                       insn->i_aux[0]=IMMOP(ZIPO_ADD,ZIPC_ALWAYS,
4177
+                               1,ZIP_PC); // Skip an instruction
4178
+                       insn->i_aux[1] = 0;
4179
+               } if (!insn->i_rp)
4180
+                       insn->i_aux[insn->i_naux-1] = immv;
4181
+               break;
4182
+       case ZIPO_MOV:
4183
+               //
4184
+               // We could go a little wild here:
4185
+               //      MOV val,Rx
4186
+               // Could be quietly compiled into
4187
+               //      LDI val,Rx      (if Rx is not a user register)
4188
+               // whereas
4189
+               //      MOV BIGVAL(Ry),Rx
4190
+               // could be quietly compiled into
4191
+               //      LDI BIGVAL,Rx
4192
+               //      ADD.Z Ry,Rx     (Note the contortions to avoid adjusting
4193
+               //      ADD.NZ Ry,Rx    ... the flag bits)
4194
+               // This would give us access to full 32-bit offsets ...
4195
+               // as long as neither Rx nor Ry was a user register.  On the
4196
+               // other hand, if we knew the flag bits would be set between
4197
+               // now and the next conditional instruction, we could do a
4198
+               //      LDI BIGVAL,Rx
4199
+               //      ADD Ry,Rx
4200
+               // and be okay with it.
4201
+               //
4202
+               // For now we just handle our normal 13 bit signed immediate
4203
+               //      offsets.
4204
+               //
4205
+               if (insn->i_rp) {
4206
+                       // MOV label(Rb),Ra
4207
+                       insn->i_rp->r_pcrel = (insn->i_breg == ZIP_PC);
4208
+                       immv += symv;
4209
+                       if (insn->i_breg == ZIP_PC) {
4210
+                               immv +=(int)(-fragP->fr_address
4211
+                                               -insn->i_rp->r_fr_offset-sizeof(uint32_t));
4212
+                               immv >>= 2;
4213
+                               if ((sym_known)&&(this_segment)) {
4214
+                                       // Tested--this works
4215
+                                       insn->i_rp->r_type = BFD_RELOC_NONE;
4216
+                               } else {
4217
+#ifdef ZIP_DEBUG
4218
+                                       fprintf(stderr, "ZIP_MOV_PCREL relocation needed for %s, pcrel = %d\n", S_GET_NAME(sym), insn->i_rp->r_pcrel);
4219
+#endif
4220
+                                       insn->i_rp->r_type = BFD_RELOC_ZIP_MOV_PCREL;
4221
+                               }
4222
+
4223
+
4224
+//                     } else if((zip_param_got)&&(insn->i_breg == ZIP_GBL)) {
4225
+//                             immv = symv;
4226
+//                             insn->i_rp->r_type = BFD_RELOC_ZIP_MOV_GOTREL;
4227
+//                             as_bad("MOV x(GOT),Ry not yet implemented");
4228
+                       } else {
4229
+                               // Do we really want to assume this is an
4230
+                               // address?
4231
+                               insn->i_rp->r_type = BFD_RELOC_ZIP_MOV_OFFSET;
4232
+                       }
4233
+               } else if ((zip_param_use_machine)&&(pzipm)
4234
+                               &&(insn->i_breg<ZIP_CC)&&(insn->i_areg<ZIP_CC)
4235
+                               &&(ZIPC_ALWAYS == insn->i_cnd)
4236
+                               &&(pzipm->r[insn->i_breg].m_known==MACH_VKNOWN)
4237
+                               &&(fits_within(4,pzipm->r[insn->i_breg].m_value
4238
+                                       +immv))) {
4239
+                       // MOV Rb,Ra
4240
+                       // turn this into an LDI instruction, rather than a
4241
+                       // move instruction
4242
+                       insn->i_op = ZIPO_LDI;
4243
+                       insn->i_imm += pzipm->r[insn->i_breg].m_value;
4244
+                       insn->i_breg = ZIP_RNONE;
4245
+
4246
+                       insn->i_code = LDIOP(insn->i_imm, insn->i_areg);
4247
+                       insn->i_naux = 0;
4248
+               }
4249
+               /*
4250
+               // Handle offsets greater than 13 bits --- up to 32 bits
4251
+               // Only works if we have no symbol in question, and when the
4252
+               // target isn't the PC register (or CC for that matter).
4253
+               else if ((!fits_within(13,insn->i_imm))
4254
+                               &&(insn->i_areg < ZIP_CC)
4255
+                               &&(insn->i_breg < ZIP_CC)) {
4256
+                       // Alternate instruction sequence:
4257
+                       //      but .... these make conditional insns from
4258
+                       //              unconditional!!
4259
+                       //      MOV.always A,Rx -> LDI A,Rx
4260
+                       //      MOV.always A(Ry),Rx -> LDI A,Rx, ADD Ry,Rx
4261
+                       // this works for conditional moves, since ADD.C
4262
+                       // doesn't set conditions
4263
+                       //      MOV.C A(Ry),Rx -> LDI.C A,Rx, ADD.C Ry,Rx
4264
+
4265
+                       return;
4266
+               }
4267
+               */
4268
+
4269
+               if (insn->i_op == ZIPO_MOV) { // If we haven't changed insns,
4270
+                       gas_assert(fits_within(13, insn->i_imm));
4271
+                       insn->i_code = SMPLMOV(insn->i_cnd, immv, insn->i_breg,
4272
+                                       insn->i_areg);
4273
+                       //
4274
+                       // Now for the extra two register bits only present
4275
+                       // in move instructions
4276
+                       //
4277
+                       insn->i_code |= (insn->i_areg&0x10)?0x40000:0;
4278
+                       insn->i_code |= (insn->i_breg&0x10)?0x02000:0;
4279
+               }
4280
+#ifdef ZIP_DEBUG
4281
+               fprintf(stderr, "MOV Instruction assembled into %08x\n", insn->i_code);
4282
+#endif
4283
+               break;
4284
+       case ZIPO_NEG:
4285
+               insn->i_naux = 1;
4286
+               if (insn->i_rp)
4287
+                       as_bad("NEG cannot handle symbols");
4288
+               gas_assert(fits_within(13, -1+insn->i_imm));
4289
+               insn->i_code = SMPLMOV(insn->i_cnd,
4290
+                       (-1+immv), insn->i_breg, insn->i_areg);
4291
+               insn->i_code &= 0xfffbdfff;
4292
+               insn->i_aux[0] = IMMOP(ZIPO_XOR, insn->i_cnd, -1,
4293
+                       insn->i_areg);
4294
+               // printf("NEG Instruction assembled into %08x:%08x\n",
4295
+                       // insn->i_code, insn->i_aux[0]);
4296
+               break;
4297
+       case ZIPO_BREAK: case ZIPO_LOCK: case ZIPO_SIM: case ZIPO_NOOP:
4298
+               if (insn->i_cnd != ZIPC_ALWAYS)
4299
+                       as_bad("NOOP/BREAK/LOCK/SIM instructions cannot handle conditions\n");
4300
+               insn->i_code = NOOPGROUP(insn->i_op, insn->i_imm);
4301
+               insn->i_naux = 0;
4302
+               break;
4303
+       case    ZIPO_SDUMP: case ZIPO_NDUMP: case ZIPO_SEXIT: case ZIPO_NEXIT:
4304
+       case    ZIPO_SOUT:  case ZIPO_NOUT:
4305
+               // These instructions *should*ve been turned into NOOP and SIM
4306
+               // instructions by now.  Getting here with them is therefore
4307
+               // an error.
4308
+               gas_assert(0);
4309
+       case ZIPO_SEXTB: {
4310
+               insn->i_naux = 1;
4311
+               insn->i_code   = IMMOP(ZIPO_LSL, insn->i_cnd, 24, insn->i_areg);
4312
+               insn->i_aux[0] = IMMOP(ZIPO_ASR, insn->i_cnd, 24, insn->i_areg);
4313
+               } break;
4314
+       case ZIPO_SEXTH: {
4315
+               insn->i_naux = 1;
4316
+               insn->i_code   = IMMOP(ZIPO_LSL, insn->i_cnd, 16, insn->i_areg);
4317
+               insn->i_aux[0] = IMMOP(ZIPO_ASR, insn->i_cnd, 16, insn->i_areg);
4318
+               } break;
4319
+       case ZIPO_JSR:
4320
+               if ((zip_param_cis)&&(insn->i_cnd == ZIPC_ALWAYS)
4321
+                               &&(insn->i_rp)) {
4322
+                       insn->i_op = ZIPO_LJSR;
4323
+                       zip_assemble_insn_words(fragP, seg, insn, relax_state, stretch, pzipm);
4324
+                       insn->i_op = ZIPO_JSR;
4325
+               } else if ((zip_param_cis)&&(insn->i_cnd == ZIPC_ALWAYS)
4326
+                               &&(insn->i_breg != ZIP_RNONE)
4327
+                               &&(insn->i_imm == 0)) {
4328
+                       // JSR (Register)
4329
+                       insn->i_naux = 0;
4330
+                       insn->i_code = 0x80008000
4331
+                               | (ZIPV_MOV<<24)
4332
+                               | 0x00f90000    // MOV OpB = PC+1
4333
+                               | 0x0f880       // MOV into PC
4334
+                               | ((insn->i_breg&0x0f)<<3)
4335
+                               | (ZIPV_MOV << 8); // The LW instruction
4336
+
4337
+                       gas_assert(!insn->i_rp);
4338
+               } else if ((!insn->i_rp)&&(insn->i_breg != ZIP_RNONE)) {
4339
+                       gas_assert(fits_within(13,immv));
4340
+                       // JSR offset+register, implemented as two moves
4341
+                       insn->i_naux = 1;
4342
+                       insn->i_code = SMPLMOV(insn->i_cnd, 1, ZIP_PC, ZIP_LR);
4343
+                       insn->i_aux[0] = SMPLMOV(insn->i_cnd, immv, insn->i_breg, ZIP_PC);
4344
+                       insn->i_aux[1] = NOOP_OPCODE;
4345
+               } else {
4346
+                       if (insn->i_rp)
4347
+                               immv += (int)(symv
4348
+                                       - fragP->fr_address
4349
+                                       - (insn->i_rp->r_fr_offset+sizeof(uint32_t)));
4350
+                       if ((zip_param_small)||((sym_known)&&(fits_within(18,immv-4)))) {
4351
+                               // MOV.C 1(PC),R0
4352
+                               // BRA.C _somewhere_
4353
+                               insn->i_naux = 1;
4354
+                               insn->i_code = SMPLMOV(insn->i_cnd, 1, ZIP_PC,
4355
+                                       ZIP_LR);
4356
+                               // Bias the branch by -4, since we're one word
4357
+                               // into this instruction
4358
+                               insn->i_aux[0] = IMMOP(ZIPO_ADD, insn->i_cnd,
4359
+                                       immv-4, ZIP_PC);
4360
+                               insn->i_aux[1] = NOOP_OPCODE;
4361
+                               insn->i_rp->r_pcrel = TRUE;
4362
+                               insn->i_rp->r_type = BFD_RELOC_NONE;
4363
+                       } else {
4364
+                               // If all else fails, call for a long JSR
4365
+                               insn->i_op = ZIPO_LJSR;
4366
+                               zip_assemble_insn_words(fragP, seg, insn,
4367
+                                       relax_state, stretch, pzipm);
4368
+                               insn->i_op = ZIPO_JSR;
4369
+                       }
4370
+               } break;
4371
+       case ZIPO_LJSR:
4372
+               if (insn->i_rp)
4373
+                       immv += (int)(symv
4374
+                               - fragP->fr_address
4375
+                               - (insn->i_rp->r_fr_offset+sizeof(uint32_t)));
4376
+               if (insn->i_rp) {
4377
+                       insn->i_rp->r_pcrel = FALSE;
4378
+                       insn->i_rp->r_type = BFD_RELOC_ZIP_VALUE;
4379
+               } insn->i_breg = ZIP_RNONE;
4380
+               if (insn->i_cnd == ZIPC_ALWAYS) {
4381
+                       if (zip_param_cis) {
4382
+                               insn->i_naux = 1;
4383
+                               insn->i_code = 0x80008000
4384
+                                       | (ZIPV_MOV<<24)
4385
+                                       | 0x00fa0000    // MOV OpB = PC+2
4386
+                                       | 0x0f8f8       // (LW) into PC
4387
+                                       | (ZIPV_LW << 8); // The LW instruction
4388
+                               insn->i_aux[0] = 0; // To be filled in with address value
4389
+                       } else { // If unconditional
4390
+                               // MOV 1(PC),R0
4391
+                               // LW (PC),PC
4392
+                               // _somewhere_
4393
+                               insn->i_naux = 2;
4394
+                               insn->i_code = SMPLMOV(insn->i_cnd, 2, ZIP_PC,
4395
+                                       ZIP_LR);
4396
+                               insn->i_aux[0] = DBLREGOP(ZIPO_LW,ZIPC_ALWAYS,
4397
+                                       0,ZIP_PC,ZIP_PC); // Load into PC register
4398
+                               insn->i_aux[1] = 0;
4399
+                       }
4400
+               } else {
4401
+                       // If conditional, naux = 3
4402
+                       // BRA.!cnd (for whatever condition) +3
4403
+                       // MOV 4(PC),R0
4404
+                       // LW (PC),PC
4405
+                       // address
4406
+                       insn->i_naux = 3;
4407
+                       insn->i_aux[2] = 0;
4408
+                       if (insn->i_cnd != ZIPC_V) {
4409
+                               ZIP_CONDITION cnd =
4410
+                                       zip_negate_condition(insn->i_cnd);
4411
+                               insn->i_code = IMMOP(ZIPO_ADD, cnd, 12, ZIP_PC);
4412
+                               insn->i_aux[0] = SMPLMOV(ZIPC_ALWAYS, 2, ZIP_PC,
4413
+                                       ZIP_LR);
4414
+                               insn->i_aux[1] = DBLREGOP(ZIPO_LW, ZIPC_ALWAYS,
4415
+                                       0,ZIP_PC,ZIP_PC); // Load into PC register
4416
+                       } else {
4417
+                               insn->i_code = SMPLMOV(ZIPC_V, 3, ZIP_PC,
4418
+                                       ZIP_LR);
4419
+                               insn->i_aux[0] = DBLREGOP(ZIPO_LW, ZIPC_V,
4420
+                                       0,ZIP_PC,ZIP_PC); // Load into PC register
4421
+                               insn->i_aux[1] = IMMOP(ZIPO_ADD, ZIPC_ALWAYS, 4, ZIP_PC);
4422
+                       }
4423
+               } if (!insn->i_rp)
4424
+                       insn->i_aux[insn->i_naux-1] = immv;
4425
+               break;
4426
+       default:
4427
+               if (insn->i_rp) {
4428
+                       insn->i_rp->r_pcrel = FALSE;
4429
+                       immv += symv;
4430
+                       if (insn->i_breg == ZIP_PC) {
4431
+                               immv+=(int)(-fragP->fr_address
4432
+                                               -insn->i_rp->r_fr_offset-sizeof(uint32_t));
4433
+                               insn->i_rp->r_pcrel= TRUE;
4434
+                               insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_PCREL;
4435
+                               if ((sym_known)&&(this_segment))
4436
+                                       insn->i_rp->r_type = BFD_RELOC_NONE;
4437
+//                     } else if((zip_param_got)&&(insn->i_breg == ZIP_GBL)) {
4438
+//                             as_bad("<OP> x(GOT),Ry not yet implemented");
4439
+//                             immv += symv;
4440
+//                             insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_GOTREL;
4441
+                       } else {
4442
+                               // Do we really want to assume this is an
4443
+                               // address?
4444
+                               immv += symv;
4445
+                               insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_OFFSET;
4446
+                       }
4447
+               } if (insn->i_breg != ZIP_RNONE) {
4448
+                       if (insn->i_breg == ZIP_PC)
4449
+                               immv >>= 2;
4450
+                       if (!fits_within(14, insn->i_imm)) {
4451
+                               fprintf(stderr, "ERR: (%d) 0x%08x does not fit within 18 bits!\n", insn->i_imm, insn->i_imm);
4452
+                               gas_assert(fits_within(14, insn->i_imm));
4453
+                       }
4454
+                       insn->i_code = DBLREGOP(insn->i_op, insn->i_cnd,
4455
+                               immv, insn->i_breg, insn->i_areg);
4456
+                       if (insn->i_rp) {
4457
+                               if (insn->i_breg == ZIP_PC) {
4458
+                                       insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_PCREL;
4459
+                                       insn->i_rp->r_pcrel = TRUE;
4460
+//                             } else if((zip_param_got)&&(insn->i_breg == ZIP_GBL)) {
4461
+//                                     insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_GOTREL;
4462
+                               } else
4463
+                                       insn->i_rp->r_type = BFD_RELOC_ZIP_OPB_OFFSET;
4464
+                       }
4465
+               } else {
4466
+                       if (!fits_within(18, insn->i_imm)) {
4467
+                               fprintf(stderr, "ERR: (%d) 0x%08x does not fit within 18 bits!\n", insn->i_imm, insn->i_imm);
4468
+                               gas_assert(fits_within(18, insn->i_imm));
4469
+                       }
4470
+                       insn->i_code = IMMOP(insn->i_op, insn->i_cnd,
4471
+                               immv, insn->i_areg);
4472
+                       if (insn->i_rp)
4473
+                               insn->i_rp->r_type  = BFD_RELOC_ZIP_OPB_IMM;
4474
+               }
4475
+#ifdef ZIP_DEBUG
4476
+               fprintf(stderr, "Instruction assembled into %08x%s\n", insn->i_code,
4477
+                       (insn->i_rp)?" w/ Symbol":"");
4478
+#endif
4479
+               break;
4480
+       }
4481
+}
4482
+
4483
+void
4484
+md_assemble(char *line) {
4485
+       const char *linep;
4486
+       char    *p;
4487
+
4488
+       linep = (char *)zip_skip_white_spaces(line);
4489
+       if ((strncasecmp(linep, "nstr", 4)==0)
4490
+               ||(strncasecmp(linep, "sstr", 4)==0)) {
4491
+               uint32_t        base;
4492
+               const char      *sbeg;
4493
+
4494
+               if ((linep[0] =='n')||(linep[0] == 'N'))
4495
+                       base = 0x0400 + NOOP_OPCODE;
4496
+               else
4497
+                       base = 0x0400 + SIM_OPCODE;
4498
+
4499
+               sbeg = strchr(line, '\"');
4500
+               if (!sbeg)
4501
+                       as_bad("String instruction with no quotes found");
4502
+               sbeg++;
4503
+               while(*sbeg != '\"') {
4504
+                       unsigned v = *sbeg++;
4505
+                       if (v == '\\') {
4506
+                               switch(*sbeg++) {
4507
+                               case 'a':       v = '\a'; break;
4508
+                               case 'b':       v = '\b'; break;
4509
+                               case 'f':       v = '\f'; break;
4510
+                               case 'n':       v = '\n'; break;
4511
+                               case 'r':       v = '\r'; break;
4512
+                               case 't':       v = '\t'; break;
4513
+                               case '\\':      v = '\\'; break;
4514
+                               case '\"':      v = '\"'; break;
4515
+                               case '\'':      v = '\''; break;
4516
+                               default: sbeg--;
4517
+                               }
4518
+                       }
4519
+                       p = frag_more(sizeof(uint32_t));
4520
+                       md_number_to_chars(p,base+(v&0x0ff),sizeof(uint32_t));
4521
+               }
4522
+
4523
+               return;
4524
+       }
4525
+
4526
+
4527
+       // Decode an instruction from op_str
4528
+       //      Determine the opcode
4529
+       //              any conditions
4530
+       //              the result register
4531
+       //              any immediate
4532
+       //              any "B" register
4533
+       ZIPIS   insnv, *insn = &insnv;
4534
+       const char *error = zip_parse(line, insn);
4535
+
4536
+#ifdef ZIP_DEBUG
4537
+       fprintf(stderr, "ZIP-ASSEMBLE %s\n", line);
4538
+#endif
4539
+
4540
+       if (error) {
4541
+               as_bad("%s \'%s\'", error, line);
4542
+               return;
4543
+       }
4544
+
4545
+       zip_assemble_insn_words(frag_now, now_seg, insn, 0, 0, &zipm);
4546
+       if (zip_param_use_machine)
4547
+               zip_advance_machine(&zipm, insn);
4548
+#ifdef ZIP_DEBUG
4549
+       zip_debug_machine(&zipm);
4550
+#endif
4551
+
4552
+       // Then, if the instruction is valid we ...
4553
+       //      for branches ...
4554
+       //        symbolS *symp = reloc->reloc_expression.X_add_symbol;
4555
+       //        offsetT offset = reloc->reloc_expression.X_add_number;
4556
+       //        char *f;
4557
+       //        f = frag_var(?, 4, 4, BRANCH_SUBTYPE, symp, offset, NULL);
4558
+       //        md_number_to_chars(f, insn->insn_code, 4); // ???
4559
+       //
4560
+       //
4561
+       if ((!insn->i_rp)&&(cis_mergable)&&(insn->i_naux == 0)
4562
+                       &&(frag_now_fix_octets()>=4)) {
4563
+               // ZIPI lcode = md_number_from_chars(p, 4);
4564
+               unsigned int    last_iword;
4565
+               char    *buf;
4566
+
4567
+               buf = &frag_now->fr_literal[frag_now_fix_octets() - 4];
4568
+               last_iword = (buf[3]&0x0ff)|((buf[2]&0x0ff)<<8)
4569
+                       |((buf[1]&0x0ff)<<16)|((buf[0]&0x0ff)<<24);
4570
+               if ((buf >= frag_now->fr_literal)
4571
+                               &&(zip_can_merge(last_iword,insn->i_code))) {
4572
+                       p = buf;
4573
+                       insn->i_code =zip_insn_merge(last_iword, insn->i_code);
4574
+                       md_number_to_chars(p, insn->i_code, sizeof(uint32_t));
4575
+               } else {
4576
+                       p = frag_more(sizeof(uint32_t)*(1+insn->i_naux));
4577
+                       md_number_to_chars(p, insn->i_code, sizeof(uint32_t));
4578
+               }
4579
+       } else {
4580
+               int     i;
4581
+               ZIPRELOC        *rp = insn->i_rp;
4582
+
4583
+               if ((insn->i_naux == 0)||(NULL == insn->i_rp)) {
4584
+                       // If we have no need of any symbols, we come here.
4585
+                       // If we have no need of any extra 4-byte instructions,
4586
+                       // we come here.
4587
+                       p = frag_more(sizeof(uint32_t)*(1+insn->i_naux));
4588
+
4589
+                       if (insn->i_rp) {
4590
+                               // Generate a fixup for when we finally
4591
+                               // know where this symbol links to
4592
+                               insn->i_rp->r_fr_offset =
4593
+                                       (p - frag_now->fr_literal);
4594
+                               insn->i_rp->r_fix = fix_new(frag_now,
4595
+                                       insn->i_rp->r_fr_offset, // where w/in frag?
4596
+                                       4,              // 1,2, or 4 usually ... ??
4597
+                                       insn->i_rp->r_sym, // Add symbol,
4598
+                                       insn->i_imm, // Fixed/known offset to the symbol
4599
+                                       insn->i_rp->r_pcrel, // T if PC-Relative reloc
4600
+                                       insn->i_rp->r_type); // Reloc type
4601
+                               insn->i_rp = NULL;
4602
+                       }
4603
+               } else {
4604
+                       // First, grow our fragment so we know we have the
4605
+                       // size we'll need.
4606
+                       frag_grow(sizeof(uint32_t)*(1+insn->i_naux));
4607
+
4608
+                       // Then p, the place where we'd get more from, is
4609
+                       // where our instruction will be installed.  We know
4610
+                       // there's room for it there now.
4611
+                       p = frag_more(0);
4612
+                       // Record where in the frag our relaxable instruction
4613
+                       // will be installed
4614
+                       insn->i_rp->r_fr_offset = ( p - frag_now->fr_literal );
4615
+                       // Now tell the assembler about what we've done.
4616
+                       // This'll close off the frag as well, so this is the
4617
+                       // last we'll see of this frag--even though frag_now
4618
+                       // may yet point to it (or not) for a while.
4619
+                       frag_var(rs_machine_dependent, // Relaxation type
4620
+                               sizeof(uint32_t)*(1+insn->i_naux),
4621
+                               sizeof(uint32_t)*(1+insn->i_naux),
4622
+                               rs_machine_dependent,   // Subtype
4623
+                               insn->i_rp->r_sym,      // Symbol
4624
+                               insn->i_imm,    // Offset (to be added to symbol)
4625
+                               (char *)zip_copy_insn(insn));   // Opcode
4626
+
4627
+               }
4628
+               md_number_to_chars(p, insn->i_code, sizeof(uint32_t));
4629
+               for(i=0; i< insn->i_naux; i++)
4630
+                       md_number_to_chars(&p[sizeof(uint32_t)*(1+i)], insn->i_aux[i], sizeof(uint32_t));
4631
+
4632
+               if (rp) {
4633
+                       cis_mergable = FALSE;
4634
+               } else {
4635
+                       cis_mergable = (zip_param_cis)&&(insn->i_naux == 0);
4636
+               }
4637
+       }
4638
+#ifdef ZIP_DEBUG
4639
+       zip_dump_insn(insn);
4640
+#endif
4641
+}
4642
+
4643
+/* Turn a string in input_line_pointer into a floating point constant
4644
+ * of type type, and store the appropriate bytes in *LITP.  The number of
4645
+ * LIITTLENUMS emitted is stored in *SIZEP.  An error message is returned, or
4646
+ * NULL on OK.
4647
+ */
4648
+const char     *
4649
+md_atof(int type, char *litP, int *sizeP) {
4650
+       int     prec;
4651
+       LITTLENUM_TYPE  words[4];
4652
+       char    *t;
4653
+       int     i;
4654
+
4655
+       switch(type) {
4656
+       case 'f':
4657
+               prec = 2; break;
4658
+       case 'd':
4659
+               prec = 4; break;
4660
+       default:
4661
+               *sizeP = 0;
4662
+               return _("Unsupported floating point format");
4663
+       }
4664
+
4665
+       t = atof_ieee(input_line_pointer, type, words);
4666
+       if (t)
4667
+               input_line_pointer = t;
4668
+
4669
+       *sizeP = prec * 2;
4670
+
4671
+       for(i=prec-1; i>= 0; i--) {
4672
+               md_number_to_chars(litP, (valueT)words[i], 2);
4673
+               litP += 2;
4674
+       }
4675
+
4676
+       return NULL;
4677
+}
4678
+#define        AS_BAD_FIX(FXP,STR) as_bad_where(FXP->fx_file,FXP->fx_line,_(STR))
4679
+
4680
+/* Apply a fixup to the object file
4681
+ *
4682
+ * This function will only be called once all of the details of the fixup are
4683
+ * known, before the object file heads to the linker (if at all).
4684
+ */
4685
+void
4686
+md_apply_fix(fixS *fixP, valueT *val, segT seg ATTRIBUTE_UNUSED)
4687
+{
4688
+       unsigned int    iword, mask, final_fix;
4689
+
4690
+       final_fix = (fixP->fx_addsy == NULL) ? TRUE : FALSE;
4691
+
4692
+       bfd_byte *buf = (bfd_byte *)&fixP->fx_frag->fr_literal[fixP->fx_where];
4693
+       iword = (buf[3]&0x0ff)|((buf[2]&0x0ff)<<8)
4694
+                       |((buf[1]&0x0ff)<<16)|((buf[0]&0x0ff)<<24);
4695
+       (*val) &= 0x0ffffffff;
4696
+
4697
+#ifdef ZIP_DEBUG
4698
+       fprintf(stderr, "MD-APPLY-FIX: FRAG=%08x@%08x+%08x(%d), IWORD = %08x Val=%08x (RTYP=%d->%s)%s%s, ADDEND=%08lx\n",
4699
+               ((unsigned)((unsigned long)fixP->fx_frag->fr_literal)),
4700
+               ((unsigned)((unsigned long)fixP->fx_frag->fr_address)),
4701
+               ((unsigned)((unsigned long)fixP->fx_where)),
4702
+               ((unsigned)((unsigned long)fixP->fx_size)),
4703
+               iword, (unsigned)(*val), fixP->fx_r_type,
4704
+               bfd_get_reloc_code_name(fixP->fx_r_type),
4705
+               (fixP->fx_pcrel)?" PC-Rel ":" std ",
4706
+               (fixP->fx_addsy)?"":"(Null Sym)",
4707
+               fixP->fx_offset);
4708
+#endif
4709
+       switch(fixP->fx_r_type) {
4710
+       case BFD_RELOC_NONE:
4711
+               fixP->fx_done = final_fix;
4712
+               break;
4713
+       case BFD_RELOC_ZIP_OPB_IMM:
4714
+               // printf("\tMD-APPLY-FIX: BFD_RELOC_ZIP_OPB_IMM\n");
4715
+               mask = 0x3ffff; iword = (iword &(~mask))|((*val)&mask);
4716
+               (*val) &= 0x03ffff;
4717
+               md_number_to_chars((char *)buf, iword, sizeof(uint32_t));
4718
+               fixP->fx_done = final_fix;
4719
+               break;
4720
+       case BFD_RELOC_ZIP_OPB_PCREL:
4721
+               {
4722
+               uint32_t        oval;
4723
+               oval = *val;
4724
+               oval = (*val>>2);
4725
+               oval &= 0x03fff;
4726
+               mask = 0x3fff; iword = (iword &(~mask))|((oval)&mask);
4727
+               *val = oval;
4728
+               md_number_to_chars((char *)buf, iword, sizeof(uint32_t));
4729
+               fixP->fx_done = final_fix;
4730
+               } break;
4731
+       case BFD_RELOC_ZIP_OPB_OFFSET:
4732
+//     case BFD_RELOC_ZIP_OPB_GOTREL:
4733
+       case BFD_RELOC_14:
4734
+               // if (fixP->fx_r_type == BFD_RELOC_ZIP_OPB_OFFSET)
4735
+                       // printf("\tMD-APPLY-FIX: BFD_RELOC_ZIP_OPB_OFFSET\n");
4736
+               // else if (fixP->fx_r_type == BFD_RELOC_ZIP_OPB_PCREL)
4737
+                       // printf("\tMD-APPLY-FIX: BFD_RELOC_ZIP_OPB_PCREL\n");
4738