Commit 6a2602de authored by Russ Cox's avatar Russ Cox

cgo: can look up C identifier kind (type or value) and type

gmp.go:197:4:  type mpz_t          C type  mpz_t
gmp.go:205:2:  call mpz_init       C value func(mpz_ptr) void
gmp.go:206:2:  call mpz_set        C value func(mpz_ptr, mpz_srcptr) void
gmp.go:221:2:  call mpz_init       C value func(mpz_ptr) void
gmp.go:227:7:  call size_t         C type  size_t
gmp.go:228:2:  call mpz_export     C value func(*void, *size_t, int, size_t, int, size_t, mpz_srcptr) *void
gmp.go:235:13: call mpz_sizeinbase C value func(mpz_srcptr, int) size_t
gmp.go:241:2:  call mpz_set        C value func(mpz_ptr, mpz_srcptr) void
gmp.go:252:3:  call mpz_import     C value func(mpz_ptr, size_t, int, size_t, int, size_t, *const void) void
gmp.go:261:2:  call mpz_set_si     C value func(mpz_ptr, long int) void
gmp.go:273:5:  call mpz_set_str    C value func(mpz_ptr, *const char, int) int
gmp.go:282:9:  call mpz_get_str    C value func(*char, int, mpz_srcptr) *char
gmp.go:287:3:  call mpz_clear      C value func(mpz_ptr) void
gmp.go:302:2:  call mpz_add        C value func(mpz_ptr, mpz_srcptr, mpz_srcptr) void
gmp.go:311:2:  call mpz_sub        C value func(mpz_ptr, mpz_srcptr, mpz_srcptr) void
gmp.go:320:2:  call mpz_mul        C value func(mpz_ptr, mpz_srcptr, mpz_srcptr) void
gmp.go:329:2:  call mpz_tdiv_q     C value func(mpz_ptr, mpz_srcptr, mpz_srcptr) void
gmp.go:339:2:  call mpz_tdiv_r     C value func(mpz_ptr, mpz_srcptr, mpz_srcptr) void
gmp.go:348:2:  call mpz_mul_2exp   C value func(mpz_ptr, mpz_srcptr, long unsigned int) void
gmp.go:356:2:  call mpz_div_2exp   C value func(mpz_ptr, mpz_srcptr, long unsigned int) void
gmp.go:367:3:  call mpz_pow_ui     C value func(mpz_ptr, mpz_srcptr, long unsigned int) void
gmp.go:369:3:  call mpz_powm       C value func(mpz_ptr, mpz_srcptr, mpz_srcptr, mpz_srcptr) void
gmp.go:378:2:  call mpz_neg        C value func(mpz_ptr, mpz_srcptr) void
gmp.go:386:2:  call mpz_abs        C value func(mpz_ptr, mpz_srcptr) void
gmp.go:404:9:  call mpz_cmp        C value func(mpz_srcptr, mpz_srcptr) int
gmp.go:413:2:  call mpz_tdiv_qr    C value func(mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr) void
gmp.go:426:2:  call mpz_gcdext     C value func(mpz_ptr, mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr) void

R=r
DELTA=938  (628 added, 308 deleted, 2 changed)
OCL=34733
CL=34791
parent 92f773dc
......@@ -6,6 +6,9 @@ include $(GOROOT)/src/Make.$(GOARCH)
TARG=cgo
GOFILES=\
cgo.go\
ast.go\
gcc.go\
main.go\
util.go\
include $(GOROOT)/src/Make.cmd
This diff is collapsed.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Annotate Crefs in Prog with C types by parsing gcc debug output.
package main
import (
"debug/dwarf";
"debug/elf";
"debug/macho";
"fmt";
"os";
"strconv";
"strings";
)
func (p *Prog) loadDebugInfo() {
// Construct a slice of unique names from p.Crefs.
m := make(map[string]int);
for _, c := range p.Crefs {
m[c.Name] = -1;
}
names := make([]string, 0, len(m));
for name, _ := range m {
i := len(names);
names = names[0:i+1];
names[i] = name;
m[name] = i;
}
// Coerce gcc into telling us whether each name is
// a type, a value, or undeclared. We compile a function
// containing the line:
// name;
// If name is a type, gcc will print:
// x.c:2: warning: useless type name in empty declaration
// If name is a value, gcc will print
// x.c:2: warning: statement with no effect
// If name is undeclared, gcc will print
// x.c:2: error: 'name' undeclared (first use in this function)
// A line number directive causes the line number to
// correspond to the index in the names array.
var b strings.Buffer;
b.WriteString(p.Preamble);
b.WriteString("void f(void) {\n");
b.WriteString("#line 0 \"cgo-test\"\n");
for _, n := range names {
b.WriteString(n);
b.WriteString(";\n");
}
b.WriteString("}\n");
kind := make(map[string]string);
_, stderr := gccDebug(b.Bytes());
if stderr == "" {
fatal("gcc produced no output");
}
for _, line := range strings.Split(stderr, "\n", 0) {
if len(line) < 9 || line[0:9] != "cgo-test:" {
continue;
}
line = line[9:len(line)];
colon := strings.Index(line, ":");
if colon < 0 {
continue;
}
i, err := strconv.Atoi(line[0:colon]);
if err != nil {
continue;
}
what := "";
switch {
default:
continue;
case strings.Index(line, "warning: useless type name in empty declaration") >= 0:
what = "type";
case strings.Index(line, "warning: statement with no effect") >= 0:
what = "value";
case strings.Index(line, "undeclared") >= 0:
what = "error";
}
if old, ok := kind[names[i]]; ok && old != what {
error(noPos, "inconsistent gcc output about C.%s", names[i]);
}
kind[names[i]] = what;
}
for _, n := range names {
if _, ok := kind[n]; !ok {
error(noPos, "could not determine kind of name for C.%s", n);
}
}
// Extract the types from the DWARF section of an object
// from a well-formed C program. Gcc only generates DWARF info
// for symbols in the object file, so it is not enough to print the
// preamble and hope the symbols we care about will be there.
// Instead, emit
// typeof(names[i]) *__cgo__i;
// for each entry in names and then dereference the type we
// learn for __cgo__i.
b.Reset();
b.WriteString(p.Preamble);
for i, n := range names {
fmt.Fprintf(&b, "typeof(%s) *__cgo__%d;\n", n, i);
}
d, stderr := gccDebug(b.Bytes());
if d == nil {
fatal("gcc failed:\n%s\non input:\n%s", stderr, b.Bytes());
}
// Scan DWARF info for top-level TagVariable entries with AttrName __cgo__i.
types := make([]dwarf.Type, len(names));
r := d.Reader();
for {
e, err := r.Next();
if err != nil {
fatal("reading DWARF entry: %s", err);
}
if e == nil {
break;
}
if e.Tag != dwarf.TagVariable {
goto Continue;
}
name, _ := e.Val(dwarf.AttrName).(string);
typOff, _ := e.Val(dwarf.AttrType).(dwarf.Offset);
if name == "" || typOff == 0 {
fatal("malformed DWARF TagVariable entry");
}
if !strings.HasPrefix(name, "__cgo__") {
goto Continue;
}
typ, err := d.Type(typOff);
if err != nil {
fatal("loading DWARF type: %s", err);
}
t, ok := typ.(*dwarf.PtrType);
if !ok || t == nil {
fatal("internal error: %s has non-pointer type", name);
}
i, err := strconv.Atoi(name[7:len(name)]);
if err != nil {
fatal("malformed __cgo__ name: %s", name);
}
types[i] = t.Type;
Continue:
if e.Tag != dwarf.TagCompileUnit {
r.SkipChildren();
}
}
// Apply types to Crefs.
for _, c := range p.Crefs {
i := m[c.Name];
c.TypeName = kind[c.Name] == "type";
c.DebugType = types[i];
}
}
// gccDebug runs gcc -gdwarf-2 over the C program stdin and
// returns the corresponding DWARF data and any messages
// printed to standard error.
func gccDebug(stdin []byte) (*dwarf.Data, string) {
machine := "-m32";
if os.Getenv("GOARCH") == "amd64" {
machine = "-m64";
}
tmp := "_cgo_.o";
_, stderr, ok := run(stdin, []string{
"gcc",
machine,
"-Wall", // many warnings
"-Werror", // warnings are errors
"-o"+tmp, // write object to tmp
"-gdwarf-2", // generate DWARF v2 debugging symbols
"-c", // do not link
"-xc", // input language is C
"-", // read input from standard input
});
if !ok {
return nil, string(stderr);
}
// Try to parse f as ELF and Mach-O and hope one works.
var f interface{DWARF() (*dwarf.Data, os.Error)};
var err os.Error;
if f, err = elf.Open(tmp); err != nil {
if f, err = macho.Open(tmp); err != nil {
fatal("cannot parse gcc output %s as ELF or Mach-O object", tmp);
}
}
d, err := f.DWARF();
if err != nil {
fatal("cannot load DWARF debug information from %s: %s", tmp, err);
}
return d, "";
}
......@@ -364,7 +364,7 @@ func (z *Int) Exp(x, y, m *Int) *Int {
y.doinit();
z.doinit();
if m == nil {
C.mpz_pow(&z.i, &x.i, &y.i);
C.mpz_pow_ui(&z.i, &x.i, mpz_get_ui(&y.i));
} else {
C.mpz_powm(&z.i, &x.i, &y.i, &m.i);
}
......
// Copyright 2009 The Go Authors. All rights reserved.
// 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.
func usage() {
fmt.Fprint(os.Stderr, "usage: cgo file.cgo\n");
flag.PrintDefaults();
}
func main() {
flag.Usage = usage;
flag.Parse();
args := flag.Args();
if len(args) != 1 {
usage();
os.Exit(2);
}
p := openProg(args[0]);
p.loadDebugInfo();
tw := tabwriter.NewWriter(os.Stdout, 1, 1, ' ', 0);
for _, cref := range p.Crefs {
what := "value";
if cref.TypeName {
what = "type";
}
fmt.Fprintf(tw, "%s:\t%s %s\tC %s\t%s\n", (*cref.Expr).Pos(), cref.Context, cref.Name, what, cref.DebugType);
}
tw.Flush();
}
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes";
"exec";
"fmt";
"go/token";
"io";
"os";
)
// A ByteReaderAt implements io.ReadAt using a slice of bytes.
type ByteReaderAt []byte
func (r ByteReaderAt) ReadAt(p []byte, off int64) (n int, err os.Error) {
if off >= int64(len(r)) || off < 0 {
return 0, os.EOF;
}
return bytes.Copy(p, r[off:len(r)]), nil;
}
// run runs the command argv, feeding in stdin on standard input.
// It returns the output to standard output and standard error.
// ok indicates whether the command exited successfully.
func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
cmd, err := exec.LookPath(argv[0]);
if err != nil {
fatal("exec %s: %s", argv[0], err);
}
r0, w0, err := os.Pipe();
if err != nil {
fatal("%s", err);
}
r1, w1, err := os.Pipe();
if err != nil {
fatal("%s", err);
}
r2, w2, err := os.Pipe();
if err != nil {
fatal("%s", err);
}
pid, err := os.ForkExec(cmd, argv, os.Environ(), "", []*os.File{r0, w1, w2});
if err != nil {
fatal("%s", err);
}
r0.Close();
w1.Close();
w2.Close();
c := make(chan bool);
go func() {
w0.Write(stdin);
w0.Close();
c <- true;
}();
var xstdout []byte; // TODO(rsc): delete after 6g can take address of out parameter
go func() {
xstdout, _ = io.ReadAll(r1);
r1.Close();
c <- true;
}();
stderr, _ = io.ReadAll(r2);
r2.Close();
<-c;
<-c;
stdout = xstdout;
w, err := os.Wait(pid, 0);
if err != nil {
fatal("%s", err);
}
ok = w.Exited() && w.ExitStatus() == 0;
return;
}
// Die with an error message.
func fatal(msg string, args ...) {
fmt.Fprintf(os.Stderr, msg+"\n", args);
os.Exit(2);
}
var nerrors int
var noPos token.Position
func error(pos token.Position, msg string, args ...) {
nerrors++;
if pos.IsValid() {
fmt.Fprintf(os.Stderr, "%s: ", pos);
}
fmt.Fprintf(os.Stderr, msg, args);
fmt.Fprintf(os.Stderr, "\n");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment