C89 calculates goto (again) as

I need to code machines, and I came across this old need for computed goto (ala fortran4 :))

I need to encode this in portable ansi-C.

I want to stay away from β€œdon't do this”, away from longjmp / setjmp, away from the built-in ASM (), from extensions without ansi-C.

Does anyone know how to do this?

+1
source share
2 answers

As I said in a comment, despite your request not to use anything but goto, the C standard cannot offer anything.

, . , . - :

struct state;
typedef void state_func(struct state*);
#define NULL_ACTION_ADDRESS (state_func*)0

struct state {
    state_func    *action;
    int            value1;
    int            value2;
};

#define INIT_STATE { initial_action, -1, -1}

state_func initial_action;
state_func handle_a;
state_func handle_b;

int main(void) {
    struct state s = INIT_STATE;

    while(s.action != NULL_ACTION_ADDRESS) {
        (*s.action)(&s);
    }

    return 0;
}

void initial_action(struct state* ps) {
    ps->action = &handle_a;
}

void handle_a(struct state* ps) {
    ps->action = &handle_b;
}

void handle_b(struct state* ps) {
    ps->action = NULL_ACTION_ADDRESS;
}
+2

, , , , , ansi C, , . , stackoverflow, "" addr , , , gcc/clang non ansi, asm.

tonite .

include cgoto.h

#ifndef CGOTO_dcl
#define CGOTO_dcl(N) int CGOTO_##N
#define CGOTO_LE(l) l,
#define CGOTO_LG(l) case l:goto l;
#define CGOTO_def(N)                                             \
  if(0){typedef enum {N(CGOTO_LE)} N; CGOTO_##N: switch(CGOTO_##N)\
  {N(CGOTO_LG) default:CGOTO_##N=0;goto CGOTO_##N;}}
#define CGOTO(N,i) CGOTO_##N=i; goto CGOTO_##N;
#endif

#include <stdio.h>
#include "cgoto.h"
int f(int x)
{ //...
  CGOTO_dcl(gtb);
  //...
# define gtb(L) L(l0) L(l1) L(l2)
  CGOTO_def(gtb);
  //...

  CGOTO(gtb,x);
  l0: printf("error\n");
  return(0);

  //...

  l1:return(11);
  l2:return(22);
  l3:return(33);
}

int main()
{ printf("f(0)=%d f(1)=%d f(2)=%d,f(3)=%d\n",f(0),f(1),f(2),f(3));
}

- 2 (), , . , , , && label .

( ) (), .

goto_table_index, (multi threadable), .

1- "" ( ) , , - "". , , 1- .

CGOTO_dcl(gtb);

"gtb" , .

  # define gtb(L) L(l0) L(l1) L(l2)
  CGOTO_def(gtb);

gtb, / L (label), , , . #define switch(), / #define , .

#define CGOTO_def(), . CGOTO_def(), , , (), .

uniq .

CGOTO(gtb,x);
...
CGOTO(gtb,y);

# define gtb1(L) L(l0) L(l1) L(l2)
  CGOTO_def(gtb1);
# define gtb2(L) L(l0) L(l4) L(l5)
  CGOTO_def(gtb2);

, , , 2 : #define CGOTO_def(), , .

FTN4:)

Cheers, Phi

+2

Source: https://habr.com/ru/post/1652882/


All Articles