diff options
author | 2003-11-04 08:10:06 +0000 | |
---|---|---|
committer | 2003-11-04 08:10:06 +0000 | |
commit | 4e2c9909df16bf849b60be24be7d280882496c81 (patch) | |
tree | e846b60dce53d139c16b8d3dbd8dc7b2ea5985e4 | |
parent | old __syscall test is really a wait() test (diff) | |
download | wireguard-openbsd-4e2c9909df16bf849b60be24be7d280882496c81.tar.xz wireguard-openbsd-4e2c9909df16bf849b60be24be7d280882496c81.zip |
Duh, a stack machine without swap; implement GNU compatible 'r'
(swap) operator. Prompted by Michael Knudsen <e at molioner dot dk>
-rw-r--r-- | usr.bin/dc/bcode.c | 12 | ||||
-rw-r--r-- | usr.bin/dc/dc.1 | 5 | ||||
-rw-r--r-- | usr.bin/dc/extern.h | 3 | ||||
-rw-r--r-- | usr.bin/dc/stack.c | 18 |
4 files changed, 32 insertions, 6 deletions
diff --git a/usr.bin/dc/bcode.c b/usr.bin/dc/bcode.c index d4a9d68d063..630a5ee4c04 100644 --- a/usr.bin/dc/bcode.c +++ b/usr.bin/dc/bcode.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bcode.c,v 1.12 2003/10/22 14:58:32 otto Exp $ */ +/* $OpenBSD: bcode.c,v 1.13 2003/11/04 08:10:06 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -17,7 +17,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: bcode.c,v 1.12 2003/10/22 14:58:32 otto Exp $"; +static const char rcsid[] = "$OpenBSD: bcode.c,v 1.13 2003/11/04 08:10:06 otto Exp $"; #endif /* not lint */ #include <ssl/ssl.h> @@ -69,6 +69,7 @@ static __inline void print_tos(void); static __inline void pop_print(void); static __inline void print_stack(); static __inline void dup(void); +static void swap(void); static void get_scale(void); static void set_scale(void); @@ -158,6 +159,7 @@ static const struct jump_entry jump_table_data[] = { { 'l', load }, { 'L', load_stack }, { 'd', dup }, + { 'r', swap }, { 'p', print_tos }, { 'P', pop_print }, { 'f', print_stack }, @@ -479,6 +481,12 @@ dup(void) } static void +swap(void) +{ + stack_swap(&bmachine.stack); +} + +static void get_scale(void) { struct number *n; diff --git a/usr.bin/dc/dc.1 b/usr.bin/dc/dc.1 index 18cffca76e0..f276f74618d 100644 --- a/usr.bin/dc/dc.1 +++ b/usr.bin/dc/dc.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: dc.1,v 1.13 2003/10/23 09:01:42 jmc Exp $ +.\" $OpenBSD: dc.1,v 1.14 2003/11/04 08:10:06 otto Exp $ .\" .\" Copyright (C) Caldera International Inc. 2001-2002. .\" All rights reserved. @@ -214,6 +214,9 @@ by that value. Exits the program. If executing a string, the recursion level is popped by two. +.It Ic r +The top two values on the stack are reversed (swapped). +This is a non-portable extension. .It Ic S Ns Ar x Register .Ar x diff --git a/usr.bin/dc/extern.h b/usr.bin/dc/extern.h index 3c0dc8de205..386b3bc7c51 100644 --- a/usr.bin/dc/extern.h +++ b/usr.bin/dc/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.1 2003/09/19 17:58:25 otto Exp $ */ +/* $OpenBSD: extern.h,v 1.2 2003/11/04 08:10:06 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -43,6 +43,7 @@ void bn_checkp(const void *); void stack_init(struct stack *); void stack_free_value(struct value *); struct value *stack_dup_value(const struct value *, struct value *); +void stack_swap(struct stack *); int stack_size(const struct stack *); void stack_dup(struct stack *); void stack_pushnumber(struct stack *, struct number *); diff --git a/usr.bin/dc/stack.c b/usr.bin/dc/stack.c index d700b787677..03e2f771369 100644 --- a/usr.bin/dc/stack.c +++ b/usr.bin/dc/stack.c @@ -1,4 +1,4 @@ -/* $OpenBSD: stack.c,v 1.4 2003/10/18 20:34:26 otto Exp $ */ +/* $OpenBSD: stack.c,v 1.5 2003/11/04 08:10:06 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -17,7 +17,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: stack.c,v 1.4 2003/10/18 20:34:26 otto Exp $"; +static const char rcsid[] = "$OpenBSD: stack.c,v 1.5 2003/11/04 08:10:06 otto Exp $"; #endif /* not lint */ #include <err.h> @@ -116,6 +116,20 @@ stack_dup(struct stack *stack) stack_push(stack, stack_dup_value(value, ©)); } +void +stack_swap(struct stack *stack) +{ + struct value copy; + + if (stack->sp < 1) { + warnx("stack empty"); + return; + } + copy = stack->stack[stack->sp]; + stack->stack[stack->sp] = stack->stack[stack->sp-1]; + stack->stack[stack->sp-1] = copy; +} + static void stack_grow(struct stack *stack) { |