Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*	$NetBSD: reg.cpp,v 1.1.1.1 2016/01/13 18:41:48 christos Exp $	*/

// -*- C++ -*-
/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2004
   Free Software Foundation, Inc.
     Written by James Clark (jjc@jclark.com)

This file is part of groff.

groff is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

groff is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with groff; see the file COPYING.  If not, write to the Free Software
Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */

#include "troff.h"
#include "dictionary.h"
#include "token.h"
#include "request.h"
#include "reg.h"

object_dictionary number_reg_dictionary(101);

int reg::get_value(units * /*d*/)
{
  return 0;
}

void reg::increment()
{
  error("can't increment read-only register");
}

void reg::decrement()
{
  error("can't decrement read-only register");
}

void reg::set_increment(units /*n*/)
{
  error("can't auto increment read-only register");
}

void reg::alter_format(char /*f*/, int /*w*/)
{
  error("can't alter format of read-only register");
}

const char *reg::get_format()
{
  return "0";
}

void reg::set_value(units /*n*/)
{
  error("can't write read-only register");
}

general_reg::general_reg() : format('1'), width(0), inc(0)
{
}

static char uppercase_array[] = {
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  'Y', 'Z',
};

static char lowercase_array[] = {
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  'y', 'z',
};

static const char *number_value_to_ascii(int value, char format, int width)
{
  static char buf[128];		// must be at least 21
  switch(format) {
  case '1':
    if (width <= 0)
      return i_to_a(value);
    else if (width > int(sizeof(buf) - 2))
      sprintf(buf, "%.*d", int(sizeof(buf) - 2), int(value));
    else
      sprintf(buf, "%.*d", width, int(value));
    break;
  case 'i':
  case 'I':
    {
      char *p = buf;
      // troff uses z and w to represent 10000 and 5000 in Roman
      // numerals; I can find no historical basis for this usage
      const char *s = format == 'i' ? "zwmdclxvi" : "ZWMDCLXVI";
      int n = int(value);
      if (n >= 40000 || n <= -40000) {
	error("magnitude of `%1' too big for i or I format", n);
	return i_to_a(n);
      }
      if (n == 0) {
	*p++ = '0';
	*p = 0;
	break;
      }
      if (n < 0) {
	*p++ = '-';
	n = -n;
      }
      while (n >= 10000) {
	*p++ = s[0];
	n -= 10000;
      }
      for (int i = 1000; i > 0; i /= 10, s += 2) {
	int m = n/i;
	n -= m*i;
	switch (m) {
	case 3:
	  *p++ = s[2];
	  /* falls through */
	case 2:
	  *p++ = s[2];
	  /* falls through */
	case 1:
	  *p++ = s[2];
	  break;
	case 4:
	  *p++ = s[2];
	  *p++ = s[1];
	  break;
	case 8:
	  *p++ = s[1];
	  *p++ = s[2];
	  *p++ = s[2];
	  *p++ = s[2];
	  break;
	case 7:
	  *p++ = s[1];
	  *p++ = s[2];
	  *p++ = s[2];
	  break;
	case 6:
	  *p++ = s[1];
	  *p++ = s[2];
	  break;
	case 5:
	  *p++ = s[1];
	  break;
	case 9:
	  *p++ = s[2];
	  *p++ = s[0];
	}
      }
      *p = 0;
      break;
    }
  case 'a':
  case 'A':
    {
      int n = value;
      char *p = buf;
      if (n == 0) {
	*p++ = '0';
	*p = 0;
      }
      else {
	if (n < 0) {
	  n = -n;
	  *p++ = '-';
	}
	// this is a bit tricky
	while (n > 0) {
	  int d = n % 26;
	  if (d == 0)
	    d = 26;
	  n -= d;
	  n /= 26;
	  *p++ = format == 'a' ? lowercase_array[d - 1] :
				 uppercase_array[d - 1];
	}
	*p-- = 0;
	char *q = buf[0] == '-' ? buf + 1 : buf;
	while (q < p) {
	  char temp = *q;
	  *q = *p;
	  *p = temp;
	  --p;
	  ++q;
	}
      }
      break;
    }
  default:
    assert(0);
    break;
  }
  return buf;
}

const char *general_reg::get_string()
{
  units n;
  if (!get_value(&n))
    return "";
  return number_value_to_ascii(n, format, width);
}


void general_reg::increment()
{
  int n;
  if (get_value(&n))
    set_value(n + inc);
}

void general_reg::decrement()
{
  int n;
  if (get_value(&n))
    set_value(n - inc);
}

void general_reg::set_increment(units n)
{
  inc = n;
}

void general_reg::alter_format(char f, int w)
{
  format = f;
  width = w;
}

static const char *number_format_to_ascii(char format, int width)
{
  static char buf[24];
  if (format == '1') {
    if (width > 0) {
      int n = width;
      if (n > int(sizeof(buf)) - 1)
	n = int(sizeof(buf)) - 1;
      sprintf(buf, "%.*d", n, 0);
      return buf;
    }
    else
      return "0";
  }
  else {
    buf[0] = format;
    buf[1] = '\0';
    return buf;
  }
}

const char *general_reg::get_format()
{
  return number_format_to_ascii(format, width);
}

class number_reg : public general_reg {
  units value;
public:
  number_reg();
  int get_value(units *);
  void set_value(units);
};

number_reg::number_reg() : value(0)
{
}

int number_reg::get_value(units *res)
{
  *res = value;
  return 1;
}

void number_reg::set_value(units n)
{
  value = n;
}

variable_reg::variable_reg(units *p) : ptr(p)
{
}

void variable_reg::set_value(units n)
{
  *ptr = n;
}

int variable_reg::get_value(units *res)
{
  *res = *ptr;
  return 1;
}

void define_number_reg()
{
  symbol nm = get_name(1);
  if (nm.is_null()) {
    skip_line();
    return;
  }
  reg *r = (reg *)number_reg_dictionary.lookup(nm);
  units v;
  units prev_value;
  if (!r || !r->get_value(&prev_value))
    prev_value = 0;
  if (get_number(&v, 'u', prev_value)) {
    if (r == 0) {
      r = new number_reg;
      number_reg_dictionary.define(nm, r);
    }
    r->set_value(v);
    if (tok.space() && has_arg() && get_number(&v, 'u'))
      r->set_increment(v);
  }
  skip_line();
}

#if 0
void inline_define_reg()
{
  token start;
  start.next();
  if (!start.delimiter(1))
    return;
  tok.next();
  symbol nm = get_name(1);
  if (nm.is_null())
    return;
  reg *r = (reg *)number_reg_dictionary.lookup(nm);
  if (r == 0) {
    r = new number_reg;
    number_reg_dictionary.define(nm, r);
  }
  units v;
  units prev_value;
  if (!r->get_value(&prev_value))
    prev_value = 0;
  if (get_number(&v, 'u', prev_value)) {
    r->set_value(v);
    if (start != tok) {
      if (get_number(&v, 'u')) {
	r->set_increment(v);
	if (start != tok)
	  warning(WARN_DELIM, "closing delimiter does not match");
      }
    }
  }
}
#endif

void set_number_reg(symbol nm, units n)
{
  reg *r = (reg *)number_reg_dictionary.lookup(nm);
  if (r == 0) {
    r = new number_reg;
    number_reg_dictionary.define(nm, r);
  }
  r->set_value(n);
}

reg *lookup_number_reg(symbol nm)
{
  reg *r = (reg *)number_reg_dictionary.lookup(nm);
  if (r == 0) {
    warning(WARN_REG, "number register `%1' not defined", nm.contents());
    r = new number_reg;
    number_reg_dictionary.define(nm, r);
  }
  return r;
}

void alter_format()
{
  symbol nm = get_name(1);
  if (nm.is_null()) {
    skip_line();
    return;
  }
  reg *r = (reg *)number_reg_dictionary.lookup(nm);
  if (r == 0) {
    r = new number_reg;
    number_reg_dictionary.define(nm, r);
  }
  tok.skip();
  char c = tok.ch();
  if (csdigit(c)) {
    int n = 0;
    do {
      ++n;
      tok.next();
    } while (csdigit(tok.ch()));
    r->alter_format('1', n);
  }
  else if (c == 'i' || c == 'I' || c == 'a' || c == 'A')
    r->alter_format(c);
  else if (tok.newline() || tok.eof())
    warning(WARN_MISSING, "missing number register format");
  else
    error("bad number register format (got %1)", tok.description());
  skip_line();
}

void remove_reg()
{
  for (;;) {
    symbol s = get_name();
    if (s.is_null())
      break;
    number_reg_dictionary.remove(s);
  }
  skip_line();
}

void alias_reg()
{
  symbol s1 = get_name(1);
  if (!s1.is_null()) {
    symbol s2 = get_name(1);
    if (!s2.is_null()) {
      if (!number_reg_dictionary.alias(s1, s2))
	warning(WARN_REG, "number register `%1' not defined", s2.contents());
    }
  }
  skip_line();
}

void rename_reg()
{
  symbol s1 = get_name(1);
  if (!s1.is_null()) {
    symbol s2 = get_name(1);
    if (!s2.is_null())
      number_reg_dictionary.rename(s1, s2);
  }
  skip_line();
}

void print_number_regs()
{
  object_dictionary_iterator iter(number_reg_dictionary);
  reg *r;
  symbol s;
  while (iter.get(&s, (object **)&r)) {
    assert(!s.is_null());
    errprint("%1\t", s.contents());
    const char *p = r->get_string();
    if (p)
      errprint(p);
    errprint("\n");
  }
  fflush(stderr);
  skip_line();
}

void init_reg_requests()
{
  init_request("rr", remove_reg);
  init_request("nr", define_number_reg);
  init_request("af", alter_format);
  init_request("aln", alias_reg);
  init_request("rnn", rename_reg);
  init_request("pnr", print_number_regs);
}