News for hackers. Stuff that matters.
Re:Porting the Go compiler from C to Go by thinkpad20 on Friday April 25, 2014 @12:57PM (#7647636)
> There are currently 1032 goto statements in the Go compiler jumping to 241 labels.

Wow, that's really striking. I know that goto statements have their uses but for something written in the last couple of years to have over a thousand of them is very surprising (it might not be surprising at all for those who write C code all the time). I guess they're mostly just for error handlers?

Re:Porting the Go compiler from C to Go by awda on Friday April 25, 2014 @01:13PM (#7647750)
As others have pointed out, this is actually idiomatic C. You have a portion of your function below a label which is "clean up everything and return this variable (usually error status)." Then at any point where you have an error, you goto that label and everything is released and the error gets returned. It's sort of a manual 'defer' pattern.

"goto considered harmful" is taken out of context and bemoaned mostly by people unfamiliar with idiomatic C, I think. (Edit: sorry, I mean just for C. For other languages, e.g. Go, there may be more appropriate patterns like the 'defer' keyword.)

Re:Porting the Go compiler from C to Go by daeken on Friday April 25, 2014 @01:11PM (#7647739)
I'm not surprised to see them in a compiler. When you're parsing and want to treat it as a big state machine, gotos are your best friend.
Re:Porting the Go compiler from C to Go by clarry on Friday April 25, 2014 @02:44PM (#7648335)
Encoding state implicitly in control flow tends to give you nicer and more natural code. A big state machine implemented with gotos can be tough to read if it's also a parser (which may need to build a tree, recurse, etc.). A small hand-rolled lexer might be reasonable to implement with goto. Although I would prefer to enumerate the states and use a switch.

If you're writing a recursive descent parser for C while keeping minimal lookahead, I can think of a place or two where goto might really help. One is to deal with casts, compound literals and subexpressions. They all begin with a left-parenthesis, so you can't tell what you're dealing with until a bit further. Since these all appear in the parsing of simple expressions (identifiers, constants, unary operators), the code can go in one function and you can jump to the right place with goto as soon as you know what you are dealing with.

I did not look in the Go code but C programmers mostly use goto for error paths.

Re:Porting the Go compiler from C to Go by bakul on Friday April 25, 2014 @02:33PM (#7648267)
Except in languages with tail recursion such as Scheme! A state ends with a call to the next state instead of a goto next state.
Re:Porting the Go compiler from C to Go by jgrahamc on Friday April 25, 2014 @01:01PM (#7647669)
For example: http://golang.org/src/cmd/6g/gsubr.c

Some are for single error return, but others are just to make a nice structure for the code.

Re:Porting the Go compiler from C to Go by saucetenuto on Friday April 25, 2014 @01:53PM (#7648007)
I dunno, a few of them seem hard to justify. The one on line 475 could just be an "else" branch, frex, and the ones in the loop on 327 could as easily be continues.
Re:Porting the Go compiler from C to Go by __david__ on Friday April 25, 2014 @02:21PM (#7648189)
Only one goto in the loop could be a "continue", the one in the nested loop cannot. The one that could be is most likely a goto just for consistency.
Re:Porting the Go compiler from C to Go by mjibson on Friday April 25, 2014 @08:17PM (#7649889)
When the full video of the talk comes out, you will see that Russ agrees with you. He lists the types of uses of goto. Various are used to make loops or elses, and he says those should not have occurred.
Re:Porting the Go compiler from C to Go by simias on Friday April 25, 2014 @01:54PM (#7648012)
I would be curious to hear the justification behind the implementation of angryrealloc, the "goto ok" is strictly equivalent to a "continue" as far as I know. Maybe it triggers an optimization with a certain compiler.
Re:Porting the Go compiler from C to Go by chc on Friday April 25, 2014 @02:05PM (#7648093)
It's to continue the outer loop from the inner loop.
Re:Porting the Go compiler from C to Go by raverbashing on Friday April 25, 2014 @01:09PM (#7647719)
Ah the old C style of function types in a different line.

This smells of very old code (as the header testifies)

Re:Porting the Go compiler from C to Go by awda on Friday April 25, 2014 @01:15PM (#7647760)
??? This is modern BSD (and probably plan9) style. Here's a line from the header:

> Portions Copyright © 2009 The Go Authors. All rights reserved

That's pretty recent.

And here's BSD style(9): http://www.freebsd.org/cgi/man.cgi?query=style&sektion=9

    The function type should be on a line by itself preceding the function.
Re:Porting the Go compiler from C to Go by rootbear on Friday April 25, 2014 @01:40PM (#7647926)
I use that style all the time. I like having the function name start in column 1, it can make searching easier.

I do wish that in C1x (for x > 1), they could find a way to let us declare multiple formal arguments of the same type without repeating the type:

    float
    my_graphics_hack(float x, y, z, r, g, b, u, v) { ...
instead of

    float my_graphics_hack(float x, float y, float z, float r, float g, float b, float u, float v) { ...
which gets really tedious and obfuscates that fact that they are all the same type, especially with the typename is more complex than just "float". When the new syntax was added to ANSI C, it wasn't possible to do multiple variables, for reasons I've forgotten but which I think had to do with forward type references. It would be awfully nice to find a fix for this.
Re:Porting the Go compiler from C to Go by simias on Friday April 25, 2014 @02:01PM (#7648057)
That's a poor justification, there are plenty of tools to index and search C(++) code like ctags or cscope, and your example could use a vector or a struct, I have a hard time imagining when a function with such a prototype would be useful in real life.
Re:Porting the Go compiler from C to Go by clarry on Friday April 25, 2014 @02:59PM (#7648415)
Compound literals are not too pretty and not too well known. Plus, if you care about c89... So, do you wrap all your values in a struct because the prototype gets too long otherwise? What a chore.

Preferring that function names start at the first column to make searching easier is a perfectly good justification.

You, on the other hand, seemingly would impose your tools (which have their shortcomings) and workflow on people with no justification at all.

Just because a tool exists, doesn't mean it's good (or better than what people are accustomed to) or that everyone must use it.

Do you think everyone should use Vim on Linux too?

Re:Porting the Go compiler from C to Go by simias on Friday April 25, 2014 @03:36PM (#7648607)
No, I think everyone should use Emacs on FreeBSD, but that's not my point.

Adding syntactic sugar to a language makes the language bigger and harder to completely understand, it makes it easier to misuse a feature (and C already has a lot of trickery with types, like when you declare a parameter as an array but it behaves like a pointer). I'm a strong believer that implicit is better than explicit and that while there are many ways to do the same thing some are better than other in practice. Of course, "in practice" can change from one project to the other, what matters in consistency. I applaud the choice of Go to standardize on a single coding style for instance.

For the particular example of the parent, while I write a lot of C code for work and for fun I have yet to encounter a situation where I saw a function taking 10 floats as arguments and thinking "yup, that's completely the right way to do that". If you have an example of such a code I'd me more that willing to reconsider my position, otherwise we're just talking about the best way to tame a unicorn.

Re:Porting the Go compiler from C to Go by coherentpony on Friday April 25, 2014 @01:48PM (#7647971)
structs are a good way to hide away all that state
Re:Porting the Go compiler from C to Go by rootbear on Friday April 25, 2014 @01:57PM (#7648041)
Agreed, but there are still times when I end up having several arguments of the same type, and I see no virtue in having to repeat the type for each formal parameter.
Re:Porting the Go compiler from C to Go by raverbashing on Friday April 25, 2014 @02:01PM (#7648060)
I don't see how having the type makes it easier to grep, grep finds the function name regardless

You can also search for something like "\w+\s(.?)\s*{"

Re:Porting the Go compiler from C to Go by coldtea on Friday April 25, 2014 @02:11PM (#7648128)
>I don't see how having the type makes it easier to grep, grep finds the function name regardless

It's not about finding the function name in general, it's about finding where the function is actually declared -- not in the call sites.

>You can also search for something like "\w+\s(.?)\s{"*

Yeah, or use ctags. That's why they said "simpler".

"^foo" beats that.

Re:Porting the Go compiler from C to Go by raverbashing on Friday April 25, 2014 @02:30PM (#7648246)
Yeah, ctags is a good choice

> "^foo" beats that

because it's so hard to use ctags...

Re:Porting the Go compiler from C to Go by clarry on Friday April 25, 2014 @03:03PM (#7648448)
So how do you use ctags with grep?
Re:Porting the Go compiler from C to Go by coldtea on Friday April 25, 2014 @04:11PM (#7648775)
It needs setup. I'd rather just grep.
Re:Porting the Go compiler from C to Go by 4ad on Friday April 25, 2014 @01:35PM (#7647886)
BSD/Plan 9 style is extremely useful because it allows grepping for functions. More specifically, it allows the creation of acme(1) addresses of the form /some/file.c:/^foo where foo is a function. In acme you can just B3-click on that string and it will open the file at the correct function...

It is also a pretty common style in the open source world.

Re:Porting the Go compiler from C to Go by raverbashing on Friday April 25, 2014 @02:31PM (#7648254)
You can do that with ctags

> It is also a pretty common style in the open source world.

Depends, one proeminent C project doesn't use that: the linux kernel

Re:Porting the Go compiler from C to Go by copergi on Friday April 25, 2014 @06:50PM (#7649558)
>You can do that with ctags

And I can do it without ctags. That's the point.

>Depends, one proeminent C project doesn't use that: the linux kernel

That doesn't make his statement "depends" at all. A single project not using that style does not contradict it being "pretty common".

Re:Porting the Go compiler from C to Go by foxhill on Friday April 25, 2014 @01:21PM (#7647800)
this is a style i still use today (almost)

    int
    main
    (int argc, char **argv)
    {
      //body..
    }
although most people think i'm weird for it..
Re:Porting the Go compiler from C to Go by lutusp on Friday April 25, 2014 @01:29PM (#7647840)
Yep, that's pretty weird. It harks back to the early days of C. When I first saw the currently recommended form:

    int main(int argc, char **argv) {
      //body..
    }
I thought it was nice but not obviously advantageous, but over the years I've come around to thinking it makes code easier to follow. Even many bash shell scripting guides now recommend a similar form:

    for x in y; do
      # body
    done
Re:Porting the Go compiler from C to Go by foxhill on Friday April 25, 2014 @04:28PM (#7648863)
it certainly helps when your function isn't as simple as main..!
Re:Porting the Go compiler from C to Go by SixSigma on Friday April 25, 2014 @04:38PM (#7648912)
it is so you can type

    grep -n ^functionname *.c 
and get the line that starts the definition
Re:Porting the Go compiler from C to Go by rafekett on Friday April 25, 2014 @01:16PM (#7647765)
which line?
Re:Porting the Go compiler from C to Go by copergi on Friday April 25, 2014 @06:48PM (#7649551)
Are you sure you aren't confusing it with K&R style with the argument names on a different line? That would indicate old code. Having the return type on a different line is incredibly common, virtually every C project with decent code uses it.
Re:Porting the Go compiler from C to Go by coldtea on Friday April 25, 2014 @02:06PM (#7648098)
>but for something written in the last couple of years to have over a thousand of them is very surprising

Hardly surprising. Gotos will be found by the hundrends or thousands in C projects like compilers, kernels etc.

It's used for localized consolidated error handling, but also for stuff like parsing code (bison generates tons of gotos IIRC), state machines, etc.

If anything it's the old "Goto statement considered harmful" that's a little too naive.

Re:Porting the Go compiler from C to Go by copergi on Friday April 25, 2014 @06:51PM (#7649570)
>If anything it's the old "Goto statement considered harmful" that's a little too naive.

No, people who didn't read it and just repeat the title without knowing what he was talking about may be too naive, but the original point was not.

Re:Porting the Go compiler from C to Go by azernik on Friday April 25, 2014 @01:00PM (#7647661)
Yeah, there are a lot of them in e.g. Linux kernel code for jumping to end-of-function cleanup. I suspect that in the process of translation to Go most will be replaced by the defer keyword.
Re:Porting the Go compiler from C to Go by im3w1l on Friday April 25, 2014 @01:17PM (#7647770)
I saw one goto that could cleanly be replaced with an else (goto fp).

I don't think the authors were very goto-averse.