<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Airs - Ian Lance Taylor &#187; Programming</title>
	<atom:link href="http://www.airs.com/blog/archives/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.airs.com/blog</link>
	<description>Ian Lance Taylor</description>
	<lastBuildDate>Tue, 09 Mar 2010 07:31:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Signed or Unsigned</title>
		<link>http://www.airs.com/blog/archives/327</link>
		<comments>http://www.airs.com/blog/archives/327#comments</comments>
		<pubDate>Wed, 03 Mar 2010 14:15:54 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=327</guid>
		<description><![CDATA[C has always permitted comparisons between any integer type, and C++ follows its lead.  Comparing signed types to signed types is straightforward: you sign extend the smaller type.  Likewise, when comparing unsigned types to unsigned types, you zero extend.  When comparing signed and unsigned types, the rules are less clear.
The C standard [...]]]></description>
			<content:encoded><![CDATA[<p>C has always permitted comparisons between any integer type, and C++ follows its lead.  Comparing signed types to signed types is straightforward: you sign extend the smaller type.  Likewise, when comparing unsigned types to unsigned types, you zero extend.  When comparing signed and unsigned types, the rules are less clear.</p>
<p>The C standard specifies a type ordering: long long > long > int > short > char.  If the unsigned type appears in that ordering before the signed type, then the signed value is converted to the unsigned type.  Note that this happens even if the types are the same size (e.g., either long long and long or long and int are often the same size).  Otherwise, if the signed type is larger than the unsigned type, in the sense of having more bits, then the unsigned value is converted to the signed type.  Otherwise both values are converted to the unsigned type which corresponds to the signed type.</p>
<p>Pre-standard K&#038;R C used a different rule, but that is old enough now that we no longer have to worry about it.</p>
<p>What this rule means is that if you write portable code, such that you don&#8217;t know the sizes of types, you can not predict whether the comparison will be done as a signed comparison or an unsigned comparison.  Therefore, the gcc compiler has an option <code>-Wsign-compare</code>.  However, this option is sufficiently awkward to avoid that it is not part of <code>-Wall</code>, though it is part of <code>-Wextra</code> (the difference between <code>-Wall</code> and <code>-Wextra</code> is that the former gives warnings for which false positives are easy to avoid through simple code changes; the latter gives warnings which are generally useful but for which false positives are harder to avoid).</p>
<p>There are good reasons to use signed types: they don&#8217;t have odd behaviour around zero, so you can write <code>i < limit - 1</code> without worrying about the case </code><code>limit == 0</code>.  There are good reasons to use unsigned types for things like the number of elements in a container: you get the full range of sizes, rather than limiting yourself to only the positive half.  In particular, the C++ standard containers use unsigned types as their size.  Combining these two rules gets you in trouble with portable code.  The only reasonable answer I can see for portable code is to use <code>-Wsign-compare</code> and work around the many false positive warnings.</p>
<p>Go avoids these problems in two ways.  First, there are no implicit conversions, so you can never be surprised by having a comparison become unsigned when you expected signed.  You have to explicitly say which type of conversion you mean.  Second, Go intentionally discards half of memory, and takes the philosophy that if you want a container which can hold more values than fit in a signed int, you should write a special purpose large container.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/327/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Thread Sanitizer</title>
		<link>http://www.airs.com/blog/archives/321</link>
		<comments>http://www.airs.com/blog/archives/321#comments</comments>
		<pubDate>Sat, 13 Feb 2010 02:36:51 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=321</guid>
		<description><![CDATA[I recently ran the gold linker under Thread Sanitizer.  It&#8217;s a nice plugin for Valgrind which looks for race conditions in multi-threaded programs.  To describe it briefly, it builds Happens-Before relationships based on mutex operations and warns when it notices a write and a read/write to the same memory location without a Happens-Before [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran the gold linker under <a href="http://code.google.com/p/data-race-test/wiki/ThreadSanitizer">Thread Sanitizer</a>.  It&#8217;s a nice plugin for Valgrind which looks for race conditions in multi-threaded programs.  To describe it briefly, it builds Happens-Before relationships based on mutex operations and warns when it notices a write and a read/write to the same memory location without a Happens-Before relationship.  This approach can yield false positives to be sure, but it does a very nice job of identifying real problems.</p>
<p>It was able to identify one real bug in gold, one problem that led to less efficient link time, and several cases where several threads would set a shared memory location to the same value.  The latter cases are not a problem on x86 architectures, though they could be on other processors.  In order to get clean Thread Sanitizer results in the future, I fixed all of the cases so that I could get a clean run of gold, at least with the default settings.</p>
<p>The real bug that it found was a typical multi-threaded bug: the code looked fine, but it had a well-hidden error.  Gold uses a workqueue of tasks to execute, with a pool of worker threads.  Many of the tasks are run using a blocker token.  The blocker token is set to the number of tasks that precede it.  As each task completes, it decrements the blocker count.  When the count goes to zero, the next set of tasks can start.  This is a simple way to parallelize linker operations, in which one set of operations (e.g., process the symbol tables) must be run before the next set (e.g., process the relocations) can begin.  Naturally I paid close attention to the blocker behaviour when a task completed, and there were no problems there.  The problem arose in setting the blocker count when the tasks started.  The code was doing a loop of &#8220;increment the blocker count&#8221; and then &#8220;queue the task.&#8221;  What I forgot was that the process of queuing the task actually lets another thread in the pool start working on it immediately.  When the task completed, it decremented the blocker count with a lock.  But if the task completed fast enough, the initial code was still running the loop queuing up new tasks, and thus incrementing the blocker count.  I didn&#8217;t think that I needed to lock the increment, since I wasn&#8217;t expecting any task to actually complete before I started all of the tasks.  A dumb mistake&mdash;just the kind of mistake one makes in multi-threaded programming.</p>
<p>Gold is written in C++.  In Go I would of course have each task communicate its completion on a channel.  The locking would be handled by the runtime, and there would be no chance for me to make the same sort of error.  If you write multi-threaded code, and you can&#8217;t use Go, you should definitely check out Thread Sanitizer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/321/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Go Linkage Names</title>
		<link>http://www.airs.com/blog/archives/309</link>
		<comments>http://www.airs.com/blog/archives/309#comments</comments>
		<pubDate>Wed, 27 Jan 2010 14:29:40 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=309</guid>
		<description><![CDATA[All Go code lives in a package.  Every Go source file starts with a package declaration which names the package that it lives in.  A package name is a simple identifier; besides appearing in a package clause, package names are also used when referring to names imported from another package.  That poses [...]]]></description>
			<content:encoded><![CDATA[<p>All Go code lives in a package.  Every Go source file starts with a <code>package</code> declaration which names the package that it lives in.  A package name is a simple identifier; besides appearing in a <code>package</code> clause, package names are also used when referring to names imported from another package.  That poses the problem of what to do when one program links together two different packages which use the same package name.  We can&#8217;t expect the author of a large program to  be aware of every package that the program uses.  However, since Go compiles straight to object files, it&#8217;s natural to use the package name in the generated symbol names.  How can we avoid multiple definition errors?</p>
<p>The gc compiler comes with its own Go specific linker.  That linker now supports automatic symbol renaming at link time based on the name used to import the package.  That name is presumed to be unique.  This means that all imports of the same package must use the same name to import it; otherwise you might get multiple definitions of a global variable in the package.  In the future there may be some need to adjust packages which are distributed without their source code, to ensure that they don&#8217;t accidentally alias locally compiled package names.</p>
<p>For the gccgo compiler I have so far avoided using a specific linker, or rather linker wrapper.  For large programs gccgo now requires a new option, <code>-fgo-prefix=PREFIX</code> to be used when compiling a package.  The <code>PREFIX</code> should be a string unique to that package; for example, in a typical installation, it could be the directory where the package is installed.  This gives a unique name used in the compiled code.  If the <code>-fgo-prefix</code> option is not used, everything will still work as long as there are not, in fact, two packages with the same name.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/309/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Protected Symbols</title>
		<link>http://www.airs.com/blog/archives/307</link>
		<comments>http://www.airs.com/blog/archives/307#comments</comments>
		<pubDate>Fri, 22 Jan 2010 05:41:52 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=307</guid>
		<description><![CDATA[Now for something really controversial: what&#8217;s wrong with protected symbols?
In an ELF shared library, an ordinary global symbol may be overridden if a symbol of the same name is defined in the executable or in a shared library which appears earlier in the runtime search path.  This is called symbol interposition.  It is [...]]]></description>
			<content:encoded><![CDATA[<p>Now for something really controversial: what&#8217;s wrong with protected symbols?</p>
<p>In an ELF shared library, an ordinary global symbol may be overridden if a symbol of the same name is defined in the executable or in a shared library which appears earlier in the runtime search path.  This is called symbol interposition.  It is often used with functions such as <code>malloc</code>.  A shared library can define <code>malloc</code> and it can have code which calls <code>malloc</code>.  If the executable linked with the shared library defines <code>malloc</code> itself, then the version in the executable will be used rather than the version in the shared library.  This permits the executable to control the memory allocation done by the shared library, perhaps for debugging or logging purposes.  In this regard, shared libraries act much as static archives do.</p>
<p>This has a few consequences.  One of them is that within a shared library, all references to a global symbol must use the GOT and PLT, to make the overriding possible.  That means that all function calls and variable accesses are slightly slower.  Also, some compiler optimizations are forbidden: the compiler can not inline a call to a global symbol, since that symbol might be overridden at run time.</p>
<p>When building a shared library, you can provide a version script which indicates that some symbols are actually not global.  That can eliminate the GOT and PLT accesses, but it does not permit the compiler optimizations, and you do have to write that version script and keep it up to date.</p>
<p>When compiling code that goes into a shared library, you can set the visibility of symbols.  You can use hidden visibility, which means that the symbol is not visible outside the shared library.  You can use internal visibility, which is a lot like hidden&mdash;I&#8217;ll skip the difference here.  Or you can use protected visibility.  Protected visibility means that the symbol is visible outside of the shared library, and can be accessed as usual.  However, all references from within the shared library will use the definition in the shared library.  In other words, the symbol acts more or less as usual, but it can not be overridden.  This means that accesses to the symbol avoid the GOT and PLT, and it permits compiler optimizations.</p>
<p>So, what&#8217;s wrong with them?  It turns out that protected symbols are slower at dynamic link time, which means that programs which use the shared library start up slower.  This happens because of the C rule that two pointers to the same function must compare as equal.  Since protected symbols are globally visible, you can get a pointer to a protected function in the main executable.  You can also get a pointer to that same function in the shared library, of course.  Those pointers have to be equal, or the C rule will break.</p>
<p>As noted, the access to the function in the shared library will not use the GOT or PLT.  The access in the main executable obviously will use the PLT.  How can we make those function pointers equal?  We can&#8217;t.  The executable will have a direct reference to the PLT.  The shared library will have a direct reference to the function itself.  In neither case will there be a relocation for the reference.  So there is no way to make the results equal.  (This can work for some targets, but not for ones with simple function references like the x86 targets.)</p>
<p>So, I must have lied.  The lie was that there is a case where you need to use the GOT for a protected symbol: when compiling position independent code for a shared library, and taking the address of a protected function, you need to use the GOT.  Unfortunately, gcc for the x86_64 target, surely the most widely used gcc target today, gets this wrong: <a href="http://gcc.gnu.org/PR19520">http://gcc.gnu.org/PR19520</a>.  This generally reveals itself as an error report when you go to create a shared library: <code>relocation R_X86_64_PC32 against protected symbol `NAME' can not be used when making a shared object</code>.</p>
<p>In any case, when the compiler gets it right, the dynamic linker has to fill in that GOT entry.  In order to make the function pointers compare as equal, it has to fill in the entry with the address of the PLT in the executable (or the earlier shared library).  But remember, this is a protected symbol, and protected symbols don&#8217;t support symbol interposition.  So the dynamic linker must only use the PLT of the executable if the reference in the executable refers to the definition in the shared library.  That means that when the dynamic linker sees a reloc against a protected symbol in a shared library, it has to do another walk through the executable and earlier shared libraries to see if any of them have a definition for the symbol, in which case the GOT entry must <i>not</i> be set to that earlier PLT entry but must instead be set to the address of the symbol in the shared library itself.  This check has to be done for every symbol in the shared library.</p>
<p>Those extra symbol resolution passes means a slow down for every program which uses the shared library, and that is what is wrong with protected symbols.</p>
<p>So how do you get the compiler and linker speedups available by avoiding symbol interpositioning?  Unfortunately, you have to give your symbols hidden visibility, which means that they can not be accessed from other modules.  Assuming you do want them to be accessed, you need to define symbol aliases for the ones which should be publicly visible.  That means that you need to use different names for the hidden symbols.  This is awkward at best.  Unfortunately I have nothing better to offer.  ELF is designed to support symbol interpositioning, and there is no very good way to avoid that without causing other consequences.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/307/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Version Scripts</title>
		<link>http://www.airs.com/blog/archives/300</link>
		<comments>http://www.airs.com/blog/archives/300#comments</comments>
		<pubDate>Wed, 13 Jan 2010 01:20:34 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=300</guid>
		<description><![CDATA[I recently spent some time sorting through linker version script issues, so I&#8217;m going to document what I discovered.
Linker symbol versioning was invented at Sun.  The Solaris linker lets you use a version script when you create a shared library.  This script assigns versions to specific named symbols, and  defines a version [...]]]></description>
			<content:encoded><![CDATA[<p>I recently spent some time sorting through linker version script issues, so I&#8217;m going to document what I discovered.</p>
<p>Linker symbol versioning was invented at Sun.  The Solaris linker lets you use a version script when you create a shared library.  This script assigns versions to specific named symbols, and  defines a version hierarchy.  When an executable is linked against the shared library, the versions that it uses are recorded in the executable.  If you later try to dynamically link the executable with a shared library which does not provide the required versions, you get a sensible error message.</p>
<p>Sun&#8217;s scheme (as I understand it) only permits you to add new versions and new symbols.  Once a symbol has been defined at a specific version, you can not change that in later releases.  if you change the behaviour of a symbol, you don&#8217;t change the version of the symbol itself, instead you add a new version to the library even if it does not define any symbols.  That is sufficient to ensure that an executable will not be dynamically linked against a version of the shared library which is too old.</p>
<p>Eric Youngdale and Ulrich Drepper introduced a more sophisticated symbol versioning scheme in the GNU linker and the GNU/Linux dynamic linker.  The GNU linker permits symbols to have multiple versions, of which only one is the default.  These versions are specified in the object files linked together to form the shared library.  The assembler <code>.symver</code> directive is used to assign a version to a symbol (the version is simply encoded in the name of the symbol).  This scheme permits using symbol versioning to actually change the behaviour of a symbol; older executables will continue to use the old version.  This also permits deleting symbols, by removing the default version.  The older versions of the symbol remain but are inaccessible.</p>
<p>That is all fine.  The problems come in with the extensions to the version script language.  First, the GNU linker permits wildcards in version scripts.  Second, the GNU linker permits symbols to match against demangled names, again typically using wildcards.  Third, the GNU linker permits the version script to hide symbols which have explicit versions in input object files.</p>
<p>Every symbol can only have one version.  When the linker asks for the version of a symbol, there can only be one answer.  The support for wildcards and matching of demangled names in the GNU linker script means that there may not be a unique answer for the version to use for a given name.  The fact that the GNU linker permits version scripts to hide symbols with explicit versions means that in some cases you absolutely must list a symbol two times in a version script (because you might have a <code>local: *;</code> entry which must not match your symbol with an old version).  This potential confusion means that using linker scripts correctly with wildcards requires a clear understanding of exactly how the linker parses a version script.</p>
<p>Unfortunately, this was never documented.  Until now.  Here are the rules which the GNU linker uses to parse version scripts, as of 2010-01-11.</p>
<p>The GNU linker walks through the version tags in the order in which they appear in the version script.  For each tag, it first walks through the global patterns for that tag, then the local patterns.  When looking at a single pattern, it first applies any language specific demangling as specified for the pattern, and then matches the resulting symbol name to the pattern.  If it finds an exact match for a literal pattern (a pattern enclosed in quotes or with no wildcard characters), then that is the match that it uses.  If finds a match with a wildcard pattern, then it saves it and continues searching.  Wildcard patterns that are exactly &#8220;*&#8221; are saved separately.</p>
<p>If no exact match with a literal pattern is ever found, then if a wildcard match with a global pattern was found it is used, otherwise if a wildcard match with a local pattern was found it is used.</p>
<p>This is the result:</p>
<ul>
<li>If there is an exact match, then we use the first tag in the version script where it matches.
<ul>
<li>If the exact match in that tag is global, it is used.</li>
<li>Otherwise the exact match in that tag is local, and is used.</li>
</ul>
</li>
<li>Otherwise, if there is any match with a global wildcard pattern:
<ul>
<li>If there is any match with a wildcard pattern which is not &#8220;*&#8221;, then we use the tag in which the <i>last</i> such pattern appears.
</li>
<li>Otherwise, we matched &#8220;*&#8221;.  If there is no match with a local wildcard pattern which is not &#8220;*&#8221;, then we use the <i>last</i> match with a global &#8220;*&#8221;.  Otherwise, continue.
</li>
</ul>
</li>
<li>Otherwise, if there is any match with a local wildcard pattern:
<ul>
<li>If there is any match with a wildcard pattern which is not &#8220;*&#8221;, then we use the tag in which the <i>last</i> such pattern appears.
</li>
<li>Otherwise, we matched &#8220;*&#8221;, and we use the tag in which the <i>last</i> such match occurred.
</li>
</ul>
</li>
</ul>
<p>As mentioned above, there is an additional wrinkle.  When the GNU linker finds a symbol with a version defined in an object file due to a <code>.symver</code> directive, it looks up that symbol name in that version tag.  If it finds it, it matches the symbol name against the patterns for that version.  If there is no match with a global pattern, but there is a match with a local pattern, then the GNU linker marks the symbol as local.</p>
<p>I want gold to be compatible, but I also want gold to be efficient.  I&#8217;ve introduced a hash table in gold to do fast lookups for exact matches.  That makes it impossible for gold to follow the exact rules when matching demangled names.  Currently gold does not do the final lookup to see if a symbol with an explicit version should be forced local; I don&#8217;t understand why that is useful.  It is possible that I will be forced to add that to gold at some later date.</p>
<p>Here are the current rules for gold:</p>
<ul>
<li>If there is an exact match for the mangled name, we use it.</p>
<ul>
<li>If there is more than one exact match, we give a warning, and we use the first tag in the script which matches.
</li>
<li>If a symbol has an exact match as both global and local for the same version tag, we give an error.
</li>
</ul>
</li>
<li>Otherwise, we look for an extern C++ or an extern Java exact match.  If we find an exact match, we use it.
<ul>
<li>If there is more than one exact match, we give a warning, and we use the first tag in the script which matches.
</li>
<li>If a symbol has an exact match as both global and local for the same version tag, we give an error.
</li>
</ul>
</li>
<li>Otherwise, we look through the wildcard patterns, ignoring &#8220;*&#8221; patterns.  We look through the version tags in reverse order.  For each version tag, we look through the global patterns and then the local patterns.  We use the first match we find (i.e., the <i>last</i> matching version tag in the file).
</li>
<li>Otherwise, we use the &#8220;*&#8221; pattern if there is one.  We give a warning if there are multiple &#8220;*&#8221; patterns.
</li>
</ul>
<p>I hope for your sake that this information never actually matters to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/300/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cargo Cult Programming</title>
		<link>http://www.airs.com/blog/archives/294</link>
		<comments>http://www.airs.com/blog/archives/294#comments</comments>
		<pubDate>Sat, 09 Jan 2010 05:35:29 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=294</guid>
		<description><![CDATA[I recently encountered a nice example of cargo cult programming.  In bug 10980 Robert Wohlrab helpfully built  a large number of Debian packages with the gold linker and reported errors about unknown options.  These were options supported by the GNU linker but not by gold.  (I&#8217;ve now added all the options [...]]]></description>
			<content:encoded><![CDATA[<p>I recently encountered a nice example of cargo cult programming.  In <a href="http://sourceware.org/bugzilla/show_bug.cgi?id=10980">bug 10980</a> Robert Wohlrab helpfully built  a large number of Debian packages with the gold linker and reported errors about unknown options.  These were options supported by the GNU linker but not by gold.  (I&#8217;ve now added all the options to gold).</p>
<p>Among the options that packages used were <code>-g</code> and <code>-assert</code>.  The GNU linker accepts and ignores these options.  It has never done anything with them.  Why do people pass them to the linker?  I can only assume that they were copied from some other linker invocation.</p>
<p>In today&#8217;s increasingly complex world of programming, when so much code involves integrating libraries in various ways, I expect that cargo cult programming is on the rise.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/294/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Generics</title>
		<link>http://www.airs.com/blog/archives/291</link>
		<comments>http://www.airs.com/blog/archives/291#comments</comments>
		<pubDate>Wed, 06 Jan 2010 14:18:01 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=291</guid>
		<description><![CDATA[The goal of generics in a programming language is to save programmer time.  Generics permit you to write code that works with multiple types only once.  They also permit one programmer to write generic algorithms which you can use with your program&#8217;s types.  This kind of thing is attractive to anybody who [...]]]></description>
			<content:encoded><![CDATA[<p>The goal of generics in a programming language is to save programmer time.  Generics permit you to write code that works with multiple types only once.  They also permit one programmer to write generic algorithms which you can use with your program&#8217;s types.  This kind of thing is attractive to anybody who has written a lot of C code, where you find yourself writing the same linked list and hash table code over and over again.  The core code will use <code>void *</code> pointers but for everything else you need type casts.  Those type casts are mechanical and tedious.</p>
<p>I would say that the most significant advantage of C++ over C is the C++ mechanism for generic programming, which is templates.  C++ has a powerful template system which is somewhat like a macro system built into the language proper.  It&#8217;s not really a macro system&mdash;in a true macro system the code would be parsed only at template instantiation time, but in fact C++ code is parsed at template definition time&mdash;but I&#8217;ve discovered that people who are not deeply familiar with the language think of it that way, and it is an effective approach.  C++ pays for this power in compilation time, as each template must be separately compiled for each type for which it is instantiated.  It also pays in language complexity, which is most visible to users in the widely noted incomprehensible error messages.  For a while the new C++ standard introduced the idea of concepts which would at least improve the error messages, but concepts themselves turned out to be very complex and were dropped from the standardization process for now.</p>
<p>Other languages have other approaches to generics.  I know these languages less well, so I hope I don&#8217;t make any mistakes.  Java restricts generics to types which are inherited from Object, which permits the templates to be instantiated a single time; a cost of Java&#8217;s approach is that the actual types are lost (this is known as <i>type erasure</i>).  C# instantiates generics at runtime using a JIT; all types which are representable as pointers can share a single instantiation.  ML has two different generics implementations: simple polymorphism which can be used for, e.g., type-safe containers, and a powerful system in which you can write a transformation which takes a set of types, variables, and functions and produces a different set; the latter system is pretty much as powerful as C++ templates but does not need to be compiled each time (I think) and produces comprehensible error messages.  LISP has an extremely powerful macro system which simply rewrites the program, something which is relatively easy to do in LISP due to the simple syntax; this is so powerful that it lets you add new syntactic constructs to your program.</p>
<p>I&#8217;ve been thinking about generics because of Go.  Go&#8217;s goals include fast compilation and efficient execution.  Those goals are in conflict when it comes to generics.  Go is not interpreted, and it is not the case that every type looks like a pointer.  Therefore efficient execution suggests separate compilation for each instantiation, which of course slows down compilation time.  ML&#8217;s approach is quite powerful, but it may be complex to use in simple cases, and it&#8217;s hard to understand how to implement it for Go.  Relying on every type matching the empty interface doesn&#8217;t help when somebody wants to write a generic function for <code>chan T</code>, since <code>chan int</code> is not the same as <code>chan interface{}</code>.</p>
<p>Implementing generics in Go looks like a struggle among the goals of fast compilation time (i.e., avoid multiple compilation of generics), easy programming (i.e., have some form of generics), and fast execution (i.e., different types are represented differently).  All languages have those goals in different degrees, and all languages seem to compromise on one of them when it comes to implementing generics.  The question for Go is whether we can find an appropriate compromise.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/291/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>A Gcc Frontend</title>
		<link>http://www.airs.com/blog/archives/287</link>
		<comments>http://www.airs.com/blog/archives/287#comments</comments>
		<pubDate>Wed, 09 Dec 2009 14:29:44 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=287</guid>
		<description><![CDATA[When writing the gccgo frontend I had to figure out how to write a new gcc frontend.  This is a largely undocumented procedure.   Unfortunately, I did not take notes as I went along.  However, here are some retrospective comments.
Every gcc frontend needs a set of language hooks.  This is done [...]]]></description>
			<content:encoded><![CDATA[<p>When writing the gccgo frontend I had to figure out how to write a new gcc frontend.  This is a largely undocumented procedure.   Unfortunately, I did not take notes as I went along.  However, here are some retrospective comments.</p>
<p>Every gcc frontend needs a set of language hooks.  This is done by including <code>"langhooks-def.h"</code> and writing <code>struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;</code>.  The specific language hooks are defined via various <code>LANG_HOOKS_xxx</code> macros.</p>
<p>Some language hooks are required even though for a language like Go there is nothing for them to do: <code>LANG_HOOKS_GLOBAL_BINDINGS_P</code>, <code>LANG_HOOKS_PUSHDECL</code>, <code>LANG_HOOKS_GETDECLS</code>.  Also <code>LANG_HOOKS_TYPE_FOR_MODE</code> and <code>LANG_HOOKS_TYPE_FOR_SIZE</code> must be defined and must do something reasonable.  The <code>LANG_HOOKS_INIT</code> function must call some functions: <code>build_common_tree_nodes</code>, <code>set_sizetype</code>, <code>build_common_tree_nodes_2</code>, <code>build_common_builtin_nodes</code>.  It must also set the global variable <code>void_list_node</code>.  As far as I know all these steps are required and none of them are documented.  The <code>LANG_HOOKS_POST_OPTIONS</code> hook must set <code>flag_excess_precision_cmdline</code>.</p>
<p>The main language hook is <code>LANG_HOOKS_PARSE_FILE</code>.  It will find the input file names in the global variables <code>in_fnames</code> and <code>num_in_fnames</code>.  At that point the frontend can take over and do the actual parsing and initial compilation.</p>
<p>After <code>LANG_HOOKS_PARSE_FILE</code> creates a complete parse tree (in a global variable) and returns, the rest of the work is done by <code>LANG_HOOKS_WRITE_GLOBALS</code>.  The gccgo frontend generates GENERIC, although these days it could be modified to generate GIMPLE instead.  This basically means creating appropriate <code>DECL</code> nodes for all the global types, variables, and functions.  For a function, the frontend must create a <code>cfun</code> structure via <code>push_struct_function</code> or similar, and it must set <code>current_function_decl</code>.  The frontend must not only create the <code>FUNCTION_DECL</code>, it must fill in the <code>DECL_RESULT</code> field with a <code>RESULT_DECL</code>.  After creating the GENERIC or GIMPLE, it should call <code>cgraph_finalize_function</code>.</p>
<p>After all the functions have been finalized and the global variables created, the frontend must call <code>cgraph_finalize_compilation_unit</code>.  That is where the middle-end really takes over and generates code.  The frontend must finish by calling <code>wrapup_global_declarations</code>, <code>check_global_declarations</code>, and <code>emit_debug_global_declarations</code>.</p>
<p>The above is probably slightly inaccurate, and I&#8217;m sure I&#8217;ve left out some details.  And, of course, the frontend interfaces changes with each new gcc release.  However, if you are interested in writing a gcc frontend, I hope this will give you a start.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/287/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Go New/Make</title>
		<link>http://www.airs.com/blog/archives/283</link>
		<comments>http://www.airs.com/blog/archives/283#comments</comments>
		<pubDate>Sun, 06 Dec 2009 01:16:12 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=283</guid>
		<description><![CDATA[One of the aspects of Go that some people find confusing is the two predeclared functions, new and make.  Both functions are specially implemented in the compiler.  They take a type as an argument and return a value.  This can make it seem confusing when you should use one and when you [...]]]></description>
			<content:encoded><![CDATA[<p>One of the aspects of Go that some people find confusing is the two predeclared functions, <code>new</code> and <code>make</code>.  Both functions are specially implemented in the compiler.  They take a type as an argument and return a value.  This can make it seem confusing when you should use one and when you should use the other.  In fact the functions are quite different.</p>
<p>The <code>new</code> function is the familiar one from C++ and other languages: it takes a type, allocates a new zero value of that type in the heap, and returns a pointer to the new value.  A call <code>new(T)</code> returns a value <code>*T</code> (in Go the <code>*</code> in a pointer type appears before the type to which it points, not after).  The important point here is that new creates a zero value of the given type.</p>
<p>The <code>make</code> function is different.  It creates a non-zero value of the given type.  The <code>make</code> function may only be used with a few specific types: slices, maps, and channels.  The zero value for these types is simply <code>nil</code>.  In the case of maps and channels, <code>make</code> is the only way to create a real, non-<code>nil</code>, value.  In the case of a slice, <code>make</code> creates an array in the heap (as with <code>new</code>) and then returns a pointer to that array converted to a slice.</p>
<p>So you use <code>new</code> when you want to allocate space in the heap, e.g., for a linked list.  You use <code>make</code> when you want to create a map or channel, or as a convenient shorthand for creating a slice.</p>
<p>Composite literals are also related to <code>new</code>, but not <code>make</code>.  If you take the address of a composite literal, as in <code>&#038;(struct { i int}){1}</code> then you get a new value on the heap.  Each time you execute that code, you get a new one.  So taking the address of a composite literal is similar to using <code>new</code>, except that you get a non-zero value.  This serves as a convenient, frequently used, shorthand.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/283/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Go Interface Values</title>
		<link>http://www.airs.com/blog/archives/281</link>
		<comments>http://www.airs.com/blog/archives/281#comments</comments>
		<pubDate>Mon, 30 Nov 2009 01:53:32 +0000</pubDate>
		<dc:creator>Ian Lance Taylor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.airs.com/blog/?p=281</guid>
		<description><![CDATA[While interface values in Go are flexible, they do have confusing aspects.
An interface value&#8212;e.g., a variable of an interface type&#8212; holds a value of some other type.  The interface type is known as the static type, since that is the type that the compiler sees at compile type.  The other type, which is [...]]]></description>
			<content:encoded><![CDATA[<p>While interface values in Go are flexible, they do have confusing aspects.</p>
<p>An interface value&mdash;e.g., a variable of an interface type&mdash; holds a value of some other type.  The interface type is known as the static type, since that is the type that the compiler sees at compile type.  The other type, which is only visible at runtime, is known as the dynamic type.  The dynamic type is, by definition, never an interface type; it can be any other sort of type, though.</p>
<p>When you copy an interface value, via assignment or a function call, you are making a copy of the value of the dynamic type.  This is how most types work in general.  However, a very common case of using an interface is to have the dynamic type be a pointer type.  In that case, when you copy the interface, you copy the pointer, but you of course do not copy the value to which the pointer points.  In many cases, the methods of a type will take a receiver of pointer type; this is required if the methods are going to change the value itself.  When that happens for a method which the interface requires, then the interface actually requires that the dynamic type be a pointer type.</p>
<p>What this means is that although interfaces are technically always copied as a value, in actual use they often behave as though they are copied by reference.  That is, although there is no explicit marker, interface objects often act like pointers.  This can be confusing until you understand what is really going on.</p>
<p>In my last post I mentioned that for gccgo, an interface value always holds a pointer to the value stored in the interface.  Now I&#8217;m going to correct that: if a program stores a pointer (or a value of the type <code>unsafe.Pointer</code>) in an interface, then the value stored in the interface is the pointer itself.  That is, gccgo does not store a pointer to the pointer (which would require allocating heap space to hold the pointer).  This is a natural efficiency hack, since in practice most interface objects hold pointers.</p>
<p>This efficiency hack carries through to the implementation of methods.  Methods are implemented to always accept a pointer as the receiver parameter.  If the receiver type of the method is actually not a pointer, then the pointer is implicitly dereferenced, and the value copied, at the start of the method&#8217;s code.  This means that when calling a method on an interface, the value stored in the interface can be passed directly to the method, regardless of whether the dynamic type is a pointer or not.  (As with the last post, the gc compiler does things somewhat differently.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.airs.com/blog/archives/281/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
