mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cgo checkpoint.
can write all 3 output files and then compile them by hand. R=r DELTA=919 (841 added, 16 deleted, 62 changed) OCL=34954 CL=34973
This commit is contained in:
parent
ffe83e582e
commit
12fc217336
10 changed files with 887 additions and 70 deletions
|
|
@ -2,26 +2,28 @@
|
|||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag";
|
||||
"fmt";
|
||||
"os";
|
||||
"tabwriter";
|
||||
)
|
||||
|
||||
// Cgo; see gmp.go for an overview.
|
||||
|
||||
// TODO(rsc):
|
||||
// Emit correct line number annotations.
|
||||
// Make 6g understand the annotations.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag";
|
||||
"fmt";
|
||||
"go/ast";
|
||||
"os";
|
||||
)
|
||||
|
||||
func usage() {
|
||||
fmt.Fprint(os.Stderr, "usage: cgo file.cgo\n");
|
||||
flag.PrintDefaults();
|
||||
}
|
||||
|
||||
const ptrSize = 8 // TODO
|
||||
|
||||
func main() {
|
||||
flag.Usage = usage;
|
||||
flag.Parse();
|
||||
|
|
@ -32,15 +34,40 @@ func main() {
|
|||
os.Exit(2);
|
||||
}
|
||||
p := openProg(args[0]);
|
||||
p.loadDebugInfo();
|
||||
p.Preamble = p.Preamble + "\n" + builtinProlog;
|
||||
p.loadDebugInfo(ptrSize);
|
||||
p.Vardef = make(map[string]*Type);
|
||||
p.Funcdef = make(map[string]*FuncType);
|
||||
|
||||
tw := tabwriter.NewWriter(os.Stdout, 1, 1, ' ', 0);
|
||||
for _, cref := range p.Crefs {
|
||||
what := "value";
|
||||
if cref.TypeName {
|
||||
what = "type";
|
||||
switch cref.Context {
|
||||
case "call":
|
||||
if !cref.TypeName {
|
||||
// Is an actual function call.
|
||||
*cref.Expr = &ast.Ident{Value: "_C_" + cref.Name};
|
||||
p.Funcdef[cref.Name] = cref.FuncType;
|
||||
break;
|
||||
}
|
||||
*cref.Expr = cref.Type.Go;
|
||||
case "expr":
|
||||
if cref.TypeName {
|
||||
error((*cref.Expr).Pos(), "type C.%s used as expression", cref.Name);
|
||||
}
|
||||
// Reference to C variable.
|
||||
// We declare a pointer and arrange to have it filled in.
|
||||
*cref.Expr = &ast.StarExpr{X: &ast.Ident{Value: "_C_" + cref.Name}};
|
||||
p.Vardef[cref.Name] = cref.Type;
|
||||
case "type":
|
||||
if !cref.TypeName {
|
||||
error((*cref.Expr).Pos(), "expression C.%s used as type", cref.Name);
|
||||
}
|
||||
*cref.Expr = cref.Type.Go;
|
||||
}
|
||||
fmt.Fprintf(tw, "%s:\t%s %s\tC %s\t%s\n", (*cref.Expr).Pos(), cref.Context, cref.Name, what, cref.DebugType);
|
||||
}
|
||||
tw.Flush();
|
||||
if nerrors > 0 {
|
||||
os.Exit(2);
|
||||
}
|
||||
|
||||
p.PackagePath = p.Package;
|
||||
p.writeOutput(args[0], "_cgo_go.go", "_cgo_c.c", "_cgo_gcc.c");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue