Skip to content

Commit 4b55db1

Browse files
rscYinJiaJin
authored and
YinJiaJin
committed
cmd/gc: make qsort comparisons totally ordered
Otherwise different qsort implementations might result in different sort orders and therefore different compiled object files. Change-Id: Ie783ba55a55af06941307e150b0c406e0a8128b0 Reviewed-on: https://go-review.googlesource.com/4590 Reviewed-by: Austin Clements <[email protected]>
1 parent 37b0cf8 commit 4b55db1

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/cmd/gc/popt.c

+8
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,14 @@ startcmp(const void *va, const void *vb)
532532
return -1;
533533
if(a->start > b->start)
534534
return +1;
535+
// Order what's left by id or symbol name,
536+
// just so that sort is forced into a specific ordering,
537+
// so that the result of the sort does not depend on
538+
// the sort implementation.
539+
if(a->def != b->def)
540+
return a->def->id - b->def->id;
541+
if(a->node != b->node)
542+
return strcmp(a->node->sym->name, b->node->sym->name);
535543
return 0;
536544
}
537545

src/cmd/gc/reg.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ static int
5050
rcmp(const void *a1, const void *a2)
5151
{
5252
Rgn *p1, *p2;
53-
int c1, c2;
5453

5554
p1 = (Rgn*)a1;
5655
p2 = (Rgn*)a2;
57-
c1 = p2->cost;
58-
c2 = p1->cost;
59-
if(c1 -= c2)
60-
return c1;
61-
return p2->varno - p1->varno;
56+
if(p1->cost != p2->cost)
57+
return p2->cost - p1->cost;
58+
if(p1->varno != p2->varno)
59+
return p2->varno - p1->varno;
60+
if(p1->enter != p2->enter)
61+
return p2->enter->id - p1->enter->id;
62+
return 0;
6263
}
6364

6465
static void

0 commit comments

Comments
 (0)