Use static inline to do the right thing when built in C99 mode. Of course,
[llvm/stm8.git] / runtime / libprofile / GCDAProfiling.c
blobc8161d44b07ffd6aeef22034893fb3c6d31869ad
1 /*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
2 |*
3 |* The LLVM Compiler Infrastructure
4 |*
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
7 |*
8 |*===----------------------------------------------------------------------===*|
9 |*
10 |* This file implements the call back routines for the gcov profiling
11 |* instrumentation pass. Link against this library when running code through
12 |* the -insert-gcov-profiling LLVM pass.
14 |* We emit files in a corrupt version of GCOV's "gcda" file format. These files
15 |* are only close enough that LCOV will happily parse them. Anything that lcov
16 |* ignores is missing.
18 |* TODO: gcov is multi-process safe by having each exit open the existing file
19 |* and append to it. We'd like to achieve that and be thread-safe too.
21 \*===----------------------------------------------------------------------===*/
23 #include "llvm/Support/DataTypes.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 /* #define DEBUG_GCDAPROFILING */
31 * --- GCOV file format I/O primitives ---
34 static FILE *output_file = NULL;
36 static void write_int32(uint32_t i) {
37 fwrite(&i, 4, 1, output_file);
40 static void write_int64(uint64_t i) {
41 uint32_t lo, hi;
42 lo = i >> 0;
43 hi = i >> 32;
45 write_int32(lo);
46 write_int32(hi);
50 * --- LLVM line counter API ---
53 /* A file in this case is a translation unit. Each .o file built with line
54 * profiling enabled will emit to a different file. Only one file may be
55 * started at a time.
57 void llvm_gcda_start_file(const char *filename) {
58 output_file = fopen(filename, "w+");
60 /* gcda file, version 404*, stamp LLVM. */
61 fwrite("adcg*404MVLL", 12, 1, output_file);
63 #ifdef DEBUG_GCDAPROFILING
64 printf("llvmgcda: [%s]\n", filename);
65 #endif
68 /* Given an array of pointers to counters (counters), increment the n-th one,
69 * where we're also given a pointer to n (predecessor).
71 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
72 uint64_t **counters) {
73 uint64_t *counter;
74 if (*predecessor == 0xffffffff)
75 return;
77 /* Don't crash if the pred# is out of sync. This can happen due to threads,
78 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
79 if ((counter = counters[*predecessor]))
80 ++*counter;
81 #ifdef DEBUG_GCDAPROFILING
82 else
83 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
84 state_table_row, *predecessor);
85 #endif
88 void llvm_gcda_emit_function(uint32_t ident) {
89 #ifdef DEBUG_GCDAPROFILING
90 printf("llvmgcda: function id=%x\n", ident);
91 #endif
93 /* function tag */
94 fwrite("\0\0\0\1", 4, 1, output_file);
95 write_int32(2);
96 write_int32(ident);
97 write_int32(0);
100 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
101 uint32_t i;
102 /* counter #1 (arcs) tag */
103 fwrite("\0\0\xa1\1", 4, 1, output_file);
104 write_int32(num_counters * 2);
105 for (i = 0; i < num_counters; ++i) {
106 write_int64(counters[i]);
109 #ifdef DEBUG_GCDAPROFILING
110 printf("llvmgcda: %u arcs\n", num_counters);
111 for (i = 0; i < num_counters; ++i) {
112 printf("llvmgcda: %llu\n", (unsigned long long)counters[i]);
114 #endif
117 void llvm_gcda_end_file() {
118 /* Write out EOF record. */
119 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
120 fclose(output_file);
121 output_file = NULL;
123 #ifdef DEBUG_GCDAPROFILING
124 printf("llvmgcda: -----\n");
125 #endif