Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package gc |
| 6 | |
| 7 | import "fmt" |
| 8 | |
| 9 | // case OADD: |
| 10 | // if(n->right->op == OLITERAL) { |
| 11 | // v = n->right->vconst; |
| 12 | // naddr(n->left, a, canemitcode); |
| 13 | // } else |
| 14 | // if(n->left->op == OLITERAL) { |
| 15 | // v = n->left->vconst; |
| 16 | // naddr(n->right, a, canemitcode); |
| 17 | // } else |
| 18 | // goto bad; |
| 19 | // a->offset += v; |
| 20 | // break; |
| 21 | |
| 22 | /* |
| 23 | * a function named init is a special case. |
| 24 | * it is called by the initialization before |
| 25 | * main is run. to make it unique within a |
| 26 | * package and also uncallable, the name, |
Russ Cox | f6791da | 2015-02-20 13:54:45 -0500 | [diff] [blame] | 27 | * normally "pkg.init", is altered to "pkg.init.1". |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | var renameinit_initgen int |
| 31 | |
| 32 | func renameinit() *Sym { |
| 33 | renameinit_initgen++ |
Russ Cox | f6791da | 2015-02-20 13:54:45 -0500 | [diff] [blame] | 34 | namebuf = fmt.Sprintf("init.%d", renameinit_initgen) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 35 | return Lookup(namebuf) |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * hand-craft the following initialization code |
| 40 | * var initdone· uint8 (1) |
| 41 | * func init() (2) |
| 42 | * if initdone· != 0 { (3) |
| 43 | * if initdone· == 2 (4) |
| 44 | * return |
| 45 | * throw(); (5) |
| 46 | * } |
| 47 | * initdone· = 1; (6) |
| 48 | * // over all matching imported symbols |
| 49 | * <pkg>.init() (7) |
| 50 | * { <init stmts> } (8) |
Russ Cox | f6791da | 2015-02-20 13:54:45 -0500 | [diff] [blame] | 51 | * init.<n>() // if any (9) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 52 | * initdone· = 2; (10) |
| 53 | * return (11) |
| 54 | * } |
| 55 | */ |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 56 | func anyinit(n *NodeList) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 57 | // are there any interesting init statements |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 58 | for l := n; l != nil; l = l.Next { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 59 | switch l.N.Op { |
| 60 | case ODCLFUNC, |
| 61 | ODCLCONST, |
| 62 | ODCLTYPE, |
| 63 | OEMPTY: |
| 64 | break |
| 65 | |
| 66 | case OAS: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 67 | if isblank(l.N.Left) && candiscard(l.N.Right) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 68 | break |
| 69 | } |
| 70 | fallthrough |
| 71 | |
| 72 | // fall through |
| 73 | default: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 74 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | // is this main |
| 79 | if localpkg.Name == "main" { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 80 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // is there an explicit init function |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 84 | s := Lookup("init.1") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 85 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 86 | if s.Def != nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 87 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // are there any imported init functions |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 91 | for h := uint32(0); h < NHASH; h++ { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 92 | for s = hash[h]; s != nil; s = s.Link { |
| 93 | if s.Name[0] != 'i' || s.Name != "init" { |
| 94 | continue |
| 95 | } |
| 96 | if s.Def == nil { |
| 97 | continue |
| 98 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 99 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | // then none |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 104 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | func fninit(n *NodeList) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 108 | if Debug['A'] != 0 { |
| 109 | // sys.go or unsafe.go during compiler build |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | n = initfix(n) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 114 | if !anyinit(n) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 115 | return |
| 116 | } |
| 117 | |
Russ Cox | 175929b | 2015-03-02 14:22:05 -0500 | [diff] [blame^] | 118 | var r *NodeList |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 119 | |
| 120 | // (1) |
Josh Bleecher Snyder | 05ca0f3 | 2015-02-28 20:31:32 +0000 | [diff] [blame] | 121 | namebuf = "initdone·" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 122 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 123 | gatevar := newname(Lookup(namebuf)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 124 | addvar(gatevar, Types[TUINT8], PEXTERN) |
| 125 | |
| 126 | // (2) |
| 127 | Maxarg = 0 |
| 128 | |
Josh Bleecher Snyder | 05ca0f3 | 2015-02-28 20:31:32 +0000 | [diff] [blame] | 129 | namebuf = "init" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 130 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 131 | fn := Nod(ODCLFUNC, nil, nil) |
| 132 | initsym := Lookup(namebuf) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 133 | fn.Nname = newname(initsym) |
| 134 | fn.Nname.Defn = fn |
| 135 | fn.Nname.Ntype = Nod(OTFUNC, nil, nil) |
| 136 | declare(fn.Nname, PFUNC) |
| 137 | funchdr(fn) |
| 138 | |
| 139 | // (3) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 140 | a := Nod(OIF, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 141 | |
| 142 | a.Ntest = Nod(ONE, gatevar, Nodintconst(0)) |
| 143 | r = list(r, a) |
| 144 | |
| 145 | // (4) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 146 | b := Nod(OIF, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 147 | |
| 148 | b.Ntest = Nod(OEQ, gatevar, Nodintconst(2)) |
| 149 | b.Nbody = list1(Nod(ORETURN, nil, nil)) |
| 150 | a.Nbody = list1(b) |
| 151 | |
| 152 | // (5) |
| 153 | b = syslook("throwinit", 0) |
| 154 | |
| 155 | b = Nod(OCALL, b, nil) |
| 156 | a.Nbody = list(a.Nbody, b) |
| 157 | |
| 158 | // (6) |
| 159 | a = Nod(OAS, gatevar, Nodintconst(1)) |
| 160 | |
| 161 | r = list(r, a) |
| 162 | |
| 163 | // (7) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 164 | var s *Sym |
| 165 | for h := uint32(0); h < NHASH; h++ { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 166 | for s = hash[h]; s != nil; s = s.Link { |
| 167 | if s.Name[0] != 'i' || s.Name != "init" { |
| 168 | continue |
| 169 | } |
| 170 | if s.Def == nil { |
| 171 | continue |
| 172 | } |
| 173 | if s == initsym { |
| 174 | continue |
| 175 | } |
| 176 | |
| 177 | // could check that it is fn of no args/returns |
| 178 | a = Nod(OCALL, s.Def, nil) |
| 179 | |
| 180 | r = list(r, a) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // (8) |
| 185 | r = concat(r, n) |
| 186 | |
| 187 | // (9) |
| 188 | // could check that it is fn of no args/returns |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 189 | for i := 1; ; i++ { |
Russ Cox | f6791da | 2015-02-20 13:54:45 -0500 | [diff] [blame] | 190 | namebuf = fmt.Sprintf("init.%d", i) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 191 | s = Lookup(namebuf) |
| 192 | if s.Def == nil { |
| 193 | break |
| 194 | } |
| 195 | a = Nod(OCALL, s.Def, nil) |
| 196 | r = list(r, a) |
| 197 | } |
| 198 | |
| 199 | // (10) |
| 200 | a = Nod(OAS, gatevar, Nodintconst(2)) |
| 201 | |
| 202 | r = list(r, a) |
| 203 | |
| 204 | // (11) |
| 205 | a = Nod(ORETURN, nil, nil) |
| 206 | |
| 207 | r = list(r, a) |
| 208 | exportsym(fn.Nname) |
| 209 | |
| 210 | fn.Nbody = r |
| 211 | funcbody(fn) |
| 212 | |
| 213 | Curfn = fn |
| 214 | typecheck(&fn, Etop) |
| 215 | typechecklist(r, Etop) |
| 216 | Curfn = nil |
| 217 | funccompile(fn) |
| 218 | } |