Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / runtime / gc / sources.c
1 /* Copyright (C) 1999-2006, 2008 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 * Copyright (C) 1997-2000 NEC Research Institute.
4 *
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
7 */
8
9 GC_sourceSeqIndex getCachedStackTopFrameSourceSeqIndex (GC_state s) {
10 GC_frameIndex i;
11
12 i = getCachedStackTopFrameIndex (s);
13 assert(i < s->sourceMaps.frameSourcesLength);
14 return s->sourceMaps.frameSources[i];
15 }
16
17 char* getSourceName (GC_state s, GC_sourceIndex i) {
18 assert (i < s->sourceMaps.sourcesLength);
19 return s->sourceMaps.sourceNames[s->sourceMaps.sources[i].sourceNameIndex];
20 }
21
22 char* GC_sourceName (GC_state s, GC_sourceIndex i) {
23 return getSourceName (s, i);
24 }
25
26 #if HAS_TIME_PROFILING
27
28 int compareSourceLabels (const void *v1, const void *v2) {
29 const struct GC_sourceLabel* l1 = (const struct GC_sourceLabel*)v1;
30 const struct GC_sourceLabel* l2 = (const struct GC_sourceLabel*)v2;
31 uintptr_t ui1 = (uintptr_t)(l1->label);
32 uintptr_t ui2 = (uintptr_t)(l2->label);
33
34 if (ui1 < ui2)
35 return -1;
36 else if (ui1 == ui2)
37 return 0;
38 else /* if (ui1 > ui2) */
39 return 1;
40 }
41
42 void sortSourceLabels (GC_state s) {
43 GC_sourceLabelIndex i;
44
45 /* Sort sourceLabels by address. */
46 qsort (s->sourceMaps.sourceLabels,
47 s->sourceMaps.sourceLabelsLength,
48 sizeof (*s->sourceMaps.sourceLabels),
49 compareSourceLabels);
50 if (0 == s->sourceMaps.sourceLabels[s->sourceMaps.sourceLabelsLength - 1].label)
51 die ("Max source label is 0 -- something is wrong.");
52 if (ASSERT)
53 for (i = 1; i < s->sourceMaps.sourceLabelsLength; i++)
54 assert (s->sourceMaps.sourceLabels[i-1].label
55 <= s->sourceMaps.sourceLabels[i].label);
56 }
57
58 void compressSourceLabels (GC_state s) {
59 GC_sourceLabelIndex in, out, i;
60 GC_sourceSeqIndex sourceSeqIndex;
61
62 /* Eliminate duplicate sourceLabels */
63 out = 0;
64 sourceSeqIndex = SOURCE_SEQ_UNKNOWN;
65 for (in = 0; in < s->sourceMaps.sourceLabelsLength; ++in) {
66 if (s->sourceMaps.sourceLabels[in].sourceSeqIndex != sourceSeqIndex) {
67 s->sourceMaps.sourceLabels[out++] = s->sourceMaps.sourceLabels[in];
68 sourceSeqIndex = s->sourceMaps.sourceLabels[in].sourceSeqIndex;
69 }
70 }
71
72 s->sourceMaps.sourceLabelsLength = out;
73
74 if (DEBUG_SOURCES)
75 for (i = 0; i < s->sourceMaps.sourceLabelsLength; i++)
76 fprintf (stderr, FMTPTR" "FMTSSI"\n",
77 (uintptr_t)s->sourceMaps.sourceLabels[i].label,
78 s->sourceMaps.sourceLabels[i].sourceSeqIndex);
79 }
80
81 void initSourceLabels (GC_state s) {
82 sortSourceLabels (s);
83 compressSourceLabels (s);
84 }
85
86 #endif
87
88 void showSources (GC_state s) {
89 uint32_t i;
90 uint32_t j;
91
92 fprintf (stdout, "0x%08"PRIx32"\n", s->magic);
93 fprintf (stdout, "%"PRIu32"\n", s->sourceMaps.sourceNamesLength);
94 for (i = 0; i < s->sourceMaps.sourceNamesLength; i++)
95 fprintf (stdout, "%s\n", s->sourceMaps.sourceNames[i]);
96 fprintf (stdout, "%"PRIu32"\n", s->sourceMaps.sourcesLength);
97 for (i = 0; i < s->sourceMaps.sourcesLength; i++)
98 fprintf (stdout, "%"PRIu32" %"PRIu32"\n",
99 s->sourceMaps.sources[i].sourceNameIndex,
100 s->sourceMaps.sources[i].successorSourceSeqIndex);
101 fprintf (stdout, "%"PRIu32"\n", s->sourceMaps.sourceSeqsLength);
102 for (i = 0; i < s->sourceMaps.sourceSeqsLength; i++) {
103 uint32_t *sourceSeq;
104
105 sourceSeq = s->sourceMaps.sourceSeqs[i];
106 for (j = 1; j <= sourceSeq[0]; j++)
107 fprintf (stdout, "%"PRIu32" ", sourceSeq[j]);
108 fprintf (stdout, "\n");
109 }
110 }