~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/cmd/8a/lex.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-11-18 15:12:26 UTC
  • mfrom: (14.2.12 vivid-proposed)
  • Revision ID: package-import@ubuntu.com-20141118151226-zug7vn93mn3dtiz3
Tags: 2:1.3.2-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - Support co-installability with gccgo-go tool:
    - d/rules,golang-go.install: Rename bin/go -> bin/golang-go
    - d/golang-go.{postinst,prerm}: Install/remove /usr/bin/go using
      alternatives.
  - d/copyright: Amendments for full compiliance with copyright format.
  - d/control: Demote golang-go.tools to Suggests to support Ubuntu MIR.
  - dropped patches (now upstream):
    - d/p/issue27650045_40001_50001.diff
    - d/p/issue28050043_60001_70001.diff
    - d/p/issue54790044_100001_110001.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
        return '/';
58
58
}
59
59
 
 
60
int
 
61
Lconv(Fmt *fp)
 
62
{
 
63
        return linklinefmt(ctxt, fp);
 
64
}
 
65
 
 
66
void
 
67
dodef(char *p)
 
68
{
 
69
        if(nDlist%8 == 0)
 
70
                Dlist = allocn(Dlist, nDlist*sizeof(char *),
 
71
                        8*sizeof(char *));
 
72
        Dlist[nDlist++] = p;
 
73
}
 
74
 
 
75
void
 
76
usage(void)
 
77
{
 
78
        print("usage: %ca [options] file.c...\n", thechar);
 
79
        flagprint(1);
 
80
        errorexit();
 
81
}
60
82
void
61
83
main(int argc, char *argv[])
62
84
{
63
85
        char *p;
64
 
        int c;
65
86
 
66
87
        thechar = '8';
67
88
        thestring = "386";
68
89
 
 
90
        ctxt = linknew(&link386);
 
91
        ctxt->diag = yyerror;
 
92
        ctxt->bso = &bstdout;
 
93
        Binit(&bstdout, 1, OWRITE);
 
94
        listinit8();
 
95
        fmtinstall('L', Lconv);
 
96
 
 
97
        // Allow GOARCH=thestring or GOARCH=thestringsuffix,
 
98
        // but not other values.        
 
99
        p = getgoarch();
 
100
        if(strncmp(p, thestring, strlen(thestring)) != 0)
 
101
                sysfatal("cannot use %cc with GOARCH=%s", thechar, p);
 
102
 
69
103
        ensuresymb(NSYMB);
70
104
        memset(debug, 0, sizeof(debug));
71
105
        cinit();
72
106
        outfile = 0;
73
107
        setinclude(".");
74
 
        ARGBEGIN {
75
 
        default:
76
 
                c = ARGC();
77
 
                if(c >= 0 && c < sizeof(debug))
78
 
                        debug[c] = 1;
79
 
                break;
80
 
 
81
 
        case 'o':
82
 
                outfile = ARGF();
83
 
                break;
84
 
 
85
 
        case 'D':
86
 
                p = ARGF();
87
 
                if(p) {
88
 
                        if (nDlist%8 == 0)
89
 
                                Dlist = allocn(Dlist, nDlist*sizeof(char *), 
90
 
                                        8*sizeof(char *));
91
 
                        Dlist[nDlist++] = p;
92
 
                }
93
 
                break;
94
 
 
95
 
        case 'I':
96
 
                p = ARGF();
97
 
                setinclude(p);
98
 
                break;
99
 
        } ARGEND
100
 
        if(*argv == 0) {
101
 
                print("usage: %ca [-options] file.s\n", thechar);
102
 
                errorexit();
103
 
        }
 
108
        
 
109
        flagfn1("D", "name[=value]: add #define", dodef);
 
110
        flagfn1("I", "dir: add dir to include path", setinclude);
 
111
        flagcount("S", "print assembly and machine code", &debug['S']);
 
112
        flagcount("m", "debug preprocessor macros", &debug['m']);
 
113
        flagstr("o", "file: set output file", &outfile);
 
114
        flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath);
 
115
 
 
116
        flagparse(&argc, &argv, usage);
 
117
        ctxt->debugasm = debug['S'];
 
118
 
 
119
        if(argc < 1)
 
120
                usage();
104
121
        if(argc > 1){
105
122
                print("can't assemble multiple files\n");
106
123
                errorexit();
107
124
        }
 
125
 
108
126
        if(assemble(argv[0]))
109
127
                errorexit();
 
128
        Bflush(&bstdout);
110
129
        exits(0);
111
130
}
112
131
 
145
164
                errorexit();
146
165
        }
147
166
        Binit(&obuf, of, OWRITE);
148
 
 
149
 
        pass = 1;
150
 
        pinit(file);
151
 
 
152
 
        Bprint(&obuf, "go object %s %s %s\n", getgoos(), thestring, getgoversion());
153
 
 
154
 
        for(i=0; i<nDlist; i++)
155
 
                dodefine(Dlist[i]);
156
 
        yyparse();
157
 
        if(nerrors) {
 
167
        Bprint(&obuf, "go object %s %s %s\n", getgoos(), getgoarch(), getgoversion());
 
168
        Bprint(&obuf, "!\n");
 
169
 
 
170
        for(pass = 1; pass <= 2; pass++) {
 
171
                pinit(file);
 
172
                for(i=0; i<nDlist; i++)
 
173
                        dodefine(Dlist[i]);
 
174
                yyparse();
158
175
                cclean();
159
 
                return nerrors;
 
176
                if(nerrors)
 
177
                        return nerrors;
160
178
        }
161
179
 
162
 
        Bprint(&obuf, "\n!\n");
163
 
 
164
 
        pass = 2;
165
 
        outhist();
166
 
        pinit(file);
167
 
        for(i=0; i<nDlist; i++)
168
 
                dodefine(Dlist[i]);
169
 
        yyparse();
170
 
        cclean();
171
 
        return nerrors;
 
180
        writeobj(ctxt, &obuf);
 
181
        Bflush(&obuf);
 
182
        return 0;
172
183
}
173
184
 
174
185
struct
225
236
        "ES",           LSREG,  D_ES,
226
237
        "FS",           LSREG,  D_FS,
227
238
        "GS",           LSREG,  D_GS,
 
239
        "TLS",          LSREG,  D_TLS,
228
240
 
229
241
        "GDTR",         LBREG,  D_GDTR,
230
242
        "IDTR",         LBREG,  D_IDTR,
778
790
        "PSUBW",        LTYPE3, APSUBW,
779
791
        "PUNPCKHQDQ",   LTYPE3, APUNPCKHQDQ,
780
792
        "PUNPCKLQDQ",   LTYPE3, APUNPCKLQDQ,
 
793
        "PXOR",         LTYPE3, APXOR,
781
794
        "RCPPS",        LTYPE3, ARCPPS,
782
795
        "RCPSS",        LTYPE3, ARCPSS,
783
796
        "RSQRTPS",      LTYPE3, ARSQRTPS,
810
823
        Sym *s;
811
824
        int i;
812
825
 
813
 
        nullgen.sym = S;
814
 
        nullgen.offset = 0;
815
 
        if(FPCHIP)
816
 
                nullgen.dval = 0;
817
 
        for(i=0; i<sizeof(nullgen.sval); i++)
818
 
                nullgen.sval[i] = 0;
819
826
        nullgen.type = D_NONE;
820
827
        nullgen.index = D_NONE;
821
 
        nullgen.scale = 0;
822
828
 
823
829
        nerrors = 0;
824
830
        iostack = I;
834
840
                s->type = itab[i].type;
835
841
                s->value = itab[i].value;
836
842
        }
837
 
 
838
 
        pathname = allocn(pathname, 0, 100);
839
 
        if(getwd(pathname, 99) == 0) {
840
 
                pathname = allocn(pathname, 100, 900);
841
 
                if(getwd(pathname, 999) == 0)
842
 
                        strcpy(pathname, "/???");
843
 
        }
844
843
}
845
844
 
846
845
void
868
867
void
869
868
cclean(void)
870
869
{
871
 
        Gen2 g2;
 
870
        Addr2 g2;
872
871
 
873
872
        g2.from = nullgen;
874
873
        g2.to = nullgen;
875
874
        outcode(AEND, &g2);
876
 
        Bflush(&obuf);
877
 
}
878
 
 
879
 
void
880
 
zname(char *n, int t, int s)
881
 
{
882
 
 
883
 
        BPUTLE2(&obuf, ANAME);          /* as(2) */
884
 
        BPUTC(&obuf, t);                /* type */
885
 
        BPUTC(&obuf, s);                /* sym */
886
 
        while(*n) {
887
 
                BPUTC(&obuf, *n);
888
 
                n++;
889
 
        }
890
 
        BPUTC(&obuf, 0);
891
 
}
892
 
 
893
 
void
894
 
zaddr(Gen *a, int s)
895
 
{
896
 
        int32 l;
897
 
        int i, t;
898
 
        char *n;
899
 
        Ieee e;
900
 
 
901
 
        t = 0;
902
 
        if(a->index != D_NONE || a->scale != 0)
903
 
                t |= T_INDEX;
904
 
        if(a->offset != 0)
905
 
                t |= T_OFFSET;
906
 
        if(s != 0)
907
 
                t |= T_SYM;
908
 
 
909
 
        switch(a->type) {
910
 
        default:
911
 
                t |= T_TYPE;
912
 
                break;
913
 
        case D_FCONST:
914
 
                t |= T_FCONST;
915
 
                break;
916
 
        case D_CONST2:
917
 
                t |= T_OFFSET|T_OFFSET2;
918
 
                break;
919
 
        case D_SCONST:
920
 
                t |= T_SCONST;
921
 
                break;
922
 
        case D_NONE:
923
 
                break;
924
 
        }
925
 
        BPUTC(&obuf, t);
926
 
 
927
 
        if(t & T_INDEX) {       /* implies index, scale */
928
 
                BPUTC(&obuf, a->index);
929
 
                BPUTC(&obuf, a->scale);
930
 
        }
931
 
        if(t & T_OFFSET) {      /* implies offset */
932
 
                l = a->offset;
933
 
                BPUTLE4(&obuf, l);
934
 
        }
935
 
        if(t & T_OFFSET2) {
936
 
                l = a->offset2;
937
 
                BPUTLE4(&obuf, l);
938
 
        }
939
 
        if(t & T_SYM)           /* implies sym */
940
 
                BPUTC(&obuf, s);
941
 
        if(t & T_FCONST) {
942
 
                ieeedtod(&e, a->dval);
943
 
                BPUTLE4(&obuf, e.l);
944
 
                BPUTLE4(&obuf, e.h);
945
 
                return;
946
 
        }
947
 
        if(t & T_SCONST) {
948
 
                n = a->sval;
949
 
                for(i=0; i<NSNAME; i++) {
950
 
                        BPUTC(&obuf, *n);
951
 
                        n++;
952
 
                }
953
 
                return;
954
 
        }
955
 
        if(t & T_TYPE)
956
 
                BPUTC(&obuf, a->type);
957
 
}
958
 
 
959
 
void
960
 
outcode(int a, Gen2 *g2)
961
 
{
962
 
        int sf, st, t;
963
 
        Sym *s;
964
 
 
 
875
}
 
876
 
 
877
static Prog *lastpc;
 
878
 
 
879
void
 
880
outcode(int a, Addr2 *g2)
 
881
{
 
882
        Prog *p;
 
883
        Plist *pl;
 
884
        
965
885
        if(pass == 1)
966
886
                goto out;
967
887
 
968
 
jackpot:
969
 
        sf = 0;
970
 
        s = g2->from.sym;
971
 
        while(s != S) {
972
 
                sf = s->sym;
973
 
                if(sf < 0 || sf >= NSYM)
974
 
                        sf = 0;
975
 
                t = g2->from.type;
976
 
                if(t == D_ADDR)
977
 
                        t = g2->from.index;
978
 
                if(h[sf].type == t)
979
 
                if(h[sf].sym == s)
980
 
                        break;
981
 
                zname(s->name, t, sym);
982
 
                s->sym = sym;
983
 
                h[sym].sym = s;
984
 
                h[sym].type = t;
985
 
                sf = sym;
986
 
                sym++;
987
 
                if(sym >= NSYM)
988
 
                        sym = 1;
989
 
                break;
990
 
        }
991
 
        st = 0;
992
 
        s = g2->to.sym;
993
 
        while(s != S) {
994
 
                st = s->sym;
995
 
                if(st < 0 || st >= NSYM)
996
 
                        st = 0;
997
 
                t = g2->to.type;
998
 
                if(t == D_ADDR)
999
 
                        t = g2->to.index;
1000
 
                if(h[st].type == t)
1001
 
                if(h[st].sym == s)
1002
 
                        break;
1003
 
                zname(s->name, t, sym);
1004
 
                s->sym = sym;
1005
 
                h[sym].sym = s;
1006
 
                h[sym].type = t;
1007
 
                st = sym;
1008
 
                sym++;
1009
 
                if(sym >= NSYM)
1010
 
                        sym = 1;
1011
 
                if(st == sf)
1012
 
                        goto jackpot;
1013
 
                break;
1014
 
        }
1015
 
        BPUTLE2(&obuf, a);
1016
 
        BPUTLE4(&obuf, stmtline);
1017
 
        zaddr(&g2->from, sf);
1018
 
        zaddr(&g2->to, st);
 
888
        p = malloc(sizeof *p);
 
889
        memset(p, 0, sizeof *p);
 
890
        p->as = a;
 
891
        p->lineno = stmtline;
 
892
        p->from = g2->from;
 
893
        p->to = g2->to;
 
894
        p->pc = pc;
 
895
 
 
896
        if(lastpc == nil) {
 
897
                pl = linknewplist(ctxt);
 
898
                pl->firstpc = p;
 
899
        } else
 
900
                lastpc->link = p;
 
901
        lastpc = p;     
1019
902
 
1020
903
out:
1021
904
        if(a != AGLOBL && a != ADATA)
1022
905
                pc++;
1023
906
}
1024
907
 
1025
 
void
1026
 
outhist(void)
1027
 
{
1028
 
        Gen g;
1029
 
        Hist *h;
1030
 
        char *p, *q, *op, c;
1031
 
        int n;
1032
 
        char *tofree;
1033
 
        static int first = 1;
1034
 
        static char *goroot, *goroot_final;
1035
 
 
1036
 
        if(first) {
1037
 
                // Decide whether we need to rewrite paths from $GOROOT to $GOROOT_FINAL.
1038
 
                first = 0;
1039
 
                goroot = getenv("GOROOT");
1040
 
                goroot_final = getenv("GOROOT_FINAL");
1041
 
                if(goroot == nil)
1042
 
                        goroot = "";
1043
 
                if(goroot_final == nil)
1044
 
                        goroot_final = goroot;
1045
 
                if(strcmp(goroot, goroot_final) == 0) {
1046
 
                        goroot = nil;
1047
 
                        goroot_final = nil;
1048
 
                }
1049
 
        }
1050
 
 
1051
 
        tofree = nil;
1052
 
 
1053
 
        g = nullgen;
1054
 
        c = pathchar();
1055
 
        for(h = hist; h != H; h = h->link) {
1056
 
                p = h->name;
1057
 
                if(p != nil && goroot != nil) {
1058
 
                        n = strlen(goroot);
1059
 
                        if(strncmp(p, goroot, strlen(goroot)) == 0 && p[n] == '/') {
1060
 
                                tofree = smprint("%s%s", goroot_final, p+n);
1061
 
                                p = tofree;
1062
 
                        }
1063
 
                }
1064
 
                op = 0;
1065
 
                if(systemtype(Windows) && p && p[1] == ':'){
1066
 
                        c = p[2];
1067
 
                } else if(p && p[0] != c && h->offset == 0 && pathname){
1068
 
                        if(systemtype(Windows) && pathname[1] == ':') {
1069
 
                                op = p;
1070
 
                                p = pathname;
1071
 
                                c = p[2];
1072
 
                        } else if(pathname[0] == c){
1073
 
                                op = p;
1074
 
                                p = pathname;
1075
 
                        }
1076
 
                }
1077
 
                while(p) {
1078
 
                        q = strchr(p, c);
1079
 
                        if(q) {
1080
 
                                n = q-p;
1081
 
                                if(n == 0){
1082
 
                                        n = 1;  /* leading "/" */
1083
 
                                        *p = '/';       /* don't emit "\" on windows */
1084
 
                                }
1085
 
                                q++;
1086
 
                        } else {
1087
 
                                n = strlen(p);
1088
 
                                q = 0;
1089
 
                        }
1090
 
                        if(n) {
1091
 
                                BPUTLE2(&obuf, ANAME);
1092
 
                                BPUTC(&obuf, D_FILE);   /* type */
1093
 
                                BPUTC(&obuf, 1);        /* sym */
1094
 
                                BPUTC(&obuf, '<');
1095
 
                                Bwrite(&obuf, p, n);
1096
 
                                BPUTC(&obuf, 0);
1097
 
                        }
1098
 
                        p = q;
1099
 
                        if(p == 0 && op) {
1100
 
                                p = op;
1101
 
                                op = 0;
1102
 
                        }
1103
 
                }
1104
 
                g.offset = h->offset;
1105
 
 
1106
 
                BPUTLE2(&obuf, AHISTORY);
1107
 
                BPUTLE4(&obuf, h->line);
1108
 
                zaddr(&nullgen, 0);
1109
 
                zaddr(&g, 0);
1110
 
 
1111
 
                if(tofree) {
1112
 
                        free(tofree);
1113
 
                        tofree = nil;
1114
 
                }
1115
 
        }
1116
 
}
1117
 
 
1118
908
#include "../cc/lexbody"
1119
909
#include "../cc/macbody"