summary refs log tree commit diff stats
path: root/compiler/sem.nim
Commit message (Collapse)AuthorAgeFilesLines
* IC: backend: remember produced type information (#17440)Andreas Rumpf2021-03-231-1/+1
|
* semTemplateDef and t17433 clean-ups (#17448)Saem Ghani2021-03-221-2/+22
| | | | | - use `doAssert` in t17433 - use setGenericParamsMisc in semTemplateDef akin to semProcAux - pragma handling in semTemplateDef inline with semProcAux
* semLambda removed, semProcAux reworked (#17379)Saem Ghani2021-03-171-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * simplified proc-like name ident to symbol code * wip - reworking generic param sem * wip - closer to removing nkEmpty generic params * it's hacky but tests pass * slowly tweaking semProcAux to take on semLambda * fix pragma superset check proto vs current * Set the symbol owner earlier * partial progress reworking proto found bug where default values between forward and impl lead to overload resolution issues. * simplified pragma handling and callConv checks Co-authored-by: Clyybber <Clyybber@users.noreply.github.com> * partially working * cgexprs issue * It works! * comment clean-up * clean-up asserts, comments, and other bits * add isGenericParams, inline isGeneric queries * seeing if this is sufficiently consistent * can use this approach or continue it in a further PR * commentary about nullary generics and clean-ups * fixed a mistake in PNode isGenericRoutine * Some small cleanups * Small cleanup * for func lambdas ensure we use lambda pragmas * add some basic compileTime func tests * [ci skip] remove comments Co-authored-by: Clyybber <Clyybber@users.noreply.github.com> Co-authored-by: Clyybber <darkmine956@gmail.com>
* new-style concepts implementation, WIP (#15251)Andreas Rumpf2021-02-241-1/+1
| | | | | | | | | | | | | * fixes #15210 [backport:1.2] * make tests green * make ordinal work * makes Swapable test compile * make Indexable example work * concepts: 'self' is now 'Self' * concepts: make Dictionary example compile * document the new concept implementation * concepts: make typeDesc work properly * concepts: allow documentation comments (d'oh)
* use typeof instead type (#16962)flywind2021-02-081-1/+1
|
* IC: bugfixes (WIP) (#16836)Andreas Rumpf2021-02-021-0/+1
| | | | | | | | * minor improvements * IC: added the required logic for compilerProcs * LazySym ftw * we need this testing logic * reimplement the old way we use for module package creation * fixes a regression; don't pick module names if you can avoid it
* IC: next steps (#16729)Andreas Rumpf2021-01-231-6/+6
| | | | | | | | | | | * IC: dead code elimination pass * preparations for a different codegen strategy * added documentation to the newly written code * IC: backend code * IC: backend adjustments * optimized the compiler a bit * IC: yet another massive refactoring * fixes regressions * cleanups
* IC: next steps (#16705)Andreas Rumpf2021-01-141-0/+2
| | | | | | | | | | | * code cleanups * refactorings for IC * more refactorings for IC * IC: attach the 'nil' type to its module * IC: refactorings and improvements * IC: progress * IC: more serialization fixes * IC: embarrassing omission * code cleanups
* IC: next steps (#16550)Andreas Rumpf2021-01-071-1/+2
| | | | | | | | | | | | | | | | | | * cleanups * ast.nim: cleanups * IC: no more sym.tab field, stored externally in the module graph * nimble compiles again * rodfiles: store bitwidth of integers and the endianness in the cookie because we serialize 'int' directly * rodfiles: added compilerproc and export sections * rodfiles: added all the missing sections * rodfiles: track the missing information * IC: architecture for lazy loading of proc bodies * make tests green again * completed the lazy loading of proc bodies * symbol lookup integration, part 1 * symbol lookup integration, part 2 * symbol lookup integration, part 3 * make tcompilerapi work again * rodfiles: fixed config change handling
* big steps torwards an efficient, simple IC implementation (#16543)Andreas Rumpf2021-01-021-10/+10
| | | | | | | | | | | | | | | | | | | * reworked ID handling * the packed AST now has its own ID mechanism * basic serialization code works * extract rodfiles to its own module * rodfiles: store and compare configs * rodfiles: store dependencies * store config at the end * precise dependency tracking * dependency tracking for rodfiles * completed loading of PSym, PType, etc * removed dead code * bugfix: do not realloc seqs when taking addr into an element * make IC opt-in for now * makes tcompilerapi green again * final cleanups Co-authored-by: Andy Davidoff <github@andy.disruptek.com>
* refactorings to prepare the compiler for IC (#15935)Andreas Rumpf2020-12-171-3/+5
| | | | | | | | | | | | | | * added ic specific Nim code; WIP * make the symbol import mechanism lazy; WIP * ensure that modules can be imported multiple times * ambiguity checking * handle converters and TR macros properly * make 'enum' test category green again * special logic for semi-pure enums * makes nimsuggest tests green again * fixes nimdata * makes nimpy green again * makes more important packages work
* fix #16150 improve type mismatch errors (#16152)Timothee Cour2020-12-091-1/+1
| | | | | | | * fix #16150 improve type mismatch errors * allow -d:nimLegacyTypeMismatch * address comment
* explicit ID generation for easier IC (#15559)Andreas Rumpf2020-10-251-22/+22
| | | | | | | | | | | | | | | | | * refactoring: idents don't need inheritance * refactoring: adding an IdGenerator (part 1) * refactoring: adding an IdGenerator (part 2) * refactoring: adding an IdGenerator (part 3) * refactoring: adding an IdGenerator (part 4) * refactoring: adding an IdGenerator (part 5) * refactoring: adding an IdGenerator (part 5) * IdGenerator must be a ref type; hello world works again * make bootstrapping work again * progress: add back the 'exactReplica' ideas * added back the missing exactReplica hacks * make tcompilerapi work again * make important packages green * attempt to fix the build for 32 bit machines (probably need a better solution here)
* Revert "fixes #15280 [backport:1.2] (#15281)" (#15700)Clyybber2020-10-241-1/+1
| | | This reverts commit 3f00a738dbc8319b4dd2b86bf5529c096f2dd243.
* fixes #15280 [backport:1.2] (#15281)Andreas Rumpf2020-09-091-1/+1
| | | | | | | * fixes #15280 [backport:1.2] * make tests green again * adapt tests
* borrow checking (#15282)Andreas Rumpf2020-09-091-5/+5
| | | | | | | | | | | * refactoring: move procs to typeallowed.nim * frontend preparations for first class openArray support * prepare the code generator for first class openArray * code generation for first class openArray; WIP * code generation for open arrays, progress * added isViewType proc * preparations for borrow checking * added borrow checking to the front end
* Fix #5691 (#15158)Clyybber2020-08-271-8/+5
| | | | | | | | * Fix #5691 * Cleanup and thoughts * Use scope approach * Seperate defined/declared/declaredInScope magics * Fix declaredInScope * Update spec accordingly
* Fix forward declaration issues in template/macro context (#15091)Clyybber2020-07-291-1/+2
| | | | | | | | | | | | | | | | | | | | | | | * Fix forward declaration issues in template/macro context * Correct forward declaration resolving for overloads * Remove old dead code * WIP consistent gensym ids * Minimize diff * Remove obsoleted hack * Add templInstCounter to give unique IDs to template instantiations * Remove obsoleted code * Eh, init in myOpen, not myProcess... * Remove optNimV019 * Add testcase for #13484
* 'isolate' builtin; refs https://github.com/nim-lang/RFCs/issues/244 (#15011)Andreas Rumpf2020-07-201-1/+2
|
* misc cleanups in compiler msgs: use toHumanStr, etc (#14677)Timothee Cour2020-06-151-4/+2
|
* * honor --errorMax even for tools (eg drnim, nim doc) (#14546)Timothee Cour2020-06-021-1/+1
| | | * fix a bug that prevented nim doc compiler/nim on windows
* disallow typedesc in arrays & move existing checks to `types.typeAllowedAux` ↵Neelesh Chandola2020-05-291-5/+7
| | | | | | (#13261) * disallow typedesc in arrays and move previous checks to types.typeAllowedAux
* Make ./koch temp --gc:arc work (#14186)Clyybber2020-05-011-2/+5
|
* implement (#14114)cooldome2020-04-251-8/+3
| | | Co-authored-by: cooldome <ariabushenko@bk.ru>
* The raises list can now use expressions referencing the generic paramsZahary Karadjov2020-04-011-0/+1
|
* Replace tfHasRequiresInit with a more accurate mechanismZahary Karadjov2020-04-011-1/+3
| | | | | | | | | The new mechanism can deal with more complex scenarios such as not nil field appearing in a non-default case object branch or a field within a generic object that may depend on a when branch. The commit also plugs another hole: the user is no longer able to create illegal default values through seq.setLen(N).
* Perform nil checks during object construction and within compiles()Zahary Karadjov2020-04-011-1/+1
| | | | Close https://github.com/nim-lang/Nim/issues/6494
* Plug another hole: default(T) forbidden for objects requiring initializationZahary Karadjov2020-04-011-18/+1
|
* Don't allow 'var x: T' for objects that require initializationZahary Karadjov2020-04-011-0/+18
|
* EndsInNoReturn in expressions extension, fixes #13490 (#13520)cooldome2020-02-281-1/+1
| | | | * fix #13490
* fixes #12627 (#13521)cooldome2020-02-281-1/+1
| | | | * fixes #12627
* make case-object transitions explicit, make unknownLineInfo a const, replace ↵Jasper Jenkins2020-01-171-1/+1
| | | | a few magic numbers with consts (#13170)
* remove all remaining warnings when build nim (with -d:nimHasLibFFI) (#13084)Timothee Cour2020-01-091-1/+1
| | | | | | * cleanup deprecations in evalffi + elsewhere * remove dead code getOrdValue64
* Cosmetic compiler cleanup (#12718)Clyybber2019-11-281-18/+17
| | | | | | | | | | | | | | | | | | * Cleanup compiler code base * Unify add calls * Unify len invocations * Unify range operators * Fix oversight * Remove {.procvar.} pragma * initCandidate -> newCandidate where reasonable * Unify safeLen calls
* Small ast.nim cleanup (#12156)Clyybber2019-09-091-1/+1
| | | | | * Remove sonsLen * Use Indexable
* fixes #11941 (#12079)Andreas Rumpf2019-08-291-2/+7
|
* lift destructor for openarray (#12073)cooldome2019-08-281-6/+4
| | | | * destroy for sink openarray
* Fix several float range issues (supersedes #11905) (#12067)Andreas Rumpf2019-08-271-0/+1
|\ | | | | | | | | | | * Fix several float range issues * address the comments
| * address the commentsnarimiran2019-08-271-0/+1
| |
* | fixes #12029; finish the 'unused import' feature (#12064)Andreas Rumpf2019-08-271-1/+2
|/
* [refactoring] compiler: simplified markUsedAndreas Rumpf2019-08-081-1/+1
|
* IC: some progressAndreas Rumpf2019-07-271-6/+0
|
* revert changesAndrii Riabushenko2019-07-251-2/+2
|
* fixes #11826Andrii Riabushenko2019-07-251-2/+2
|
* Merge branch 'devel' into araq-detect-unused-importsAndreas Rumpf2019-07-181-4/+4
|\
| * [refactoring] remove unused imports in the compiler and in some stdlib modulesAraq2019-07-181-4/+4
| |
* | warn about unused imports; fixes an 'export' regression [nobackport]Andreas Rumpf2019-07-181-1/+1
| |
* | [feature] detect unused importsAraq2019-07-171-1/+6
|/
* [refactoring] moves transformation for 'spawn' into its own spawn.nim ↵Araq2019-07-141-1/+1
| | | | implementation
* introduce internal Int128 type, use it to fix case stmt checks (#11652)Arne Döring2019-07-051-1/+1
| | | | | | * initial version of int128 * use int128 in case stmt * fixes #11552
iv>
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
      













                 

                          
              





                                           
                                                                                                                                                                                                                                                             

                                            
                                                                                                                                                                                                                           



                                   
                                                                                                                                                                                                                                                             

                                    
                                                                                                                                                                     



                                    
                                                                                                                                                                                                                                                     

                                     
                                                                                                                                                                                                                                     



                                          
                                                                                                                                                                                                                                                             

                                           
                                                                                                                                                                                                                          



                                   
                                                                                                                                                                                                                                                        

                                    
                                                                                                                                                     



                                   
                                                                                                                                                                                                                                                   

                                    
                                                                                                                                                                                                                                             


                    


                     
             






                                                             






                                                             
                                                
 




                           
 
                           



                                  




























                                                                                 






                                                                        























                                                            


                                                                    
                 
                                           
                                                                  
                                                





                                               

                                                
                        


                                  














                                                              

                                                              
                                    
                              







                                              


                                             














                                                        






                                                                         





                                              


                                   
                                                


                                  


                                                                        


                            
























                                                       
                                                                                               
                                                        
                                       



                                                  



                                       



                            


                                 




                                       
                                                                                              

                                                                        
                                         




                            
 
enum {
  T_ENTER_CA,
  T_EXIT_CA,
  T_SHOW_CURSOR,
  T_HIDE_CURSOR,
  T_CLEAR_SCREEN,
  T_SGR0,
  T_UNDERLINE,
  T_BOLD,
  T_BLINK,
  T_REVERSE,
  T_ENTER_KEYPAD,
  T_EXIT_KEYPAD,
  T_ENTER_MOUSE,
  T_EXIT_MOUSE,
  T_ENTER_BRACKETED_PASTE,
  T_EXIT_BRACKETED_PASTE,
  T_FUNCS_NUM,
};

#define EUNSUPPORTED_TERM -1

// rxvt-256color
static const char *rxvt_256color_keys[] = {
  "\033[11~", "\033[12~", "\033[13~", "\033[14~", "\033[15~", "\033[17~", "\033[18~", "\033[19~", "\033[20~", "\033[21~", "\033[23~", "\033[24~", "\033[2~", "\033[3~", "\033[7~", "\033[8~", "\033[5~", "\033[6~", "\033[A", "\033[B", "\033[D", "\033[C", 0
};
static const char *rxvt_256color_funcs[] = {
  "\0337\033[?47h", "\033[2J\033[?47l\0338", "\033[?25h", "\033[?25l", "\033[H\033[2J", "\033[m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "\033=", "\033>", "\033[?1000h", "\033[?1000l", "\033[?2004h", "\033[?2004l",
};

// Eterm
static const char *eterm_keys[] = {
  "\033[11~", "\033[12~", "\033[13~", "\033[14~", "\033[15~", "\033[17~", "\033[18~", "\033[19~", "\033[20~", "\033[21~", "\033[23~", "\033[24~", "\033[2~", "\033[3~", "\033[7~", "\033[8~", "\033[5~", "\033[6~", "\033[A", "\033[B", "\033[D", "\033[C", 0
};
static const char *eterm_funcs[] = {
  "\0337\033[?47h", "\033[2J\033[?47l\0338", "\033[?25h", "\033[?25l", "\033[H\033[2J", "\033[m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "", "", "", "", "", "",
};

// screen
static const char *screen_keys[] = {
  "\033OP", "\033OQ", "\033OR", "\033OS", "\033[15~", "\033[17~", "\033[18~", "\033[19~", "\033[20~", "\033[21~", "\033[23~", "\033[24~", "\033[2~", "\033[3~", "\033[1~", "\033[4~", "\033[5~", "\033[6~", "\033OA", "\033OB", "\033OD", "\033OC", 0
};
static const char *screen_funcs[] = {
  "\033[?1049h", "\033[?1049l", "\033[34h\033[?25h", "\033[?25l", "\033[H\033[J", "\033[m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "\033[?1h\033=", "\033[?1l\033>", "\033[?1000h", "\033[?1000l", "\033[?2004h", "\033[?2004l",
};

// rxvt-unicode
static const char *rxvt_unicode_keys[] = {
  "\033[11~", "\033[12~", "\033[13~", "\033[14~", "\033[15~", "\033[17~", "\033[18~", "\033[19~", "\033[20~", "\033[21~", "\033[23~", "\033[24~", "\033[2~", "\033[3~", "\033[7~", "\033[8~", "\033[5~", "\033[6~", "\033[A", "\033[B", "\033[D", "\033[C", 0
};
static const char *rxvt_unicode_funcs[] = {
  "\033[?1049h", "\033[r\033[?1049l", "\033[?25h", "\033[?25l", "\033[H\033[2J", "\033[m\033(B", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "\033=", "\033>", "\033[?1000h", "\033[?1000l", "\033[?2004h", "\033[?2004l",
};

// linux
static const char *linux_keys[] = {
  "\033[[A", "\033[[B", "\033[[C", "\033[[D", "\033[[E", "\033[17~", "\033[18~", "\033[19~", "\033[20~", "\033[21~", "\033[23~", "\033[24~", "\033[2~", "\033[3~", "\033[1~", "\033[4~", "\033[5~", "\033[6~", "\033[A", "\033[B", "\033[D", "\033[C", 0
};
static const char *linux_funcs[] = {
  "", "", "\033[?25h\033[?0c", "\033[?25l\033[?1c", "\033[H\033[J", "\033[0;10m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "", "", "", "", "", "",
};

// xterm
static const char *xterm_keys[] = {
  "\033OP", "\033OQ", "\033OR", "\033OS", "\033[15~", "\033[17~", "\033[18~", "\033[19~", "\033[20~", "\033[21~", "\033[23~", "\033[24~", "\033[2~", "\033[3~", "\033OH", "\033OF", "\033[5~", "\033[6~", "\033OA", "\033OB", "\033OD", "\033OC", 0
};
static const char *xterm_funcs[] = {
  "\033[?1049h", "\033[?1049l", "\033[?12l\033[?25h", "\033[?25l", "\033[H\033[2J", "\033(B\033[m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "\033[?1h\033=", "\033[?1l\033>", "\033[?1000h", "\033[?1000l", "\033[?2004h", "\033[?2004l",
};

static struct term {
  const char *name;
  const char **keys;
  const char **funcs;
} terms[] = {
  {"rxvt-256color", rxvt_256color_keys, rxvt_256color_funcs},
  {"Eterm", eterm_keys, eterm_funcs},
  {"screen", screen_keys, screen_funcs},
  {"rxvt-unicode", rxvt_unicode_keys, rxvt_unicode_funcs},
  {"linux", linux_keys, linux_funcs},
  {"xterm", xterm_keys, xterm_funcs},
  {0, 0, 0},
};

static bool init_from_terminfo = false;
static const char **keys;
static const char **funcs;

static int try_compatible(const char *term, const char *name,
        const char **tkeys, const char **tfuncs)
{
  if (strstr(term, name)) {
    keys = tkeys;
    funcs = tfuncs;
    return 0;
  }

  return EUNSUPPORTED_TERM;
}

static int init_term_builtin(void)
{
  int i;
  const char *term = getenv("TERM");

  if (term) {
    for (i = 0; terms[i].name; i++) {
      if (!strcmp(terms[i].name, term)) {
        keys = terms[i].keys;
        funcs = terms[i].funcs;
        return 0;
      }
    }

    /* let's do some heuristic, maybe it's a compatible terminal */
    if (try_compatible(term, "xterm", xterm_keys, xterm_funcs) == 0)
      return 0;
    if (try_compatible(term, "rxvt", rxvt_unicode_keys, rxvt_unicode_funcs) == 0)
      return 0;
    if (try_compatible(term, "linux", linux_keys, linux_funcs) == 0)
      return 0;
    if (try_compatible(term, "Eterm", eterm_keys, eterm_funcs) == 0)
      return 0;
    if (try_compatible(term, "screen", screen_keys, screen_funcs) == 0)
      return 0;
    /* let's assume that 'cygwin' is xterm compatible */
    if (try_compatible(term, "cygwin", xterm_keys, xterm_funcs) == 0)
      return 0;
  }

  return EUNSUPPORTED_TERM;
}

//----------------------------------------------------------------------
// terminfo
//----------------------------------------------------------------------

static char *read_file(const char *file) {
  FILE *f = fopen(file, "rb");
  if (!f)
    return 0;

  struct stat st;
  if (fstat(fileno(f), &st) != 0) {
    fclose(f);
    return 0;
  }

  char *data = malloc(st.st_size);
  if (!data) {
    fclose(f);
    return 0;
  }

  if (fread(data, 1, st.st_size, f) != (size_t)st.st_size) {
    fclose(f);
    free(data);
    return 0;
  }

  fclose(f);
  return data;
}

static char *terminfo_try_path(const char *path, const char *term) {
  char tmp[4096];
  // snprintf guarantee for older compilers
  assert(sizeof(tmp) > sizeof(path)+sizeof("/x/")+sizeof(term)+1);
  sprintf(tmp, "%s/%c/%s", path, term[0], term);
  char *data = read_file(tmp);
  if (data) {
    return data;
  }

  // fallback to darwin specific dirs structure
  // snprintf guarantee above still applies
  sprintf(tmp, "%s/%x/%s", path, term[0], term);
  return read_file(tmp);
}

static char *load_terminfo(void) {
  char tmp[4096];
  const char *term = getenv("TERM");
  if (!term) {
    return 0;
  }

  // if TERMINFO is set, no other directory should be searched
  const char *terminfo = getenv("TERMINFO");
  if (terminfo) {
    return terminfo_try_path(terminfo, term);
  }

  // next, consider ~/.terminfo
  const char *home = getenv("HOME");
  if (home) {
    // snprintf guarantee for older compilers
    assert(sizeof(tmp) > sizeof(home)+sizeof("/.terminfo")+1);
    strncpy(tmp, home, sizeof(tmp));
    strcat(tmp, "/.terminfo");
    char *data = terminfo_try_path(tmp, term);
    if (data)
      return data;
  }

  // next, TERMINFO_DIRS
  const char *dirs = getenv("TERMINFO_DIRS");
  if (dirs) {
    // snprintf guarantee for older compilers
    assert(sizeof(tmp) > sizeof(dirs));
    strncpy(tmp, dirs, sizeof(tmp));
    char *dir = strtok(tmp, ":");
    while (dir) {
      const char *cdir = dir;
      if (strcmp(cdir, "") == 0) {
        cdir = "/usr/share/terminfo";
      }
      char *data = terminfo_try_path(cdir, term);
      if (data)
        return data;
      dir = strtok(0, ":");
    }
  }

  // fallback to /usr/share/terminfo
  return terminfo_try_path("/usr/share/terminfo", term);
}

#define TI_MAGIC 0432
#define TI_HEADER_LENGTH 12
#define TB_KEYS_NUM 22

static const char *terminfo_copy_string(char *data, int str, int table) {
  const int16_t off = *(int16_t*)(data + str);
  const char *src = data + table + off;
  int len = strlen(src);
  char *dst = malloc(len+1);
  strcpy(dst, src);
  return dst;
}

static const int16_t ti_funcs[] = {
  28, 40, 16, 13, 5, 39, 36, 27, 26, 34, 89, 88,
};

static const int16_t ti_keys[] = {
  66, 68 /* apparently not a typo; 67 is F10 for whatever reason */, 69,
  70, 71, 72, 73, 74, 75, 67, 216, 217, 77, 59, 76, 164, 82, 81, 87, 61,
  79, 83,
};

static int init_term(void) {
  int i;
  char *data = load_terminfo();
  if (!data) {
    init_from_terminfo = false;
    return init_term_builtin();
  }

  int16_t *header = (int16_t*)data;
  if ((header[1] + header[2]) % 2) {
    // old quirk to align everything on word boundaries
    header[2] += 1;
  }

  const int str_offset = TI_HEADER_LENGTH +
    header[1] + header[2] + 2 * header[3];
  const int table_offset = str_offset + 2 * header[4];

  keys = malloc(sizeof(const char*) * (TB_KEYS_NUM+1));
  for (i = 0; i < TB_KEYS_NUM; i++) {
    keys[i] = terminfo_copy_string(data,
      str_offset + 2 * ti_keys[i], table_offset);
  }
  keys[TB_KEYS_NUM] = 0;

  funcs = malloc(sizeof(const char*) * T_FUNCS_NUM);
  // the last four entries are reserved for mouse, bracketed paste. because the table offset is
  // not there, the two entries have to fill in manually
  for (i = 0; i < T_FUNCS_NUM-4; i++) {
    funcs[i] = terminfo_copy_string(data,
      str_offset + 2 * ti_funcs[i], table_offset);
  }

  funcs[T_FUNCS_NUM-4] = "\033[?1000h";
  funcs[T_FUNCS_NUM-3] = "\033[?1000l";
  funcs[T_FUNCS_NUM-2] = "\033[?2004h";
  funcs[T_FUNCS_NUM-1] = "\033[?2004l";

  init_from_terminfo = true;
  free(data);
  return 0;
}

static void shutdown_term(void) {
  if (init_from_terminfo) {
    int i;
    for (i = 0; i < TB_KEYS_NUM; i++) {
      free((void*)keys[i]);
    }
    // the last four entries are reserved for mouse, bracketed paste. because the table offset
    // is not there, the two entries have to fill in manually and do not
    // need to be freed.
    for (i = 0; i < T_FUNCS_NUM-4; i++) {
      free((void*)funcs[i]);
    }
    free(keys);
    free(funcs);
  }
}