aboutsummaryrefslogtreecommitdiff
path: root/compiler/misc.h
blob: 36f70e55819562a9daba8b9b4e030024c34575df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

/*

  misc.h -- Miscellaneous internal utility functons.

  Copyright © 2011-2017 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#ifndef LRL_MISC_H
#define LRL_MISC_H

#include <limits.h>
#include <stdlib.h>
#include <stdio.h>

#include "context.h"

/* Type for pointer comparison */
#if __STDC_VERSION__ < 199901L
#ifdef _WIN64
typedef unsigned long long uintptr_t;
#elif defined(__arm__)
typedef unsigned int uintptr_t;
#else
typedef unsigned long uintptr_t;
#endif
#else
#include <stdint.h>
#endif

/* Array lists */
#define init_list(p, size_var, capacity_var, initial_size) do {\
        *(size_var) = 0; \
        *(capacity_var) = (initial_size); \
        *(p) = malloc((initial_size)*sizeof(**(p))); \
    } while (0)

#define grow_list(p, size_var, capacity_var) \
    (++(*(size_var)) < *(capacity_var) || \
        (*(capacity_var) *= 2 , \
        (*(p) = try_realloc(*(p), (*(capacity_var))*sizeof(**(p)))) != NULL))

#define list_push(p, size_var, capacity_var, value) do { \
        if (!grow_list((p), (size_var), (capacity_var))) fail("list_push"); \
        (*(p))[*(size_var)-1] = (value); \
    } while (0)

#define list_get_last(p, size, value_var) \
    ((size) != 0 && ((*(value_var) = ((p)[(size)-1])), 1))

/* Linked lists */
#define linked_append(entry, first, last) do { \
        *(entry) = malloc(sizeof(**(entry))); \
        (*(entry))->next = NULL; \
        if (*(last) == NULL) { \
            *(first) = *(entry); \
        } else { \
            (*(last))->next = *(entry); \
        } \
        *(last) = *(entry); \
    } while (0)

/**
 * A linearly probed set. It provides fast a insert operation for
 * LRLHashCodes with a check for duplicates.
 */
typedef struct {
    size_t size; /* number of entries = 2^(size-1) */
    LRLHashCode *set;
} LinProbSet;

/* http://www.decompile.com/cpp/faq/file_and_line_error_string.htm */
#define STRINGIFY1(x) #x
#define STRINGIFY(x) STRINGIFY1(x)

/* Safe type conversion */
#define size2int(s) ((s) > INT_MAX ? \
    fail("size2int@" __FILE__ ":" STRINGIFY(__LINE__)),0 : (int)(s))

/* noreturn qualifier for functions */
#if defined(__GNUC__) || defined(__clang__)
#    define NORETURN __attribute__((__noreturn__))
#else
#    define NORETURN
#endif

void *try_realloc(void *p, size_t size);
char *lrl_strdup(const char *s);
char *lrl_strndup(const char *s, size_t len);
LRLHashCode hash_bytes(const unsigned char *bytes, size_t length);
LRLHashCode hash_name(const char *name, size_t length);
LRLHashCode hash_merge(LRLHashCode a, LRLHashCode b);
void linprobset_init(LinProbSet *lps, size_t bits);
int linprobset_add_hashcode(LinProbSet *lps, LRLHashCode hashcode);

void skip_path_sep(const char **path);
const char *end_of_path_elem(const char *path);
char *get_output_filename(const char *source_filename, const char *outdir,
                          int is_exec);

char *file_get_contents(FILE *file);
char *read_input(const char *filename);

NORETURN void fail(const char *error_id);


#endif